Skip to content
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

feat(hz): add plugin flag #315

Merged
merged 1 commit into from
Nov 9, 2022
Merged
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
2 changes: 1 addition & 1 deletion cmd/hz/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/cloudwego/thriftgo v0.1.7
github.com/hashicorp/go-version v1.5.0
github.com/jhump/protoreflect v1.12.0
github.com/urfave/cli/v2 v2.20.2
github.com/urfave/cli/v2 v2.23.0
google.golang.org/protobuf v1.28.0
gopkg.in/yaml.v2 v2.4.0
)
4 changes: 2 additions & 2 deletions cmd/hz/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/urfave/cli/v2 v2.20.2 h1:dKA0LUjznZpwmmbrc0pOgcLTEilnHeM8Av9Yng77gHM=
github.com/urfave/cli/v2 v2.20.2/go.mod h1:1CNUng3PtjQMtRzJO4FMXBQvkGtuYRxxiR9xMa7jMwI=
github.com/urfave/cli/v2 v2.23.0 h1:pkly7gKIeYv3olPAeNajNpLjeJrmTPYCoZWaV+2VfvE=
github.com/urfave/cli/v2 v2.23.0/go.mod h1:1CNUng3PtjQMtRzJO4FMXBQvkGtuYRxxiR9xMa7jMwI=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
8 changes: 8 additions & 0 deletions cmd/hz/internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func Init() *cli.App {
excludeFilesFlag := cli.StringSliceFlag{Name: "exclude_file", Aliases: []string{"E"}, Usage: "Specify the files that do not need to be updated."}
thriftOptionsFlag := cli.StringSliceFlag{Name: "thriftgo", Aliases: []string{"t"}, Usage: "Specify arguments for the thriftgo. ({flag}={value})"}
protoOptionsFlag := cli.StringSliceFlag{Name: "protoc", Aliases: []string{"p"}, Usage: "Specify arguments for the protoc. ({flag}={value})"}
thriftPluginsFlag := cli.StringSliceFlag{Name: "thrift-plugins", Usage: "Specify plugins for the thriftgo. ({plugin_name}:{options})"}
protoPluginsFlag := cli.StringSliceFlag{Name: "protoc-plugins", Usage: "Specify plugins for the protoc. ({plugin_name}:{options}:{out_dir})"}
noRecurseFlag := cli.BoolFlag{Name: "no_recurse", Usage: "Generate master model only.", Destination: &globalArgs.NoRecurse}

jsonEnumStrFlag := cli.BoolFlag{Name: "json_enumstr", Usage: "Use string instead of num for json enums when idl is thrift.", Destination: &globalArgs.JSONEnumStr}
Expand All @@ -110,6 +112,8 @@ func Init() *cli.App {
app.Name = "hz"
app.Usage = "A idl parser and code generator for Hertz projects"
app.Version = meta.Version
// The default separator for multiple parameters is modified to ";"
Duslia marked this conversation as resolved.
Show resolved Hide resolved
app.SliceFlagSeparator = ";"

// global flags
app.Flags = []cli.Flag{
Expand Down Expand Up @@ -142,6 +146,8 @@ func Init() *cli.App {
&excludeFilesFlag,
&customLayout,
&customPackage,
&protoPluginsFlag,
&thriftPluginsFlag,
},
Action: New,
},
Expand All @@ -166,6 +172,8 @@ func Init() *cli.App {
&snakeNameFlag,
&excludeFilesFlag,
&customPackage,
&protoPluginsFlag,
&thriftPluginsFlag,
},
Action: Update,
},
Expand Down
18 changes: 11 additions & 7 deletions cmd/hz/internal/config/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ type Argument struct {
Gopkg string // $GOPATH/src/{{gopkg}}
ServiceName string // service name

JSONEnumStr bool
UnsetOmitempty bool
ProtocOptions []string // options to pass through to protoc
ThriftOptions []string // options to pass through to thriftgo
SnakeName bool
Excludes []string
NoRecurse bool
JSONEnumStr bool
UnsetOmitempty bool
ProtocOptions []string // options to pass through to protoc
ThriftOptions []string // options to pass through to thriftgo for go flag
ProtobufPlugins []string
ThriftPlugins []string
SnakeName bool
Excludes []string
NoRecurse bool

CustomizeLayout string
CustomizePackage string
Expand Down Expand Up @@ -106,6 +108,8 @@ func (arg *Argument) parseStringSlice(c *cli.Context) {
arg.RawOptPkg = c.StringSlice("option_package")
arg.ThriftOptions = c.StringSlice("thriftgo")
arg.ProtocOptions = c.StringSlice("protoc")
arg.ThriftPlugins = c.StringSlice("thrift-plugins")
arg.ProtobufPlugins = c.StringSlice("protoc-plugins")
}

// checkPath sets the project path and verifies that the model、handler、router and client path is compliant
Expand Down
16 changes: 16 additions & 0 deletions cmd/hz/internal/config/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ func BuildPluginCmd(args *Argument) (*exec.Cmd, error) {
"-g", thriftOpt,
"-p", "hertz:"+kas,
)
for _, p := range args.ThriftPlugins {
cmd.Args = append(cmd.Args, "-p", p)
welkeyever marked this conversation as resolved.
Show resolved Hide resolved
}
if !args.NoRecurse {
cmd.Args = append(cmd.Args, "-r")
}
Expand All @@ -185,6 +188,19 @@ func BuildPluginCmd(args *Argument) (*exec.Cmd, error) {
"--hertz_out="+args.OutDir,
"--hertz_opt="+kas,
)
for _, p := range args.ProtobufPlugins {
pluginParams := strings.Split(p, ":")
if len(pluginParams) != 3 {
logs.Warnf("Failed to get the correct protoc plugin parameters for %. "+
"Please specify the protoc plugin in the form of \"plugin_name:options:out_dir\"", p)
os.Exit(1)
}
// pluginParams[0] -> plugin name, pluginParams[1] -> plugin options, pluginParams[2] -> out_dir
cmd.Args = append(cmd.Args,
fmt.Sprintf("--%s_out=%s", pluginParams[0], pluginParams[2]),
fmt.Sprintf("--%s_opt=%s", pluginParams[0], pluginParams[1]),
)
}
for _, kv := range args.ProtocOptions {
cmd.Args = append(cmd.Args, "--"+kv)
}
Expand Down