goctl.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "runtime"
  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/ktgen"
  14. "github.com/tal-tech/go-zero/tools/goctl/api/new"
  15. "github.com/tal-tech/go-zero/tools/goctl/api/tsgen"
  16. "github.com/tal-tech/go-zero/tools/goctl/api/validate"
  17. "github.com/tal-tech/go-zero/tools/goctl/configgen"
  18. "github.com/tal-tech/go-zero/tools/goctl/docker"
  19. model "github.com/tal-tech/go-zero/tools/goctl/model/sql/command"
  20. rpc "github.com/tal-tech/go-zero/tools/goctl/rpc/cli"
  21. "github.com/tal-tech/go-zero/tools/goctl/tpl"
  22. "github.com/urfave/cli"
  23. )
  24. var (
  25. BuildVersion = "20201125"
  26. commands = []cli.Command{
  27. {
  28. Name: "api",
  29. Usage: "generate api related files",
  30. Flags: []cli.Flag{
  31. cli.StringFlag{
  32. Name: "o",
  33. Usage: "the output api file",
  34. },
  35. },
  36. Action: apigen.ApiCommand,
  37. Subcommands: []cli.Command{
  38. {
  39. Name: "new",
  40. Usage: "fast create api service",
  41. Action: new.NewService,
  42. },
  43. {
  44. Name: "format",
  45. Usage: "format api files",
  46. Flags: []cli.Flag{
  47. cli.StringFlag{
  48. Name: "dir",
  49. Usage: "the format target dir",
  50. },
  51. cli.BoolFlag{
  52. Name: "iu",
  53. Usage: "ignore update",
  54. Required: false,
  55. },
  56. cli.BoolFlag{
  57. Name: "stdin",
  58. Usage: "use stdin to input api doc content, press \"ctrl + d\" to send EOF",
  59. Required: false,
  60. },
  61. },
  62. Action: format.GoFormatApi,
  63. },
  64. {
  65. Name: "validate",
  66. Usage: "validate api file",
  67. Flags: []cli.Flag{
  68. cli.StringFlag{
  69. Name: "api",
  70. Usage: "validate target api file",
  71. },
  72. },
  73. Action: validate.GoValidateApi,
  74. },
  75. {
  76. Name: "doc",
  77. Usage: "generate doc files",
  78. Flags: []cli.Flag{
  79. cli.StringFlag{
  80. Name: "dir",
  81. Usage: "the target dir",
  82. },
  83. },
  84. Action: docgen.DocCommand,
  85. },
  86. {
  87. Name: "go",
  88. Usage: "generate go files for provided api in yaml file",
  89. Flags: []cli.Flag{
  90. cli.StringFlag{
  91. Name: "dir",
  92. Usage: "the target dir",
  93. },
  94. cli.StringFlag{
  95. Name: "api",
  96. Usage: "the api file",
  97. },
  98. cli.StringFlag{
  99. Name: "style",
  100. Required: false,
  101. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  102. },
  103. },
  104. Action: gogen.GoCommand,
  105. },
  106. {
  107. Name: "java",
  108. Usage: "generate java 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. },
  119. Action: javagen.JavaCommand,
  120. },
  121. {
  122. Name: "ts",
  123. Usage: "generate ts files for provided api in api file",
  124. Flags: []cli.Flag{
  125. cli.StringFlag{
  126. Name: "dir",
  127. Usage: "the target dir",
  128. },
  129. cli.StringFlag{
  130. Name: "api",
  131. Usage: "the api file",
  132. },
  133. cli.StringFlag{
  134. Name: "webapi",
  135. Usage: "the web api file path",
  136. Required: false,
  137. },
  138. cli.StringFlag{
  139. Name: "caller",
  140. Usage: "the web api caller",
  141. Required: false,
  142. },
  143. cli.BoolFlag{
  144. Name: "unwrap",
  145. Usage: "unwrap the webapi caller for import",
  146. Required: false,
  147. },
  148. },
  149. Action: tsgen.TsCommand,
  150. },
  151. {
  152. Name: "dart",
  153. Usage: "generate dart files for provided api in api file",
  154. Flags: []cli.Flag{
  155. cli.StringFlag{
  156. Name: "dir",
  157. Usage: "the target dir",
  158. },
  159. cli.StringFlag{
  160. Name: "api",
  161. Usage: "the api file",
  162. },
  163. },
  164. Action: dartgen.DartCommand,
  165. },
  166. {
  167. Name: "kt",
  168. Usage: "generate kotlin code for provided api file",
  169. Flags: []cli.Flag{
  170. cli.StringFlag{
  171. Name: "dir",
  172. Usage: "the target directory",
  173. },
  174. cli.StringFlag{
  175. Name: "api",
  176. Usage: "the api file",
  177. },
  178. cli.StringFlag{
  179. Name: "pkg",
  180. Usage: "define package name for kotlin file",
  181. },
  182. },
  183. Action: ktgen.KtCommand,
  184. },
  185. },
  186. },
  187. {
  188. Name: "docker",
  189. Usage: "generate Dockerfile",
  190. Flags: []cli.Flag{
  191. cli.StringFlag{
  192. Name: "go",
  193. Usage: "the file that contains main function",
  194. },
  195. },
  196. Action: docker.DockerCommand,
  197. },
  198. {
  199. Name: "rpc",
  200. Usage: "generate rpc code",
  201. Subcommands: []cli.Command{
  202. {
  203. Name: "new",
  204. Usage: `generate rpc demo service`,
  205. Flags: []cli.Flag{
  206. cli.StringFlag{
  207. Name: "style",
  208. Required: false,
  209. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  210. },
  211. cli.BoolFlag{
  212. Name: "idea",
  213. Usage: "whether the command execution environment is from idea plugin. [optional]",
  214. },
  215. },
  216. Action: rpc.RpcNew,
  217. },
  218. {
  219. Name: "template",
  220. Usage: `generate proto template`,
  221. Flags: []cli.Flag{
  222. cli.StringFlag{
  223. Name: "out, o",
  224. Usage: "the target path of proto",
  225. },
  226. },
  227. Action: rpc.RpcTemplate,
  228. },
  229. {
  230. Name: "proto",
  231. Usage: `generate rpc from proto`,
  232. Flags: []cli.Flag{
  233. cli.StringFlag{
  234. Name: "src, s",
  235. Usage: "the file path of the proto source file",
  236. },
  237. cli.StringSliceFlag{
  238. Name: "proto_path, I",
  239. Usage: `native command of protoc, specify the directory in which to search for imports. [optional]`,
  240. },
  241. cli.StringFlag{
  242. Name: "dir, d",
  243. Usage: `the target path of the code`,
  244. },
  245. cli.StringFlag{
  246. Name: "style",
  247. Required: false,
  248. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  249. },
  250. cli.BoolFlag{
  251. Name: "idea",
  252. Usage: "whether the command execution environment is from idea plugin. [optional]",
  253. },
  254. },
  255. Action: rpc.Rpc,
  256. },
  257. },
  258. },
  259. {
  260. Name: "model",
  261. Usage: "generate model code",
  262. Subcommands: []cli.Command{
  263. {
  264. Name: "mysql",
  265. Usage: `generate mysql model`,
  266. Subcommands: []cli.Command{
  267. {
  268. Name: "ddl",
  269. Usage: `generate mysql model from ddl`,
  270. Flags: []cli.Flag{
  271. cli.StringFlag{
  272. Name: "src, s",
  273. Usage: "the path or path globbing patterns of the ddl",
  274. },
  275. cli.StringFlag{
  276. Name: "dir, d",
  277. Usage: "the target dir",
  278. },
  279. cli.StringFlag{
  280. Name: "style",
  281. Required: false,
  282. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  283. },
  284. cli.BoolFlag{
  285. Name: "cache, c",
  286. Usage: "generate code with cache [optional]",
  287. },
  288. cli.BoolFlag{
  289. Name: "idea",
  290. Usage: "for idea plugin [optional]",
  291. },
  292. },
  293. Action: model.MysqlDDL,
  294. },
  295. {
  296. Name: "datasource",
  297. Usage: `generate model from datasource`,
  298. Flags: []cli.Flag{
  299. cli.StringFlag{
  300. Name: "url",
  301. Usage: `the data source of database,like "root:password@tcp(127.0.0.1:3306)/database`,
  302. },
  303. cli.StringFlag{
  304. Name: "table, t",
  305. Usage: `the table or table globbing patterns in the database`,
  306. },
  307. cli.BoolFlag{
  308. Name: "cache, c",
  309. Usage: "generate code with cache [optional]",
  310. },
  311. cli.StringFlag{
  312. Name: "dir, d",
  313. Usage: "the target dir",
  314. },
  315. cli.StringFlag{
  316. Name: "style",
  317. Required: false,
  318. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  319. },
  320. cli.BoolFlag{
  321. Name: "idea",
  322. Usage: "for idea plugin [optional]",
  323. },
  324. },
  325. Action: model.MyDataSource,
  326. },
  327. },
  328. },
  329. },
  330. },
  331. {
  332. Name: "config",
  333. Usage: "generate config json",
  334. Flags: []cli.Flag{
  335. cli.StringFlag{
  336. Name: "path, p",
  337. Usage: "the target config go file",
  338. },
  339. },
  340. Action: configgen.GenConfigCommand,
  341. },
  342. {
  343. Name: "template",
  344. Usage: "template operation",
  345. Subcommands: []cli.Command{
  346. {
  347. Name: "init",
  348. Usage: "initialize the all templates(force update)",
  349. Action: tpl.GenTemplates,
  350. },
  351. {
  352. Name: "clean",
  353. Usage: "clean the all cache templates",
  354. Action: tpl.CleanTemplates,
  355. },
  356. {
  357. Name: "update",
  358. Usage: "update template of the target category to the latest",
  359. Flags: []cli.Flag{
  360. cli.StringFlag{
  361. Name: "category,c",
  362. Usage: "the category of template, enum [api,rpc,model]",
  363. },
  364. },
  365. Action: tpl.UpdateTemplates,
  366. },
  367. {
  368. Name: "revert",
  369. Usage: "revert the target template to the latest",
  370. Flags: []cli.Flag{
  371. cli.StringFlag{
  372. Name: "category,c",
  373. Usage: "the category of template, enum [api,rpc,model]",
  374. },
  375. cli.StringFlag{
  376. Name: "name,n",
  377. Usage: "the target file name of template",
  378. },
  379. },
  380. Action: tpl.RevertTemplates,
  381. },
  382. },
  383. },
  384. }
  385. )
  386. func main() {
  387. logx.Disable()
  388. app := cli.NewApp()
  389. app.Usage = "a cli tool to generate code"
  390. app.Version = fmt.Sprintf("%s %s/%s", BuildVersion, runtime.GOOS, runtime.GOARCH)
  391. app.Commands = commands
  392. // cli already print error messages
  393. if err := app.Run(os.Args); err != nil {
  394. fmt.Println("error:", err)
  395. }
  396. }