goctl.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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/ktgen"
  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. Subcommands: []cli.Command{
  192. {
  193. Name: "mysql",
  194. Usage: `generate mysql model"`,
  195. Subcommands: []cli.Command{
  196. {
  197. Name: "ddl",
  198. Usage: `generate mysql model from ddl"`,
  199. Flags: []cli.Flag{
  200. cli.StringFlag{
  201. Name: "src, s",
  202. Usage: "the file path of the ddl source file",
  203. },
  204. cli.StringFlag{
  205. Name: "dir, d",
  206. Usage: "the target dir",
  207. },
  208. cli.BoolFlag{
  209. Name: "cache, c",
  210. Usage: "generate code with cache [optional]",
  211. },
  212. cli.BoolFlag{
  213. Name: "idea",
  214. Usage: "for idea plugin [optional]",
  215. },
  216. },
  217. Action: command.MysqlDDL,
  218. },
  219. {
  220. Name: "datasource",
  221. Usage: `generate model from datasource"`,
  222. Flags: []cli.Flag{
  223. cli.StringFlag{
  224. Name: "url",
  225. Usage: `the data source of database,like "root:password@tcp(127.0.0.1:3306)/database"`,
  226. },
  227. cli.StringFlag{
  228. Name: "table, t",
  229. Usage: `source table,tables separated by commas,like "user,course"`,
  230. },
  231. cli.BoolFlag{
  232. Name: "cache, c",
  233. Usage: "generate code with cache [optional]",
  234. },
  235. cli.StringFlag{
  236. Name: "dir, d",
  237. Usage: "the target dir",
  238. },
  239. cli.BoolFlag{
  240. Name: "idea",
  241. Usage: "for idea plugin [optional]",
  242. },
  243. },
  244. Action: command.MyDataSource,
  245. },
  246. },
  247. },
  248. },
  249. },
  250. {
  251. Name: "config",
  252. Usage: "generate config json",
  253. Flags: []cli.Flag{
  254. cli.StringFlag{
  255. Name: "path, p",
  256. Usage: "the target config go file",
  257. },
  258. },
  259. Action: configgen.GenConfigCommand,
  260. },
  261. {
  262. Name: "feature",
  263. Usage: "the features of the latest version",
  264. Action: feature.Feature,
  265. },
  266. }
  267. )
  268. func main() {
  269. logx.Disable()
  270. app := cli.NewApp()
  271. app.Usage = "a cli tool to generate code"
  272. app.Version = BuildTime
  273. app.Commands = commands
  274. // cli already print error messages
  275. if err := app.Run(os.Args); err != nil {
  276. fmt.Println("error:", err)
  277. }
  278. }