cmd.go 7.3 KB

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