cli.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package cli
  2. import (
  3. "errors"
  4. "fmt"
  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/console"
  11. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/pathx"
  12. )
  13. var (
  14. // VarStringOutput describes the output.
  15. VarStringOutput string
  16. // VarStringHome describes the goctl home.
  17. VarStringHome string
  18. // VarStringRemote describes the remote git repository.
  19. VarStringRemote string
  20. // VarStringBranch describes the git branch.
  21. VarStringBranch string
  22. // VarStringSliceGoOut describes the go output.
  23. VarStringSliceGoOut []string
  24. // VarStringSliceGoGRPCOut describes the grpc output.
  25. VarStringSliceGoGRPCOut []string
  26. // VarStringSlicePlugin describes the protoc plugin.
  27. VarStringSlicePlugin []string
  28. // VarStringSliceProtoPath describes the proto path.
  29. VarStringSliceProtoPath []string
  30. // VarStringSliceGoOpt describes the go options.
  31. VarStringSliceGoOpt []string
  32. // VarStringSliceGoGRPCOpt describes the grpc options.
  33. VarStringSliceGoGRPCOpt []string
  34. // VarStringStyle describes the style of output files.
  35. VarStringStyle string
  36. // VarStringZRPCOut describes the zRPC output.
  37. VarStringZRPCOut string
  38. // VarBoolIdea describes whether idea or not
  39. VarBoolIdea bool
  40. // VarBoolVerbose describes whether verbose.
  41. VarBoolVerbose bool
  42. // VarBoolMultiple describes whether support generating multiple rpc services or not.
  43. VarBoolMultiple bool
  44. // VarBoolClient describes whether to generate rpc client
  45. VarBoolClient bool
  46. )
  47. // RPCNew is to generate rpc greet service, this greet service can speed
  48. // up your understanding of the zrpc service structure
  49. func RPCNew(_ *cobra.Command, args []string) error {
  50. rpcname := args[0]
  51. ext := filepath.Ext(rpcname)
  52. if len(ext) > 0 {
  53. return fmt.Errorf("unexpected ext: %s", ext)
  54. }
  55. style := VarStringStyle
  56. home := VarStringHome
  57. remote := VarStringRemote
  58. branch := VarStringBranch
  59. verbose := VarBoolVerbose
  60. if len(remote) > 0 {
  61. repo, _ := util.CloneIntoGitHome(remote, branch)
  62. if len(repo) > 0 {
  63. home = repo
  64. }
  65. }
  66. if len(home) > 0 {
  67. pathx.RegisterGoctlHome(home)
  68. }
  69. protoName := rpcname + ".proto"
  70. filename := filepath.Join(".", rpcname, protoName)
  71. src, err := filepath.Abs(filename)
  72. if err != nil {
  73. return err
  74. }
  75. err = generator.ProtoTmpl(src)
  76. if err != nil {
  77. return err
  78. }
  79. var ctx generator.ZRpcContext
  80. ctx.Src = src
  81. ctx.GoOutput = filepath.Dir(src)
  82. ctx.GrpcOutput = filepath.Dir(src)
  83. ctx.IsGooglePlugin = true
  84. ctx.Output = filepath.Dir(src)
  85. ctx.ProtocCmd = fmt.Sprintf("protoc -I=%s %s --go_out=%s --go-grpc_out=%s", filepath.Dir(src), filepath.Base(src), filepath.Dir(src), filepath.Dir(src))
  86. ctx.IsGenClient = VarBoolClient
  87. grpcOptList := VarStringSliceGoGRPCOpt
  88. if len(grpcOptList) > 0 {
  89. ctx.ProtocCmd += " --go-grpc_opt=" + strings.Join(grpcOptList, ",")
  90. }
  91. goOptList := VarStringSliceGoOpt
  92. if len(goOptList) > 0 {
  93. ctx.ProtocCmd += " --go_opt=" + strings.Join(goOptList, ",")
  94. }
  95. g := generator.NewGenerator(style, verbose)
  96. return g.Generate(&ctx)
  97. }
  98. // RPCTemplate is the entry for generate rpc template
  99. func RPCTemplate(latest bool) error {
  100. if !latest {
  101. console.Warning("deprecated: goctl rpc template -o is deprecated and will be removed in the future, use goctl rpc -o instead")
  102. }
  103. protoFile := VarStringOutput
  104. home := VarStringHome
  105. remote := VarStringRemote
  106. branch := VarStringBranch
  107. if len(remote) > 0 {
  108. repo, _ := util.CloneIntoGitHome(remote, branch)
  109. if len(repo) > 0 {
  110. home = repo
  111. }
  112. }
  113. if len(home) > 0 {
  114. pathx.RegisterGoctlHome(home)
  115. }
  116. if len(protoFile) == 0 {
  117. return errors.New("missing -o")
  118. }
  119. return generator.ProtoTmpl(protoFile)
  120. }