package bootstrap import ( "os" "os/exec" "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) func GenerateProtoFile() *cobra.Command { var protoFile string var onlyGout, onlyCocout bool cmd := &cobra.Command{ Use: "gen", Short: "generate proto file", PreRun: func(_ *cobra.Command, _ []string) { stat, err := os.Stat(protoFile) if err != nil { logrus.Errorf("read proto file %s failed: %v", protoFile, err) return } if stat.IsDir() { logrus.Errorf("proto file can not be directory") return } }, Run: func(_ *cobra.Command, _ []string) { var args = []string{protoFile} var goOut = "--go_out=.." var cocoOut = "--coco_out=.." if onlyGout { args = append(args, goOut) } if onlyCocout { args = append(args, cocoOut) } if !onlyCocout && !onlyGout { args = append(args, cocoOut, goOut) } var command = exec.Command("protoc", args...) output, err := command.CombinedOutput() logrus.Infof("exec command: %s", command.String()) if err != nil { logrus.Errorf("exec command failed: %v: %v\noutput: %v", command.String(), err, string(output)) return } logrus.Infof("generate %s success", protoFile) }, } cmd.Flags().StringVar(&protoFile, "path", "", "proto file path") cmd.Flags().BoolVar(&onlyGout, "onlyGo", false, "only generate by protoc-gen-go") cmd.Flags().BoolVar(&onlyCocout, "onlyCoco", false, "only generate by protoc-gen-coco") return cmd }