Skip to content

Commit

Permalink
feat: add plugin flag
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Oct 20, 2022
1 parent c0d67a9 commit 6791788
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
6 changes: 6 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 Down Expand Up @@ -142,6 +144,8 @@ func Init() *cli.App {
&excludeFilesFlag,
&customLayout,
&customPackage,
&protoPluginsFlag,
&thriftPluginsFlag,
},
Action: New,
},
Expand All @@ -166,6 +170,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)
}
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

0 comments on commit 6791788

Please sign in to comment.