فهرست منبع

add whether to generate rpc client option (#3361)

Co-authored-by: admin <admin@admindeMacBook-Pro.local>
Mikael 1 سال پیش
والد
کامیت
f5f5261556

+ 1 - 0
tools/goctl/.gitignore

@@ -1 +1,2 @@
 .vscode
+.idea

+ 4 - 2
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": {

+ 3 - 0
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 {

+ 1 - 0
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)
 }

+ 2 - 0
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")

+ 5 - 1
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()
 

+ 9 - 13
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)
 			},
 		}
 	}