cmd.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package rpc
  2. import (
  3. "github.com/spf13/cobra"
  4. "github.com/zeromicro/go-zero/tools/goctl/config"
  5. "github.com/zeromicro/go-zero/tools/goctl/rpc/cli"
  6. )
  7. var (
  8. // Cmd describes a rpc command.
  9. Cmd = &cobra.Command{
  10. Use: "rpc",
  11. Short: "Generate rpc code",
  12. RunE: func(cmd *cobra.Command, args []string) error {
  13. return cli.RPCTemplate(true)
  14. },
  15. }
  16. newCmd = &cobra.Command{
  17. Use: "new",
  18. Short: "Generate rpc demo service",
  19. Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
  20. RunE: cli.RPCNew,
  21. }
  22. templateCmd = &cobra.Command{
  23. Use: "template",
  24. Short: "Generate proto template",
  25. RunE: func(cmd *cobra.Command, args []string) error {
  26. return cli.RPCTemplate(false)
  27. },
  28. }
  29. protocCmd = &cobra.Command{
  30. Use: "protoc",
  31. Short: "Generate grpc code",
  32. Example: "goctl rpc protoc xx.proto --go_out=./pb --go-grpc_out=./pb --zrpc_out=.",
  33. Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
  34. RunE: cli.ZRPC,
  35. }
  36. )
  37. func init() {
  38. Cmd.Flags().StringVar(&cli.VarStringOutput, "o", "", "Output a sample proto file")
  39. Cmd.Flags().StringVar(&cli.VarStringHome, "home", "", "The goctl home path of "+
  40. "the template, --home and --remote cannot be set at the same time, if they are, --remote has"+
  41. " higher priority")
  42. Cmd.Flags().StringVar(&cli.VarStringRemote, "remote", "", "The remote git repo"+
  43. " of the template, --home and --remote cannot be set at the same time, if they are, --remote"+
  44. " has higher priority\n\tThe git repo directory must be consistent with the "+
  45. "https://github.com/zeromicro/go-zero-template directory structure")
  46. Cmd.Flags().StringVar(&cli.VarStringBranch, "branch", "", "The branch of the "+
  47. "remote repo, it does work with --remote")
  48. newCmd.Flags().StringSliceVar(&cli.VarStringSliceGoOpt, "go_opt", nil, "")
  49. newCmd.Flags().StringSliceVar(&cli.VarStringSliceGoGRPCOpt, "go-grpc_opt", nil, "")
  50. newCmd.Flags().StringVar(&cli.VarStringStyle, "style", config.DefaultFormat, "The file "+
  51. "naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]")
  52. newCmd.Flags().BoolVar(&cli.VarBoolIdea, "idea", false, "Whether the command "+
  53. "execution environment is from idea plugin.")
  54. newCmd.Flags().StringVar(&cli.VarStringHome, "home", "", "The goctl home path "+
  55. "of the template, --home and --remote cannot be set at the same time, if they are, --remote "+
  56. "has higher priority")
  57. newCmd.Flags().StringVar(&cli.VarStringRemote, "remote", "", "The remote git "+
  58. "repo of the template, --home and --remote cannot be set at the same time, if they are, "+
  59. "--remote has higher priority\n\tThe git repo directory must be consistent with the "+
  60. "https://github.com/zeromicro/go-zero-template directory structure")
  61. newCmd.Flags().StringVar(&cli.VarStringBranch, "branch", "",
  62. "The branch of the remote repo, it does work with --remote")
  63. newCmd.Flags().BoolVarP(&cli.VarBoolVerbose, "verbose", "v", false, "Enable log output")
  64. newCmd.Flags().MarkHidden("go_opt")
  65. newCmd.Flags().MarkHidden("go-grpc_opt")
  66. protocCmd.Flags().BoolVarP(&cli.VarBoolMultiple, "multiple", "m", false,
  67. "Generated in multiple rpc service mode")
  68. protocCmd.Flags().StringSliceVar(&cli.VarStringSliceGoOut, "go_out", nil, "")
  69. protocCmd.Flags().StringSliceVar(&cli.VarStringSliceGoGRPCOut, "go-grpc_out", nil, "")
  70. protocCmd.Flags().StringSliceVar(&cli.VarStringSliceGoOpt, "go_opt", nil, "")
  71. protocCmd.Flags().StringSliceVar(&cli.VarStringSliceGoGRPCOpt, "go-grpc_opt", nil, "")
  72. protocCmd.Flags().StringSliceVar(&cli.VarStringSlicePlugin, "plugin", nil, "")
  73. protocCmd.Flags().StringSliceVarP(&cli.VarStringSliceProtoPath, "proto_path", "I", nil, "")
  74. protocCmd.Flags().StringVar(&cli.VarStringZRPCOut, "zrpc_out", "", "The zrpc output directory")
  75. protocCmd.Flags().StringVar(&cli.VarStringStyle, "style", config.DefaultFormat, "The file "+
  76. "naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]")
  77. protocCmd.Flags().StringVar(&cli.VarStringHome, "home", "", "The goctl home "+
  78. "path of the template, --home and --remote cannot be set at the same time, if they are, "+
  79. "--remote has higher priority")
  80. protocCmd.Flags().StringVar(&cli.VarStringRemote, "remote", "", "The remote "+
  81. "git repo of the template, --home and --remote cannot be set at the same time, if they are, "+
  82. "--remote has higher priority\n\tThe git repo directory must be consistent with the "+
  83. "https://github.com/zeromicro/go-zero-template directory structure")
  84. protocCmd.Flags().StringVar(&cli.VarStringBranch, "branch", "",
  85. "The branch of the remote repo, it does work with --remote")
  86. protocCmd.Flags().BoolVarP(&cli.VarBoolVerbose, "verbose", "v", false, "Enable log output")
  87. protocCmd.Flags().MarkHidden("go_out")
  88. protocCmd.Flags().MarkHidden("go-grpc_out")
  89. protocCmd.Flags().MarkHidden("go_opt")
  90. protocCmd.Flags().MarkHidden("go-grpc_opt")
  91. protocCmd.Flags().MarkHidden("plugin")
  92. protocCmd.Flags().MarkHidden("proto_path")
  93. templateCmd.Flags().StringVar(&cli.VarStringOutput, "o", "", "Output a sample proto file")
  94. templateCmd.Flags().StringVar(&cli.VarStringHome, "home", "", "The goctl home"+
  95. " path of the template, --home and --remote cannot be set at the same time, if they are, "+
  96. "--remote has higher priority")
  97. templateCmd.Flags().StringVar(&cli.VarStringRemote, "remote", "", "The remote "+
  98. "git repo of the template, --home and --remote cannot be set at the same time, if they are, "+
  99. "--remote has higher priority\n\tThe git repo directory must be consistent with the "+
  100. "https://github.com/zeromicro/go-zero-template directory structure")
  101. templateCmd.Flags().StringVar(&cli.VarStringBranch, "branch", "", "The branch"+
  102. " of the remote repo, it does work with --remote")
  103. Cmd.AddCommand(newCmd)
  104. Cmd.AddCommand(protocCmd)
  105. Cmd.AddCommand(templateCmd)
  106. }