zrpc.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // ZRPC generates grpc code directly by protoc and generates
  20. // zrpc code by goctl.
  21. func ZRPC(c *cli.Context) error {
  22. args := c.Parent().Args()
  23. protocArgs := removeGoctlFlag(args)
  24. pwd, err := os.Getwd()
  25. if err != nil {
  26. return err
  27. }
  28. source, err := getSourceProto(c.Args(), pwd)
  29. if err != nil {
  30. return err
  31. }
  32. grpcOutList := c.StringSlice("go-grpc_out")
  33. goOutList := c.StringSlice("go_out")
  34. zrpcOut := c.String("zrpc_out")
  35. style := c.String("style")
  36. home := c.String("home")
  37. remote := c.String("remote")
  38. branch := c.String("branch")
  39. verbose := c.Bool("verbose")
  40. if len(grpcOutList) == 0 {
  41. return errInvalidGrpcOutput
  42. }
  43. if len(goOutList) == 0 {
  44. return errInvalidGoOutput
  45. }
  46. goOut := goOutList[len(goOutList)-1]
  47. grpcOut := grpcOutList[len(grpcOutList)-1]
  48. if len(goOut) == 0 {
  49. return errInvalidGrpcOutput
  50. }
  51. if len(zrpcOut) == 0 {
  52. return errInvalidZrpcOutput
  53. }
  54. goOutAbs, err := filepath.Abs(goOut)
  55. if err != nil {
  56. return err
  57. }
  58. grpcOutAbs, err := filepath.Abs(grpcOut)
  59. if err != nil {
  60. return err
  61. }
  62. err = pathx.MkdirIfNotExist(goOutAbs)
  63. if err != nil {
  64. return err
  65. }
  66. err = pathx.MkdirIfNotExist(grpcOutAbs)
  67. if err != nil {
  68. return err
  69. }
  70. if len(remote) > 0 {
  71. repo, _ := util.CloneIntoGitHome(remote, branch)
  72. if len(repo) > 0 {
  73. home = repo
  74. }
  75. }
  76. if len(home) > 0 {
  77. pathx.RegisterGoctlHome(home)
  78. }
  79. if !filepath.IsAbs(zrpcOut) {
  80. zrpcOut = filepath.Join(pwd, zrpcOut)
  81. }
  82. isGooglePlugin := len(grpcOut) > 0
  83. goOut, err = filepath.Abs(goOut)
  84. if err != nil {
  85. return err
  86. }
  87. grpcOut, err = filepath.Abs(grpcOut)
  88. if err != nil {
  89. return err
  90. }
  91. zrpcOut, err = filepath.Abs(zrpcOut)
  92. if err != nil {
  93. return err
  94. }
  95. var ctx generator.ZRpcContext
  96. ctx.Src = source
  97. ctx.GoOutput = goOut
  98. ctx.GrpcOutput = grpcOut
  99. ctx.IsGooglePlugin = isGooglePlugin
  100. ctx.Output = zrpcOut
  101. ctx.ProtocCmd = strings.Join(protocArgs, " ")
  102. g := generator.NewGenerator(style, verbose)
  103. return g.Generate(&ctx)
  104. }
  105. func removeGoctlFlag(args []string) []string {
  106. var ret []string
  107. var step int
  108. for step < len(args) {
  109. arg := args[step]
  110. switch {
  111. case arg == "--style", arg == "--home", arg == "--zrpc_out", arg == "--verbose", arg == "-v":
  112. step += 2
  113. continue
  114. case strings.HasPrefix(arg, "--style="),
  115. strings.HasPrefix(arg, "--home="),
  116. strings.HasPrefix(arg, "--verbose="),
  117. strings.HasPrefix(arg, "-v="),
  118. strings.HasPrefix(arg, "--zrpc_out="):
  119. step += 1
  120. continue
  121. }
  122. step += 1
  123. ret = append(ret, arg)
  124. }
  125. return ret
  126. }
  127. func getSourceProto(args []string, pwd string) (string, error) {
  128. var source []string
  129. for _, p := range args {
  130. if strings.HasSuffix(p, ".proto") {
  131. source = append(source, p)
  132. }
  133. }
  134. switch len(source) {
  135. case 0:
  136. return "", errInvalidInput
  137. case 1:
  138. isAbs := filepath.IsAbs(source[0])
  139. if isAbs {
  140. return source[0], nil
  141. }
  142. abs := filepath.Join(pwd, source[0])
  143. return abs, nil
  144. default:
  145. return "", errMultiInput
  146. }
  147. }
  148. func removePluginFlag(goOut string) string {
  149. goOut = strings.ReplaceAll(goOut, "plugins=", "")
  150. index := strings.LastIndex(goOut, ":")
  151. if index < 0 {
  152. return goOut
  153. }
  154. return goOut[index+1:]
  155. }