Compare commits

...

3 Commits

Author SHA1 Message Date
01e8e2905e
fix: create file protobuf content 2024-03-22 00:07:09 +08:00
d0a43019bc
fix: receive method to function 2024-03-20 23:45:19 +08:00
06d0f1cec5
feat: new function for create file 2024-03-20 23:36:09 +08:00

View File

@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/emicklei/proto"
"gitter.top/sync/proto-contrib/pkg/protofmt"
@ -35,6 +36,31 @@ func NewParser(file string) (*Parser, error) {
}, nil
}
func CreateFile(filename string) error {
// 检查文件是否存在
if _, err := os.Stat(filename); err == nil {
return nil
} else if !os.IsNotExist(err) {
return err
}
// 确保文件所在的目录存在
baseDir := filepath.Dir(filename)
if baseDir != "." && baseDir != ".." {
if err := os.MkdirAll(baseDir, os.ModePerm); err != nil {
return err
}
}
// 写入基础的protobuf内容此步骤会创建文件
baseContent := []byte("syntax = \"proto3\";\n\npackage proto.v1;\n\noption go_package = \"projects/gen;genv1\";\n")
if err := os.WriteFile(filename, baseContent, os.ModePerm); err != nil {
return err
}
return nil
}
func (parser *Parser) ExistService(serviceName string) bool {
var result bool
proto.Walk(parser.proto, proto.WithService(func(service *proto.Service) {