goctl.go 5.4 KB

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