cmd.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. dartCmd.Flags().StringVar(&dartgen.VarStringScheme, "scheme", "", "scheme of the server")
  96. docCmd.Flags().StringVar(&docgen.VarStringDir, "dir", "", "The target dir")
  97. docCmd.Flags().StringVar(&docgen.VarStringOutput, "o", "", "The output markdown directory")
  98. formatCmd.Flags().StringVar(&format.VarStringDir, "dir", "", "The format target dir")
  99. formatCmd.Flags().BoolVar(&format.VarBoolIgnore, "iu", false, "Ignore update")
  100. formatCmd.Flags().BoolVar(&format.VarBoolUseStdin, "stdin", false, "Use stdin to input api"+
  101. " doc content, press \"ctrl + d\" to send EOF")
  102. formatCmd.Flags().BoolVar(&format.VarBoolSkipCheckDeclare, "declare", false, "Use to skip check "+
  103. "api types already declare")
  104. goCmd.Flags().StringVar(&gogen.VarStringDir, "dir", "", "The target dir")
  105. goCmd.Flags().StringVar(&gogen.VarStringAPI, "api", "", "The api file")
  106. goCmd.Flags().StringVar(&gogen.VarStringHome, "home", "", "The goctl home path of "+
  107. "the template, --home and --remote cannot be set at the same time, if they are, --remote "+
  108. "has higher priority")
  109. goCmd.Flags().StringVar(&gogen.VarStringRemote, "remote", "", "The remote git repo "+
  110. "of the template, --home and --remote cannot be set at the same time, if they are, --remote"+
  111. " has higher priority\nThe git repo directory must be consistent with the "+
  112. "https://github.com/zeromicro/go-zero-template directory structure")
  113. goCmd.Flags().StringVar(&gogen.VarStringBranch, "branch", "", "The branch of "+
  114. "the remote repo, it does work with --remote")
  115. goCmd.Flags().StringVar(&gogen.VarStringStyle, "style", config.DefaultFormat, "The file naming format,"+
  116. " see [https://github.com/zeromicro/go-zero/blob/master/tools/goctl/config/readme.md]")
  117. javaCmd.Flags().StringVar(&javagen.VarStringDir, "dir", "", "The target dir")
  118. javaCmd.Flags().StringVar(&javagen.VarStringAPI, "api", "", "The api file")
  119. ktCmd.Flags().StringVar(&ktgen.VarStringDir, "dir", "", "The target dir")
  120. ktCmd.Flags().StringVar(&ktgen.VarStringAPI, "api", "", "The api file")
  121. ktCmd.Flags().StringVar(&ktgen.VarStringPKG, "pkg", "", "Define package name for kotlin file")
  122. newCmd.Flags().StringVar(&new.VarStringHome, "home", "", "The goctl home path of "+
  123. "the template, --home and --remote cannot be set at the same time, if they are, --remote "+
  124. "has higher priority")
  125. newCmd.Flags().StringVar(&new.VarStringRemote, "remote", "", "The remote git repo "+
  126. "of the template, --home and --remote cannot be set at the same time, if they are, --remote"+
  127. " has higher priority\n\tThe git repo directory must be consistent with the "+
  128. "https://github.com/zeromicro/go-zero-template directory structure")
  129. newCmd.Flags().StringVar(&new.VarStringBranch, "branch", "", "The branch of "+
  130. "the remote repo, it does work with --remote")
  131. newCmd.Flags().StringVar(&new.VarStringStyle, "style", config.DefaultFormat, "The file naming format,"+
  132. " see [https://github.com/zeromicro/go-zero/blob/master/tools/goctl/config/readme.md]")
  133. pluginCmd.Flags().StringVarP(&plugin.VarStringPlugin, "plugin", "p", "", "The plugin file")
  134. pluginCmd.Flags().StringVar(&plugin.VarStringDir, "dir", "", "The target dir")
  135. pluginCmd.Flags().StringVar(&plugin.VarStringAPI, "api", "", "The api file")
  136. pluginCmd.Flags().StringVar(&plugin.VarStringStyle, "style", "",
  137. "The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]")
  138. tsCmd.Flags().StringVar(&tsgen.VarStringDir, "dir", "", "The target dir")
  139. tsCmd.Flags().StringVar(&tsgen.VarStringAPI, "api", "", "The api file")
  140. tsCmd.Flags().StringVar(&tsgen.VarStringCaller, "caller", "", "The web api caller")
  141. tsCmd.Flags().BoolVar(&tsgen.VarBoolUnWrap, "unwrap", false, "Unwrap the webapi caller for import")
  142. validateCmd.Flags().StringVar(&validate.VarStringAPI, "api", "", "Validate target api file")
  143. // Add sub-commands
  144. Cmd.AddCommand(dartCmd)
  145. Cmd.AddCommand(docCmd)
  146. Cmd.AddCommand(formatCmd)
  147. Cmd.AddCommand(goCmd)
  148. Cmd.AddCommand(javaCmd)
  149. Cmd.AddCommand(ktCmd)
  150. Cmd.AddCommand(newCmd)
  151. Cmd.AddCommand(pluginCmd)
  152. Cmd.AddCommand(tsCmd)
  153. Cmd.AddCommand(validateCmd)
  154. }