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 Nov 1, 2022
1 parent c0d67a9 commit 2221cb7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
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.8.1
github.com/urfave/cli/v2 v2.23.0
google.golang.org/protobuf v1.28.0
gopkg.in/yaml.v2 v2.4.0
)
9 changes: 5 additions & 4 deletions cmd/hz/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/cloudwego/thriftgo v0.1.7 h1:mTGRv6Dtwfp0hTPZXuIHwm3vtGOuZVTrWarI0xVzUYg=
github.com/cloudwego/thriftgo v0.1.7/go.mod h1:LzeafuLSiHA9JTiWC8TIMIq64iadeObgRUhmVG1OC/w=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down 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.8.1 h1:CGuYNZF9IKZY/rfBe3lJpccSoIY1ytfvmgQT90cNOl4=
github.com/urfave/cli/v2 v2.8.1/go.mod h1:Z41J9TPoffeoqP0Iza0YbAhGvymRdZAd2uPmZ5JxRdY=
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 Expand Up @@ -125,5 +125,6 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
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 ";"
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)
}
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 2221cb7

Please sign in to comment.