Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7e8dc13992 | |||
|
323617361d
|
@@ -30,6 +30,7 @@ func init() {
|
|||||||
rootCmd.AddCommand(bootstrap.AddServiceCommand())
|
rootCmd.AddCommand(bootstrap.AddServiceCommand())
|
||||||
rootCmd.AddCommand(bootstrap.GenerateProtoFile())
|
rootCmd.AddCommand(bootstrap.GenerateProtoFile())
|
||||||
rootCmd.AddCommand(bootstrap.InjectProtoFile())
|
rootCmd.AddCommand(bootstrap.InjectProtoFile())
|
||||||
|
rootCmd.AddCommand(bootstrap.AddProtoFormat())
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
54
coco_proto_format.go
Normal file
54
coco_proto_format.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package bootstrap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"github.com/emicklei/proto"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"gitter.top/sync/proto-contrib/pkg/protofmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func AddProtoFormat() *cobra.Command {
|
||||||
|
var protoFile string
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "pbf",
|
||||||
|
Short: "protobuf format",
|
||||||
|
PreRun: func(_ *cobra.Command, args []string) {
|
||||||
|
if protoFile == "" && len(args) != 0 {
|
||||||
|
protoFile = args[0]
|
||||||
|
}
|
||||||
|
stat, err := os.Stat(protoFile)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatalf("read proto file %s failed: %v", protoFile, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if stat.IsDir() {
|
||||||
|
logrus.Fatalf("proto file can not be directory")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
file, err := os.Open(protoFile)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("format proto file %s failed: %v", protoFile, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
parser := proto.NewParser(file)
|
||||||
|
parser.Filename(protoFile)
|
||||||
|
def, err := parser.Parse()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("parse proto file %s failed: %v", protoFile, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
protofmt.NewFormatter(buf, " ").Format(def)
|
||||||
|
if err := os.WriteFile(protoFile, buf.Bytes(), os.ModePerm); err != nil {
|
||||||
|
logrus.Errorf("write proto file %s failed: %v", protoFile, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cmd.Flags().StringVar(&protoFile, "path", "", "proto file path")
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package bootstrap
|
package bootstrap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@@ -56,17 +55,16 @@ func AddServiceCommand() *cobra.Command {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err := gobuf.CreateFile(pbPath)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("read proto file failed (when create new file): %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
buf, err := gobuf.NewParser(pbPath)
|
buf, err := gobuf.NewParser(pbPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !errors.Is(err, os.ErrNotExist) {
|
logrus.Errorf("read proto file failed: %v", err)
|
||||||
logrus.Errorf("read proto file failed: %v", err)
|
return
|
||||||
return
|
|
||||||
}
|
|
||||||
err := buf.CreateFile(pbPath)
|
|
||||||
if err != nil {
|
|
||||||
logrus.Errorf("read proto file failed (when create new file): %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if buf.ExistService(svcName) {
|
if buf.ExistService(svcName) {
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -6,7 +6,7 @@ require (
|
|||||||
code.gitea.io/sdk/gitea v0.17.1
|
code.gitea.io/sdk/gitea v0.17.1
|
||||||
github.com/sirupsen/logrus v1.9.3
|
github.com/sirupsen/logrus v1.9.3
|
||||||
github.com/spf13/cobra v1.8.0
|
github.com/spf13/cobra v1.8.0
|
||||||
gitter.top/common/gobuf v0.0.3
|
gitter.top/common/gobuf v0.0.4
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user