cli.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package cli
  2. import (
  3. "errors"
  4. "fmt"
  5. "path/filepath"
  6. "strings"
  7. "github.com/spf13/cobra"
  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/console"
  11. "github.com/zeromicro/go-zero/tools/goctl/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. )
  45. // RPCNew is to generate rpc greet service, this greet service can speed
  46. // up your understanding of the zrpc service structure
  47. func RPCNew(_ *cobra.Command, args []string) error {
  48. rpcname := args[0]
  49. ext := filepath.Ext(rpcname)
  50. if len(ext) > 0 {
  51. return fmt.Errorf("unexpected ext: %s", ext)
  52. }
  53. style := VarStringStyle
  54. home := VarStringHome
  55. remote := VarStringRemote
  56. branch := VarStringBranch
  57. verbose := VarBoolVerbose
  58. if len(remote) > 0 {
  59. repo, _ := util.CloneIntoGitHome(remote, branch)
  60. if len(repo) > 0 {
  61. home = repo
  62. }
  63. }
  64. if len(home) > 0 {
  65. pathx.RegisterGoctlHome(home)
  66. }
  67. protoName := rpcname + ".proto"
  68. filename := filepath.Join(".", rpcname, protoName)
  69. src, err := filepath.Abs(filename)
  70. if err != nil {
  71. return err
  72. }
  73. err = generator.ProtoTmpl(src)
  74. if err != nil {
  75. return err
  76. }
  77. var ctx generator.ZRpcContext
  78. ctx.Src = src
  79. ctx.GoOutput = filepath.Dir(src)
  80. ctx.GrpcOutput = filepath.Dir(src)
  81. ctx.IsGooglePlugin = true
  82. ctx.Output = filepath.Dir(src)
  83. 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))
  84. grpcOptList := VarStringSliceGoGRPCOpt
  85. if len(grpcOptList) > 0 {
  86. ctx.ProtocCmd += " --go-grpc_opt=" + strings.Join(grpcOptList, ",")
  87. }
  88. goOptList := VarStringSliceGoOpt
  89. if len(goOptList) > 0 {
  90. ctx.ProtocCmd += " --go_opt=" + strings.Join(goOptList, ",")
  91. }
  92. g := generator.NewGenerator(style, verbose)
  93. return g.Generate(&ctx)
  94. }
  95. // RPCTemplate is the entry for generate rpc template
  96. func RPCTemplate(latest bool) error {
  97. if !latest {
  98. console.Warning("deprecated: goctl rpc template -o is deprecated and will be removed in the future, use goctl rpc -o instead")
  99. }
  100. protoFile := VarStringOutput
  101. home := VarStringHome
  102. remote := VarStringRemote
  103. branch := VarStringBranch
  104. if len(remote) > 0 {
  105. repo, _ := util.CloneIntoGitHome(remote, branch)
  106. if len(repo) > 0 {
  107. home = repo
  108. }
  109. }
  110. if len(home) > 0 {
  111. pathx.RegisterGoctlHome(home)
  112. }
  113. if len(protoFile) == 0 {
  114. return errors.New("missing -o")
  115. }
  116. return generator.ProtoTmpl(protoFile)
  117. }