goctl.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/tal-tech/go-zero/core/logx"
  6. "github.com/tal-tech/go-zero/tools/goctl/api/apigen"
  7. "github.com/tal-tech/go-zero/tools/goctl/api/dartgen"
  8. "github.com/tal-tech/go-zero/tools/goctl/api/docgen"
  9. "github.com/tal-tech/go-zero/tools/goctl/api/format"
  10. "github.com/tal-tech/go-zero/tools/goctl/api/gogen"
  11. "github.com/tal-tech/go-zero/tools/goctl/api/javagen"
  12. "github.com/tal-tech/go-zero/tools/goctl/api/tsgen"
  13. "github.com/tal-tech/go-zero/tools/goctl/api/validate"
  14. "github.com/tal-tech/go-zero/tools/goctl/configgen"
  15. "github.com/tal-tech/go-zero/tools/goctl/docker"
  16. "github.com/tal-tech/go-zero/tools/goctl/feature"
  17. "github.com/tal-tech/go-zero/tools/goctl/model/mongomodel"
  18. "github.com/urfave/cli"
  19. )
  20. var (
  21. BuildTime = "not set"
  22. commands = []cli.Command{
  23. {
  24. Name: "api",
  25. Usage: "generate api related files",
  26. Flags: []cli.Flag{
  27. cli.StringFlag{
  28. Name: "o",
  29. Usage: "the output api file",
  30. },
  31. },
  32. Action: apigen.ApiCommand,
  33. Subcommands: []cli.Command{
  34. {
  35. Name: "format",
  36. Usage: "format api files",
  37. Flags: []cli.Flag{
  38. cli.StringFlag{
  39. Name: "dir",
  40. Usage: "the format target dir",
  41. },
  42. cli.BoolFlag{
  43. Name: "p",
  44. Usage: "print result to console",
  45. },
  46. cli.BoolFlag{
  47. Name: "iu",
  48. Usage: "ignore update",
  49. Required: false,
  50. },
  51. },
  52. Action: format.GoFormatApi,
  53. },
  54. {
  55. Name: "validate",
  56. Usage: "validate api file",
  57. Flags: []cli.Flag{
  58. cli.StringFlag{
  59. Name: "api",
  60. Usage: "validate target api file",
  61. },
  62. },
  63. Action: validate.GoValidateApi,
  64. },
  65. {
  66. Name: "doc",
  67. Usage: "generate doc files",
  68. Flags: []cli.Flag{
  69. cli.StringFlag{
  70. Name: "dir",
  71. Usage: "the target dir",
  72. },
  73. },
  74. Action: docgen.DocCommand,
  75. },
  76. {
  77. Name: "go",
  78. Usage: "generate go files for provided api in yaml file",
  79. Flags: []cli.Flag{
  80. cli.StringFlag{
  81. Name: "dir",
  82. Usage: "the target dir",
  83. },
  84. cli.StringFlag{
  85. Name: "api",
  86. Usage: "the api file",
  87. },
  88. },
  89. Action: gogen.GoCommand,
  90. },
  91. {
  92. Name: "java",
  93. Usage: "generate java files for provided api in api file",
  94. Flags: []cli.Flag{
  95. cli.StringFlag{
  96. Name: "dir",
  97. Usage: "the target dir",
  98. },
  99. cli.StringFlag{
  100. Name: "api",
  101. Usage: "the api file",
  102. },
  103. },
  104. Action: javagen.JavaCommand,
  105. },
  106. {
  107. Name: "ts",
  108. Usage: "generate ts files for provided api in api file",
  109. Flags: []cli.Flag{
  110. cli.StringFlag{
  111. Name: "dir",
  112. Usage: "the target dir",
  113. },
  114. cli.StringFlag{
  115. Name: "api",
  116. Usage: "the api file",
  117. },
  118. cli.StringFlag{
  119. Name: "webapi",
  120. Usage: "the web api file path",
  121. Required: false,
  122. },
  123. cli.StringFlag{
  124. Name: "caller",
  125. Usage: "the web api caller",
  126. Required: false,
  127. },
  128. cli.BoolFlag{
  129. Name: "unwrap",
  130. Usage: "unwrap the webapi caller for import",
  131. Required: false,
  132. },
  133. },
  134. Action: tsgen.TsCommand,
  135. },
  136. {
  137. Name: "dart",
  138. Usage: "generate dart files for provided api in api file",
  139. Flags: []cli.Flag{
  140. cli.StringFlag{
  141. Name: "dir",
  142. Usage: "the target dir",
  143. },
  144. cli.StringFlag{
  145. Name: "api",
  146. Usage: "the api file",
  147. },
  148. },
  149. Action: dartgen.DartCommand,
  150. },
  151. },
  152. },
  153. {
  154. Name: "docker",
  155. Usage: "generate Dockerfile and Makefile",
  156. Flags: []cli.Flag{
  157. cli.StringFlag{
  158. Name: "go",
  159. Usage: "the file that contains main function",
  160. },
  161. cli.StringFlag{
  162. Name: "namespace, n",
  163. Usage: "which namespace of kubernetes to deploy the service",
  164. },
  165. },
  166. Action: docker.DockerCommand,
  167. },
  168. {
  169. Name: "model",
  170. Usage: "generate sql model",
  171. Flags: []cli.Flag{
  172. cli.StringFlag{
  173. Name: "config, c",
  174. Usage: "the file that contains main function",
  175. },
  176. cli.StringFlag{
  177. Name: "dir, d",
  178. Usage: "the target dir",
  179. },
  180. },
  181. Subcommands: []cli.Command{
  182. {
  183. Name: "mongo",
  184. Usage: "generate mongoModel files for provided somemongo.go in go file",
  185. Flags: []cli.Flag{
  186. cli.StringFlag{
  187. Name: "src, s",
  188. Usage: "the src file",
  189. },
  190. cli.StringFlag{
  191. Name: "cache",
  192. Usage: "need cache code([yes/no] default value is no)",
  193. },
  194. },
  195. Action: mongomodel.ModelCommond,
  196. },
  197. },
  198. },
  199. {
  200. Name: "config",
  201. Usage: "generate config json",
  202. Flags: []cli.Flag{
  203. cli.StringFlag{
  204. Name: "path, p",
  205. Usage: "the target config go file",
  206. },
  207. },
  208. Action: configgen.GenConfigCommand,
  209. },
  210. {
  211. Name: "feature",
  212. Usage: "the features of the latest version",
  213. Action: feature.Feature,
  214. },
  215. }
  216. )
  217. func main() {
  218. logx.Disable()
  219. app := cli.NewApp()
  220. app.Usage = "a cli tool to generate code"
  221. app.Version = BuildTime
  222. app.Commands = commands
  223. // cli already print error messages
  224. if err := app.Run(os.Args); err != nil {
  225. fmt.Println("error:", err)
  226. }
  227. }