Skip to content

[feat] execute protoc from stencil cli #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions cmd/protoc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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{"p"},
Short: "Execute a protoc command",
Example: "stencil protoc -- --descriptor_set_out \"<abs-path for op>\" --include_imports <abs-dir-path for import proto>/*.proto --proto_path \"<abs-path for ip>\"",
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))
fmt.Println("Successfully executed Protoc operation.")
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
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func New() *cobra.Command {
$ stencil download
$ stencil snapshot list
$ stencil serve
$ stencil protoc
`),
Annotations: map[string]string{
"group:core": "true",
Expand All @@ -42,5 +43,6 @@ func New() *cobra.Command {
cmd.AddCommand(MigrateCmd())
cmd.AddCommand(DownloadCmd())
cmd.AddCommand(Snapshot())
cmd.AddCommand(ProtocCmd())
return cmd
}