From f5f5261556314b3d872d21dcb66ec1085b2080ca Mon Sep 17 00:00:00 2001 From: Mikael <13247629622@163.com> Date: Sat, 17 Jun 2023 20:52:49 +0800 Subject: [PATCH] add whether to generate rpc client option (#3361) Co-authored-by: admin --- tools/goctl/.gitignore | 1 + tools/goctl/internal/flags/default_en.json | 6 ++++-- tools/goctl/rpc/cli/cli.go | 3 +++ tools/goctl/rpc/cli/zrpc.go | 1 + tools/goctl/rpc/cmd.go | 2 ++ tools/goctl/rpc/generator/gen.go | 6 +++++- tools/goctl/rpc/generator/mkdir.go | 22 +++++++++------------- 7 files changed, 25 insertions(+), 16 deletions(-) diff --git a/tools/goctl/.gitignore b/tools/goctl/.gitignore index 722d5e71..8cd0df3a 100644 --- a/tools/goctl/.gitignore +++ b/tools/goctl/.gitignore @@ -1 +1,2 @@ .vscode +.idea \ No newline at end of file diff --git a/tools/goctl/internal/flags/default_en.json b/tools/goctl/internal/flags/default_en.json index eaf4bafd..95f2d99c 100644 --- a/tools/goctl/internal/flags/default_en.json +++ b/tools/goctl/internal/flags/default_en.json @@ -226,7 +226,8 @@ "home": "{{.global.home}}", "remote": "{{.global.remote}}", "branch": "{{.global.branch}}", - "verbose": "Enable log output" + "verbose": "Enable log output", + "client": "Whether to generate rpc client" }, "template": { "short": "Generate proto template", @@ -243,7 +244,8 @@ "home": "{{.global.home}}", "remote": "{{.global.remote}}", "branch": "{{.global.branch}}", - "verbose": "Enable log output" + "verbose": "Enable log output", + "client": "Whether to generate rpc client" } }, "template": { diff --git a/tools/goctl/rpc/cli/cli.go b/tools/goctl/rpc/cli/cli.go index fc1288d3..762c57a1 100644 --- a/tools/goctl/rpc/cli/cli.go +++ b/tools/goctl/rpc/cli/cli.go @@ -44,6 +44,8 @@ var ( VarBoolVerbose bool // VarBoolMultiple describes whether support generating multiple rpc services or not. VarBoolMultiple bool + // VarBoolClient describes whether to generate rpc client + VarBoolClient bool ) // RPCNew is to generate rpc greet service, this greet service can speed @@ -88,6 +90,7 @@ func RPCNew(_ *cobra.Command, args []string) error { ctx.IsGooglePlugin = true ctx.Output = filepath.Dir(src) ctx.ProtocCmd = fmt.Sprintf("protoc -I=%s %s --go_out=%s --go-grpc_out=%s", filepath.Dir(src), filepath.Base(src), filepath.Dir(src), filepath.Dir(src)) + ctx.IsGenClient = VarBoolClient grpcOptList := VarStringSliceGoGRPCOpt if len(grpcOptList) > 0 { diff --git a/tools/goctl/rpc/cli/zrpc.go b/tools/goctl/rpc/cli/zrpc.go index d79e2384..558d9778 100644 --- a/tools/goctl/rpc/cli/zrpc.go +++ b/tools/goctl/rpc/cli/zrpc.go @@ -102,6 +102,7 @@ func ZRPC(_ *cobra.Command, args []string) error { ctx.IsGooglePlugin = isGooglePlugin ctx.Output = zrpcOut ctx.ProtocCmd = strings.Join(protocArgs, " ") + ctx.IsGenClient = VarBoolClient g := generator.NewGenerator(style, verbose) return g.Generate(&ctx) } diff --git a/tools/goctl/rpc/cmd.go b/tools/goctl/rpc/cmd.go index d22e740e..0bb69c3a 100644 --- a/tools/goctl/rpc/cmd.go +++ b/tools/goctl/rpc/cmd.go @@ -43,6 +43,7 @@ func init() { newCmdFlags.BoolVarP(&cli.VarBoolVerbose, "verbose", "v") newCmdFlags.MarkHidden("go_opt") newCmdFlags.MarkHidden("go-grpc_opt") + newCmdFlags.BoolVarPWithDefaultValue(&cli.VarBoolClient, "client", "c", true) protocCmdFlags.BoolVarP(&cli.VarBoolMultiple, "multiple", "m") protocCmdFlags.StringSliceVar(&cli.VarStringSliceGoOut, "go_out") @@ -63,6 +64,7 @@ func init() { protocCmdFlags.MarkHidden("go-grpc_opt") protocCmdFlags.MarkHidden("plugin") protocCmdFlags.MarkHidden("proto_path") + protocCmdFlags.BoolVarPWithDefaultValue(&cli.VarBoolClient, "client", "c", true) templateCmdFlags.StringVar(&cli.VarStringOutput, "o") templateCmdFlags.StringVar(&cli.VarStringHome, "home") diff --git a/tools/goctl/rpc/generator/gen.go b/tools/goctl/rpc/generator/gen.go index f72f9066..3140d997 100644 --- a/tools/goctl/rpc/generator/gen.go +++ b/tools/goctl/rpc/generator/gen.go @@ -28,6 +28,8 @@ type ZRpcContext struct { Output string // Multiple is the flag to indicate whether the proto file is generated in multiple mode. Multiple bool + // Whether to generate rpc client + IsGenClient bool } // Generate generates a rpc service, through the proto file, @@ -100,7 +102,9 @@ func (g *Generator) Generate(zctx *ZRpcContext) error { return err } - err = g.GenCall(dirCtx, proto, g.cfg, zctx) + if zctx.IsGenClient { + err = g.GenCall(dirCtx, proto, g.cfg, zctx) + } console.NewColorConsole().MarkDone() diff --git a/tools/goctl/rpc/generator/mkdir.go b/tools/goctl/rpc/generator/mkdir.go index a92d2d8f..163f904a 100644 --- a/tools/goctl/rpc/generator/mkdir.go +++ b/tools/goctl/rpc/generator/mkdir.go @@ -87,6 +87,7 @@ func mkdir(ctx *ctx.ProjectContext, proto parser.Proto, conf *conf.Config, c *ZR return filepath.ToSlash(pkg), nil } + var callClientDir string if !c.Multiple { callDir := filepath.Join(ctx.WorkDir, strings.ToLower(stringx.From(proto.Service[0].Name).ToCamel())) @@ -98,23 +99,18 @@ func mkdir(ctx *ctx.ProjectContext, proto parser.Proto, conf *conf.Config, c *ZR } callDir = filepath.Join(ctx.WorkDir, clientDir) } - inner[call] = Dir{ - Filename: callDir, - Package: filepath.ToSlash(filepath.Join(ctx.Path, - strings.TrimPrefix(callDir, ctx.Dir))), - Base: filepath.Base(callDir), - GetChildPackage: func(childPath string) (string, error) { - return getChildPackage(callDir, childPath) - }, - } + callClientDir = callDir } else { + callClientDir = clientDir + } + if c.IsGenClient { inner[call] = Dir{ - Filename: clientDir, + Filename: callClientDir, Package: filepath.ToSlash(filepath.Join(ctx.Path, - strings.TrimPrefix(clientDir, ctx.Dir))), - Base: filepath.Base(clientDir), + strings.TrimPrefix(callClientDir, ctx.Dir))), + Base: filepath.Base(callClientDir), GetChildPackage: func(childPath string) (string, error) { - return getChildPackage(clientDir, childPath) + return getChildPackage(callClientDir, childPath) }, } }