zrpc.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package cli
  2. import (
  3. "errors"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/spf13/cobra"
  8. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/rpc/generator"
  9. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util"
  10. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/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. )
  17. // ZRPC generates grpc code directly by protoc and generates
  18. // zrpc code by goctl.
  19. func ZRPC(_ *cobra.Command, args []string) error {
  20. protocArgs := wrapProtocCmd("protoc", args)
  21. pwd, err := os.Getwd()
  22. if err != nil {
  23. return err
  24. }
  25. source := args[0]
  26. grpcOutList := VarStringSliceGoGRPCOut
  27. goOutList := VarStringSliceGoOut
  28. zrpcOut := VarStringZRPCOut
  29. style := VarStringStyle
  30. home := VarStringHome
  31. remote := VarStringRemote
  32. branch := VarStringBranch
  33. verbose := VarBoolVerbose
  34. if len(grpcOutList) == 0 {
  35. return errInvalidGrpcOutput
  36. }
  37. if len(goOutList) == 0 {
  38. return errInvalidGoOutput
  39. }
  40. goOut := goOutList[len(goOutList)-1]
  41. grpcOut := grpcOutList[len(grpcOutList)-1]
  42. if len(goOut) == 0 {
  43. return errInvalidGrpcOutput
  44. }
  45. if len(zrpcOut) == 0 {
  46. return errInvalidZrpcOutput
  47. }
  48. goOutAbs, err := filepath.Abs(goOut)
  49. if err != nil {
  50. return err
  51. }
  52. grpcOutAbs, err := filepath.Abs(grpcOut)
  53. if err != nil {
  54. return err
  55. }
  56. err = pathx.MkdirIfNotExist(goOutAbs)
  57. if err != nil {
  58. return err
  59. }
  60. err = pathx.MkdirIfNotExist(grpcOutAbs)
  61. if err != nil {
  62. return err
  63. }
  64. if len(remote) > 0 {
  65. repo, _ := util.CloneIntoGitHome(remote, branch)
  66. if len(repo) > 0 {
  67. home = repo
  68. }
  69. }
  70. if len(home) > 0 {
  71. pathx.RegisterGoctlHome(home)
  72. }
  73. if !filepath.IsAbs(zrpcOut) {
  74. zrpcOut = filepath.Join(pwd, zrpcOut)
  75. }
  76. isGooglePlugin := len(grpcOut) > 0
  77. goOut, err = filepath.Abs(goOut)
  78. if err != nil {
  79. return err
  80. }
  81. grpcOut, err = filepath.Abs(grpcOut)
  82. if err != nil {
  83. return err
  84. }
  85. zrpcOut, err = filepath.Abs(zrpcOut)
  86. if err != nil {
  87. return err
  88. }
  89. var ctx generator.ZRpcContext
  90. ctx.Multiple = VarBoolMultiple
  91. ctx.Src = source
  92. ctx.GoOutput = goOut
  93. ctx.GrpcOutput = grpcOut
  94. ctx.IsGooglePlugin = isGooglePlugin
  95. ctx.Output = zrpcOut
  96. ctx.ProtocCmd = strings.Join(protocArgs, " ")
  97. ctx.IsGenClient = VarBoolClient
  98. g := generator.NewGenerator(style, verbose)
  99. return g.Generate(&ctx)
  100. }
  101. func wrapProtocCmd(name string, args []string) []string {
  102. ret := append([]string{name}, args...)
  103. for _, protoPath := range VarStringSliceProtoPath {
  104. ret = append(ret, "--proto_path", protoPath)
  105. }
  106. for _, goOpt := range VarStringSliceGoOpt {
  107. ret = append(ret, "--go_opt", goOpt)
  108. }
  109. for _, goGRPCOpt := range VarStringSliceGoGRPCOpt {
  110. ret = append(ret, "--go-grpc_opt", goGRPCOpt)
  111. }
  112. for _, goOut := range VarStringSliceGoOut {
  113. ret = append(ret, "--go_out", goOut)
  114. }
  115. for _, goGRPCOut := range VarStringSliceGoGRPCOut {
  116. ret = append(ret, "--go-grpc_out", goGRPCOut)
  117. }
  118. for _, plugin := range VarStringSlicePlugin {
  119. ret = append(ret, "--plugin="+plugin)
  120. }
  121. return ret
  122. }