feat: new function for create file
This commit is contained in:
parent
83cdb3f6bb
commit
06d0f1cec5
26
gobuf.go
26
gobuf.go
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/emicklei/proto"
|
"github.com/emicklei/proto"
|
||||||
"gitter.top/sync/proto-contrib/pkg/protofmt"
|
"gitter.top/sync/proto-contrib/pkg/protofmt"
|
||||||
@ -35,6 +36,31 @@ func NewParser(file string) (*Parser, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (parser *Parser) 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\";\npackage proto.v1;\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 {
|
func (parser *Parser) ExistService(serviceName string) bool {
|
||||||
var result bool
|
var result bool
|
||||||
proto.Walk(parser.proto, proto.WithService(func(service *proto.Service) {
|
proto.Walk(parser.proto, proto.WithService(func(service *proto.Service) {
|
||||||
|
Loading…
Reference in New Issue
Block a user