zrpc.go 3.5 KB

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