cli.go 3.4 KB

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