zrpc.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package cli
  2. import (
  3. "errors"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/urfave/cli"
  8. "github.com/zeromicro/go-zero/tools/goctl/rpc/generator"
  9. "github.com/zeromicro/go-zero/tools/goctl/util"
  10. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  11. )
  12. var (
  13. errInvalidGrpcOutput = errors.New("ZRPC: missing --go-grpc_out")
  14. errInvalidGoOutput = errors.New("ZRPC: missing --go_out")
  15. errInvalidZrpcOutput = errors.New("ZRPC: missing zrpc output, please use --zrpc_out to specify the output")
  16. errInvalidInput = errors.New("ZRPC: missing source")
  17. errMultiInput = errors.New("ZRPC: only one source is expected")
  18. )
  19. const (
  20. optImport = "import"
  21. optSourceRelative = "source_relative"
  22. )
  23. // ZRPC generates grpc code directly by protoc and generates
  24. // zrpc code by goctl.
  25. func ZRPC(c *cli.Context) error {
  26. args := c.Parent().Args()
  27. protocArgs := removeGoctlFlag(args)
  28. pwd, err := os.Getwd()
  29. if err != nil {
  30. return err
  31. }
  32. source, err := getSourceProto(c.Args(), pwd)
  33. if err != nil {
  34. return err
  35. }
  36. grpcOutList := c.StringSlice("go-grpc_out")
  37. goOutList := c.StringSlice("go_out")
  38. zrpcOut := c.String("zrpc_out")
  39. style := c.String("style")
  40. home := c.String("home")
  41. remote := c.String("remote")
  42. branch := c.String("branch")
  43. if len(grpcOutList) == 0 {
  44. return errInvalidGrpcOutput
  45. }
  46. if len(goOutList) == 0 {
  47. return errInvalidGoOutput
  48. }
  49. goOut := goOutList[len(goOutList)-1]
  50. grpcOut := grpcOutList[len(grpcOutList)-1]
  51. if len(goOut) == 0 {
  52. return errInvalidGrpcOutput
  53. }
  54. if len(zrpcOut) == 0 {
  55. return errInvalidZrpcOutput
  56. }
  57. if len(remote) > 0 {
  58. repo, _ := util.CloneIntoGitHome(remote, branch)
  59. if len(repo) > 0 {
  60. home = repo
  61. }
  62. }
  63. if len(home) > 0 {
  64. pathx.RegisterGoctlHome(home)
  65. }
  66. if !filepath.IsAbs(zrpcOut) {
  67. zrpcOut = filepath.Join(pwd, zrpcOut)
  68. }
  69. isGooglePlugin := len(grpcOut) > 0
  70. goOut, err = filepath.Abs(goOut)
  71. if err != nil {
  72. return err
  73. }
  74. grpcOut, err = filepath.Abs(grpcOut)
  75. if err != nil {
  76. return err
  77. }
  78. zrpcOut, err = filepath.Abs(zrpcOut)
  79. if err != nil {
  80. return err
  81. }
  82. var ctx generator.ZRpcContext
  83. ctx.Src = source
  84. ctx.GoOutput = goOut
  85. ctx.GrpcOutput = grpcOut
  86. ctx.IsGooglePlugin = isGooglePlugin
  87. ctx.Output = zrpcOut
  88. ctx.ProtocCmd = strings.Join(protocArgs, " ")
  89. g, err := generator.NewDefaultRPCGenerator(style, generator.WithZRpcContext(&ctx))
  90. if err != nil {
  91. return err
  92. }
  93. return g.Generate(source, zrpcOut, nil)
  94. }
  95. func removeGoctlFlag(args []string) []string {
  96. var ret []string
  97. var step int
  98. for step < len(args) {
  99. arg := args[step]
  100. switch {
  101. case arg == "--style", arg == "--home", arg == "--zrpc_out":
  102. step += 2
  103. continue
  104. case strings.HasPrefix(arg, "--style="),
  105. strings.HasPrefix(arg, "--home="),
  106. strings.HasPrefix(arg, "--zrpc_out="):
  107. step += 1
  108. continue
  109. }
  110. step += 1
  111. ret = append(ret, arg)
  112. }
  113. return ret
  114. }
  115. func getSourceProto(args []string, pwd string) (string, error) {
  116. var source []string
  117. for _, p := range args {
  118. if strings.HasSuffix(p, ".proto") {
  119. source = append(source, p)
  120. }
  121. }
  122. switch len(source) {
  123. case 0:
  124. return "", errInvalidInput
  125. case 1:
  126. isAbs := filepath.IsAbs(source[0])
  127. if isAbs {
  128. return source[0], nil
  129. }
  130. abs := filepath.Join(pwd, source[0])
  131. return abs, nil
  132. default:
  133. return "", errMultiInput
  134. }
  135. }
  136. func removePluginFlag(goOut string) string {
  137. goOut = strings.ReplaceAll(goOut, "plugins=", "")
  138. index := strings.LastIndex(goOut, ":")
  139. if index < 0 {
  140. return goOut
  141. }
  142. return goOut[index+1:]
  143. }