|
@@ -2,12 +2,10 @@ package cli
|
|
|
|
|
|
import (
|
|
import (
|
|
"errors"
|
|
"errors"
|
|
- "fmt"
|
|
|
|
"os"
|
|
"os"
|
|
"path/filepath"
|
|
"path/filepath"
|
|
"strings"
|
|
"strings"
|
|
|
|
|
|
- "github.com/emicklei/proto"
|
|
|
|
"github.com/urfave/cli"
|
|
"github.com/urfave/cli"
|
|
"github.com/zeromicro/go-zero/tools/goctl/rpc/generator"
|
|
"github.com/zeromicro/go-zero/tools/goctl/rpc/generator"
|
|
"github.com/zeromicro/go-zero/tools/goctl/util"
|
|
"github.com/zeromicro/go-zero/tools/goctl/util"
|
|
@@ -15,7 +13,8 @@ import (
|
|
)
|
|
)
|
|
|
|
|
|
var (
|
|
var (
|
|
- errInvalidGrpcOutput = errors.New("ZRPC: missing grpc output")
|
|
|
|
|
|
+ errInvalidGrpcOutput = errors.New("ZRPC: missing --go-grpc_out")
|
|
|
|
+ errInvalidGoOutput = errors.New("ZRPC: missing --go_out")
|
|
errInvalidZrpcOutput = errors.New("ZRPC: missing zrpc output, please use --zrpc_out to specify the output")
|
|
errInvalidZrpcOutput = errors.New("ZRPC: missing zrpc output, please use --zrpc_out to specify the output")
|
|
errInvalidInput = errors.New("ZRPC: missing source")
|
|
errInvalidInput = errors.New("ZRPC: missing source")
|
|
errMultiInput = errors.New("ZRPC: only one source is expected")
|
|
errMultiInput = errors.New("ZRPC: only one source is expected")
|
|
@@ -40,21 +39,29 @@ func ZRPC(c *cli.Context) error {
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
- src := filepath.Dir(source)
|
|
|
|
- goPackage, protoPkg, err := getGoPackage(source)
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
|
|
|
|
- grpcOut := c.String("go-grpc_out")
|
|
|
|
- goOut := c.String("go_out")
|
|
|
|
- goOpt := c.String("go_opt")
|
|
|
|
- grpcOpt := c.String("go-grpc_opt")
|
|
|
|
|
|
+ grpcOutList := c.StringSlice("go-grpc_out")
|
|
|
|
+ goOutList := c.StringSlice("go_out")
|
|
zrpcOut := c.String("zrpc_out")
|
|
zrpcOut := c.String("zrpc_out")
|
|
style := c.String("style")
|
|
style := c.String("style")
|
|
home := c.String("home")
|
|
home := c.String("home")
|
|
remote := c.String("remote")
|
|
remote := c.String("remote")
|
|
branch := c.String("branch")
|
|
branch := c.String("branch")
|
|
|
|
+ if len(grpcOutList) == 0 {
|
|
|
|
+ return errInvalidGrpcOutput
|
|
|
|
+ }
|
|
|
|
+ if len(goOutList) == 0 {
|
|
|
|
+ return errInvalidGoOutput
|
|
|
|
+ }
|
|
|
|
+ goOut := goOutList[len(goOutList)-1]
|
|
|
|
+ grpcOut := grpcOutList[len(grpcOutList)-1]
|
|
|
|
+ if len(goOut) == 0 {
|
|
|
|
+ return errInvalidGrpcOutput
|
|
|
|
+ }
|
|
|
|
+ if len(zrpcOut) == 0 {
|
|
|
|
+ return errInvalidZrpcOutput
|
|
|
|
+ }
|
|
|
|
+
|
|
if len(remote) > 0 {
|
|
if len(remote) > 0 {
|
|
repo, _ := util.CloneIntoGitHome(remote, branch)
|
|
repo, _ := util.CloneIntoGitHome(remote, branch)
|
|
if len(repo) > 0 {
|
|
if len(repo) > 0 {
|
|
@@ -65,38 +72,11 @@ func ZRPC(c *cli.Context) error {
|
|
if len(home) > 0 {
|
|
if len(home) > 0 {
|
|
pathx.RegisterGoctlHome(home)
|
|
pathx.RegisterGoctlHome(home)
|
|
}
|
|
}
|
|
- if len(goOut) == 0 {
|
|
|
|
- return errInvalidGrpcOutput
|
|
|
|
- }
|
|
|
|
- if len(zrpcOut) == 0 {
|
|
|
|
- return errInvalidZrpcOutput
|
|
|
|
- }
|
|
|
|
if !filepath.IsAbs(zrpcOut) {
|
|
if !filepath.IsAbs(zrpcOut) {
|
|
zrpcOut = filepath.Join(pwd, zrpcOut)
|
|
zrpcOut = filepath.Join(pwd, zrpcOut)
|
|
}
|
|
}
|
|
|
|
|
|
- goOut = removePluginFlag(goOut)
|
|
|
|
- goOut, err = parseOut(src, goOut, goOpt, goPackage)
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
isGooglePlugin := len(grpcOut) > 0
|
|
isGooglePlugin := len(grpcOut) > 0
|
|
- // If grpcOut is not empty means that user generates grpc code by
|
|
|
|
- // https://google.golang.org/protobuf/cmd/protoc-gen-go and
|
|
|
|
- // https://google.golang.org/grpc/cmd/protoc-gen-go-grpc,
|
|
|
|
- // for details please see https://grpc.io/docs/languages/go/quickstart/
|
|
|
|
- if isGooglePlugin {
|
|
|
|
- grpcOut, err = parseOut(src, grpcOut, grpcOpt, goPackage)
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- // Else it means that user generates grpc code by
|
|
|
|
- // https://github.com/golang/protobuf/tree/master/protoc-gen-go
|
|
|
|
- grpcOut = goOut
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
goOut, err = filepath.Abs(goOut)
|
|
goOut, err = filepath.Abs(goOut)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
@@ -110,23 +90,11 @@ func ZRPC(c *cli.Context) error {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
- if isGooglePlugin && grpcOut != goOut {
|
|
|
|
- return fmt.Errorf("the --go_out and --go-grpc_out must be the same")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if goOut == zrpcOut || grpcOut == zrpcOut {
|
|
|
|
- recommendName := goPackage
|
|
|
|
- if len(recommendName) == 0 {
|
|
|
|
- recommendName = protoPkg
|
|
|
|
- }
|
|
|
|
- return fmt.Errorf("the zrpc and grpc output can not be the same, it is recommended to output grpc to the %q",
|
|
|
|
- filepath.Join(goOut, recommendName))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
var ctx generator.ZRpcContext
|
|
var ctx generator.ZRpcContext
|
|
ctx.Src = source
|
|
ctx.Src = source
|
|
- ctx.ProtoGenGoDir = goOut
|
|
|
|
- ctx.ProtoGenGrpcDir = grpcOut
|
|
|
|
|
|
+ ctx.GoOutput = goOut
|
|
|
|
+ ctx.GrpcOutput = grpcOut
|
|
|
|
+ ctx.IsGooglePlugin = isGooglePlugin
|
|
ctx.Output = zrpcOut
|
|
ctx.Output = zrpcOut
|
|
ctx.ProtocCmd = strings.Join(protocArgs, " ")
|
|
ctx.ProtocCmd = strings.Join(protocArgs, " ")
|
|
g, err := generator.NewDefaultRPCGenerator(style, generator.WithZRpcContext(&ctx))
|
|
g, err := generator.NewDefaultRPCGenerator(style, generator.WithZRpcContext(&ctx))
|
|
@@ -137,52 +105,6 @@ func ZRPC(c *cli.Context) error {
|
|
return g.Generate(source, zrpcOut, nil)
|
|
return g.Generate(source, zrpcOut, nil)
|
|
}
|
|
}
|
|
|
|
|
|
-// parseOut calculates the output place to grpc code, about to calculate logic for details
|
|
|
|
-// please see https://developers.google.com/protocol-buffers/docs/reference/go-generated#invocation.
|
|
|
|
-func parseOut(sourceDir, grpcOut, grpcOpt, goPackage string) (string, error) {
|
|
|
|
- if !filepath.IsAbs(grpcOut) {
|
|
|
|
- grpcOut = filepath.Join(sourceDir, grpcOut)
|
|
|
|
- }
|
|
|
|
- switch grpcOpt {
|
|
|
|
- case "", optImport:
|
|
|
|
- grpcOut = filepath.Join(grpcOut, goPackage)
|
|
|
|
- case optSourceRelative:
|
|
|
|
- grpcOut = filepath.Join(grpcOut)
|
|
|
|
- default:
|
|
|
|
- return "", fmt.Errorf("parseAndSetGrpcOut: unknown path type %q: want %q or %q",
|
|
|
|
- grpcOpt, optImport, optSourceRelative)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return grpcOut, nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func getGoPackage(source string) (string, string, error) {
|
|
|
|
- r, err := os.Open(source)
|
|
|
|
- if err != nil {
|
|
|
|
- return "", "", err
|
|
|
|
- }
|
|
|
|
- defer func() {
|
|
|
|
- _ = r.Close()
|
|
|
|
- }()
|
|
|
|
-
|
|
|
|
- parser := proto.NewParser(r)
|
|
|
|
- set, err := parser.Parse()
|
|
|
|
- if err != nil {
|
|
|
|
- return "", "", err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- var goPackage, protoPkg string
|
|
|
|
- proto.Walk(set, proto.WithOption(func(option *proto.Option) {
|
|
|
|
- if option.Name == "go_package" {
|
|
|
|
- goPackage = option.Constant.Source
|
|
|
|
- }
|
|
|
|
- }), proto.WithPackage(func(p *proto.Package) {
|
|
|
|
- protoPkg = p.Name
|
|
|
|
- }))
|
|
|
|
-
|
|
|
|
- return goPackage, protoPkg, nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func removeGoctlFlag(args []string) []string {
|
|
func removeGoctlFlag(args []string) []string {
|
|
var ret []string
|
|
var ret []string
|
|
var step int
|
|
var step int
|