cmd.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package api
  2. import (
  3. "github.com/spf13/cobra"
  4. "github.com/zeromicro/go-zero/tools/goctl/api/apigen"
  5. "github.com/zeromicro/go-zero/tools/goctl/api/dartgen"
  6. "github.com/zeromicro/go-zero/tools/goctl/api/docgen"
  7. "github.com/zeromicro/go-zero/tools/goctl/api/format"
  8. "github.com/zeromicro/go-zero/tools/goctl/api/gogen"
  9. "github.com/zeromicro/go-zero/tools/goctl/api/javagen"
  10. "github.com/zeromicro/go-zero/tools/goctl/api/ktgen"
  11. "github.com/zeromicro/go-zero/tools/goctl/api/new"
  12. "github.com/zeromicro/go-zero/tools/goctl/api/tsgen"
  13. "github.com/zeromicro/go-zero/tools/goctl/api/validate"
  14. "github.com/zeromicro/go-zero/tools/goctl/plugin"
  15. )
  16. var (
  17. // Cmd describes a api command.
  18. Cmd = &cobra.Command{
  19. Use: "api",
  20. Short: "Generate api related files",
  21. RunE: apigen.CreateApiTemplate,
  22. }
  23. dartCmd = &cobra.Command{
  24. Use: "dart",
  25. Short: "Generate dart files for provided api in api file",
  26. RunE: dartgen.DartCommand,
  27. }
  28. docCmd = &cobra.Command{
  29. Use: "doc",
  30. Short: "Generate doc files",
  31. RunE: docgen.DocCommand,
  32. }
  33. formatCmd = &cobra.Command{
  34. Use: "format",
  35. Short: "Format api files",
  36. RunE: format.GoFormatApi,
  37. }
  38. goCmd = &cobra.Command{
  39. Use: "go",
  40. Short: "Generate go files for provided api in yaml file",
  41. RunE: gogen.GoCommand,
  42. }
  43. newCmd = &cobra.Command{
  44. Use: "new",
  45. Short: "Fast create api service",
  46. Example: "goctl api new [options] service-name",
  47. Args: cobra.ExactValidArgs(1),
  48. RunE: func(cmd *cobra.Command, args []string) error {
  49. return new.CreateServiceCommand(args)
  50. },
  51. }
  52. validateCmd = &cobra.Command{
  53. Use: "validate",
  54. Short: "Validate api file",
  55. RunE: validate.GoValidateApi,
  56. }
  57. javaCmd = &cobra.Command{
  58. Use: "java",
  59. Short: "Generate java files for provided api in api file",
  60. RunE: javagen.JavaCommand,
  61. }
  62. ktCmd = &cobra.Command{
  63. Use: "kt",
  64. Short: "Generate kotlin code for provided api file",
  65. RunE: ktgen.KtCommand,
  66. }
  67. pluginCmd = &cobra.Command{
  68. Use: "plugin",
  69. Short: "Custom file generator",
  70. RunE: plugin.PluginCommand,
  71. }
  72. tsCmd = &cobra.Command{
  73. Use: "ts",
  74. Short: "Generate ts files for provided api in api file",
  75. RunE: tsgen.TsCommand,
  76. }
  77. )
  78. func init() {
  79. Cmd.Flags().StringVar(&apigen.VarStringOutput, "o", "", "Output a sample api file")
  80. Cmd.Flags().StringVar(&apigen.VarStringHome, "home", "", "The goctl home path of the"+
  81. " template, --home and --remote cannot be set at the same time, if they are, --remote has "+
  82. "higher priority")
  83. Cmd.Flags().StringVar(&apigen.VarStringRemote, "remote", "", "The remote git repo of the"+
  84. " template, --home and --remote cannot be set at the same time, if they are, --remote has higher"+
  85. " priority\n\tThe git repo directory must be consistent with the"+
  86. " https://github.com/zeromicro/go-zero-template directory structure")
  87. Cmd.Flags().StringVar(&apigen.VarStringBranch, "branch", "", "The branch of the "+
  88. "remote repo, it does work with --remote")
  89. dartCmd.Flags().StringVar(&dartgen.VarStringDir, "dir", "", "The target dir")
  90. dartCmd.Flags().StringVar(&dartgen.VarStringAPI, "api", "", "The api file")
  91. dartCmd.Flags().BoolVar(&dartgen.VarStringLegacy, "legacy", false, "Legacy generator for flutter v1")
  92. dartCmd.Flags().StringVar(&dartgen.VarStringHostname, "hostname", "", "hostname of the server")
  93. docCmd.Flags().StringVar(&docgen.VarStringDir, "dir", "", "The target dir")
  94. docCmd.Flags().StringVar(&docgen.VarStringOutput, "o", "", "The output markdown directory")
  95. formatCmd.Flags().StringVar(&format.VarStringDir, "dir", "", "The format target dir")
  96. formatCmd.Flags().BoolVar(&format.VarBoolIgnore, "iu", false, "Ignore update")
  97. formatCmd.Flags().BoolVar(&format.VarBoolUseStdin, "stdin", false, "Use stdin to input api"+
  98. " doc content, press \"ctrl + d\" to send EOF")
  99. formatCmd.Flags().BoolVar(&format.VarBoolSkipCheckDeclare, "declare", false, "Use to skip check "+
  100. "api types already declare")
  101. goCmd.Flags().StringVar(&gogen.VarStringDir, "dir", "", "The target dir")
  102. goCmd.Flags().StringVar(&gogen.VarStringAPI, "api", "", "The api file")
  103. goCmd.Flags().StringVar(&gogen.VarStringHome, "home", "", "The goctl home path of "+
  104. "the template, --home and --remote cannot be set at the same time, if they are, --remote "+
  105. "has higher priority")
  106. goCmd.Flags().StringVar(&gogen.VarStringRemote, "remote", "", "The remote git repo "+
  107. "of the template, --home and --remote cannot be set at the same time, if they are, --remote"+
  108. " has higher priority\n\tThe git repo directory must be consistent with the "+
  109. "https://github.com/zeromicro/go-zero-template directory structure")
  110. goCmd.Flags().StringVar(&gogen.VarStringBranch, "branch", "", "The branch of "+
  111. "the remote repo, it does work with --remote")
  112. goCmd.Flags().StringVar(&gogen.VarStringStyle, "style", "gozero", "The file naming format,"+
  113. " see [https://github.com/zeromicro/go-zero/blob/master/tools/goctl/config/readme.md]")
  114. javaCmd.Flags().StringVar(&javagen.VarStringDir, "dir", "", "The target dir")
  115. javaCmd.Flags().StringVar(&javagen.VarStringAPI, "api", "", "The api file")
  116. ktCmd.Flags().StringVar(&ktgen.VarStringDir, "dir", "", "The target dir")
  117. ktCmd.Flags().StringVar(&ktgen.VarStringAPI, "api", "", "The api file")
  118. ktCmd.Flags().StringVar(&ktgen.VarStringPKG, "pkg", "", "Define package name for kotlin file")
  119. newCmd.Flags().StringVar(&new.VarStringHome, "home", "", "The goctl home path of "+
  120. "the template, --home and --remote cannot be set at the same time, if they are, --remote "+
  121. "has higher priority")
  122. newCmd.Flags().StringVar(&new.VarStringRemote, "remote", "", "The remote git repo "+
  123. "of the template, --home and --remote cannot be set at the same time, if they are, --remote"+
  124. " has higher priority\n\tThe git repo directory must be consistent with the "+
  125. "https://github.com/zeromicro/go-zero-template directory structure")
  126. newCmd.Flags().StringVar(&new.VarStringBranch, "branch", "", "The branch of "+
  127. "the remote repo, it does work with --remote")
  128. newCmd.Flags().StringVar(&new.VarStringStyle, "style", "gozero", "The file naming format,"+
  129. " see [https://github.com/zeromicro/go-zero/blob/master/tools/goctl/config/readme.md]")
  130. pluginCmd.Flags().StringVarP(&plugin.VarStringPlugin, "plugin", "p", "", "The plugin file")
  131. pluginCmd.Flags().StringVar(&plugin.VarStringDir, "dir", "", "The target dir")
  132. pluginCmd.Flags().StringVar(&plugin.VarStringAPI, "api", "", "The api file")
  133. pluginCmd.Flags().StringVar(&plugin.VarStringStyle, "style", "",
  134. "The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]")
  135. tsCmd.Flags().StringVar(&tsgen.VarStringDir, "dir", "", "The target dir")
  136. tsCmd.Flags().StringVar(&tsgen.VarStringAPI, "api", "", "The api file")
  137. tsCmd.Flags().StringVar(&tsgen.VarStringWebAPI, "webapi", "", "The web api file path")
  138. tsCmd.Flags().StringVar(&tsgen.VarStringCaller, "caller", "", "The web api caller")
  139. tsCmd.Flags().BoolVar(&tsgen.VarBoolUnWrap, "unwrap", false, "Unwrap the webapi caller for import")
  140. validateCmd.Flags().StringVar(&validate.VarStringAPI, "api", "", "Validate target api file")
  141. // Add sub-commands
  142. Cmd.AddCommand(dartCmd)
  143. Cmd.AddCommand(docCmd)
  144. Cmd.AddCommand(formatCmd)
  145. Cmd.AddCommand(goCmd)
  146. Cmd.AddCommand(javaCmd)
  147. Cmd.AddCommand(ktCmd)
  148. Cmd.AddCommand(newCmd)
  149. Cmd.AddCommand(pluginCmd)
  150. Cmd.AddCommand(tsCmd)
  151. Cmd.AddCommand(validateCmd)
  152. }