From 6533fa0d2bf686360decc196a587bbca5a2e20d4 Mon Sep 17 00:00:00 2001 From: Abhishek Sah Date: Thu, 16 Sep 2021 16:22:36 +0530 Subject: [PATCH 1/2] [feat]: add protoc command in stencil CLI for descriptor set generation Co-authored-by: arujit --- cmd/protoc.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 2 ++ 2 files changed, 56 insertions(+) create mode 100644 cmd/protoc.go diff --git a/cmd/protoc.go b/cmd/protoc.go new file mode 100644 index 00000000..96d1eee2 --- /dev/null +++ b/cmd/protoc.go @@ -0,0 +1,54 @@ +package cmd + +import ( + "errors" + "fmt" + "github.com/spf13/cobra" + "os/exec" +) + +// ProtocCmd calls protoc +func ProtocCmd() *cobra.Command { + var configFile string + + cmd := &cobra.Command{ + Use: "protoc", + Aliases: []string{"s"}, + Short: "Execute a protoc command", + RunE: func(cmd *cobra.Command, args []string) error { + app := "protoc" + command := exec.Command(app, args...) + protoPath, err := getProtoPathFromArgs(args) + if err != nil { + return err + } + //execute the protoc command in specified proto_path + if protoPath != "" { + command.Dir = protoPath + } + stdout, err := command.Output() + if err != nil { + fmt.Println(err.Error()) + return err + } + + fmt.Println(string(stdout)) + return nil + }, + } + + cmd.Flags().StringVarP(&configFile, "config", "c", "./config.yaml", "Config file path") + return cmd +} + +func getProtoPathFromArgs(args []string) (string, error) { + for i, arg := range args { + if arg == "--proto_path" { + if i+1 == len(args) { + return "", errors.New("--proto_path value not provided") + } + return args[i+1], nil + } + } + return "", nil +} diff --git a/cmd/root.go b/cmd/root.go index add4e5f7..073e6f9d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -26,6 +26,7 @@ func New() *cobra.Command { $ stencil download $ stencil snapshot list $ stencil serve + $ stencil protoc `), Annotations: map[string]string{ "group:core": "true", @@ -42,5 +43,6 @@ func New() *cobra.Command { cmd.AddCommand(MigrateCmd()) cmd.AddCommand(DownloadCmd()) cmd.AddCommand(Snapshot()) + cmd.AddCommand(ProtocCmd()) return cmd } From c40bdbe337b3ca362d60d58069c681078f2eef52 Mon Sep 17 00:00:00 2001 From: arujit Date: Sun, 19 Sep 2021 22:31:52 +0530 Subject: [PATCH 2/2] chore: add examples and desctiptions for protoc command --- cmd/protoc.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmd/protoc.go b/cmd/protoc.go index 96d1eee2..feef0f76 100644 --- a/cmd/protoc.go +++ b/cmd/protoc.go @@ -13,8 +13,9 @@ func ProtocCmd() *cobra.Command { cmd := &cobra.Command{ Use: "protoc", - Aliases: []string{"s"}, + Aliases: []string{"p"}, Short: "Execute a protoc command", + Example: "stencil protoc -- --descriptor_set_out \"\" --include_imports /*.proto --proto_path \"\"", RunE: func(cmd *cobra.Command, args []string) error { app := "protoc" command := exec.Command(app, args...) @@ -33,6 +34,7 @@ func ProtocCmd() *cobra.Command { } fmt.Println(string(stdout)) + fmt.Println("Successfully executed Protoc operation.") return nil }, }