goctl.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. "github.com/tal-tech/go-zero/tools/goctl/kube"
  20. model "github.com/tal-tech/go-zero/tools/goctl/model/sql/command"
  21. "github.com/tal-tech/go-zero/tools/goctl/plugin"
  22. rpc "github.com/tal-tech/go-zero/tools/goctl/rpc/cli"
  23. "github.com/tal-tech/go-zero/tools/goctl/tpl"
  24. "github.com/urfave/cli"
  25. )
  26. var (
  27. BuildVersion = "1.1.1"
  28. commands = []cli.Command{
  29. {
  30. Name: "api",
  31. Usage: "generate api related files",
  32. Flags: []cli.Flag{
  33. cli.StringFlag{
  34. Name: "o",
  35. Usage: "the output api file",
  36. },
  37. },
  38. Action: apigen.ApiCommand,
  39. Subcommands: []cli.Command{
  40. {
  41. Name: "new",
  42. Usage: "fast create api service",
  43. Action: new.NewService,
  44. },
  45. {
  46. Name: "format",
  47. Usage: "format api files",
  48. Flags: []cli.Flag{
  49. cli.StringFlag{
  50. Name: "dir",
  51. Usage: "the format target dir",
  52. },
  53. cli.BoolFlag{
  54. Name: "iu",
  55. Usage: "ignore update",
  56. },
  57. cli.BoolFlag{
  58. Name: "stdin",
  59. Usage: "use stdin to input api doc content, press \"ctrl + d\" to send EOF",
  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. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  101. },
  102. },
  103. Action: gogen.GoCommand,
  104. },
  105. {
  106. Name: "java",
  107. Usage: "generate java files for provided api in api file",
  108. Flags: []cli.Flag{
  109. cli.StringFlag{
  110. Name: "dir",
  111. Usage: "the target dir",
  112. },
  113. cli.StringFlag{
  114. Name: "api",
  115. Usage: "the api file",
  116. },
  117. },
  118. Action: javagen.JavaCommand,
  119. },
  120. {
  121. Name: "ts",
  122. Usage: "generate ts files for provided api in api file",
  123. Flags: []cli.Flag{
  124. cli.StringFlag{
  125. Name: "dir",
  126. Usage: "the target dir",
  127. },
  128. cli.StringFlag{
  129. Name: "api",
  130. Usage: "the api file",
  131. },
  132. cli.StringFlag{
  133. Name: "webapi",
  134. Usage: "the web api file path",
  135. },
  136. cli.StringFlag{
  137. Name: "caller",
  138. Usage: "the web api caller",
  139. },
  140. cli.BoolFlag{
  141. Name: "unwrap",
  142. Usage: "unwrap the webapi caller for import",
  143. },
  144. },
  145. Action: tsgen.TsCommand,
  146. },
  147. {
  148. Name: "dart",
  149. Usage: "generate dart files for provided api in api file",
  150. Flags: []cli.Flag{
  151. cli.StringFlag{
  152. Name: "dir",
  153. Usage: "the target dir",
  154. },
  155. cli.StringFlag{
  156. Name: "api",
  157. Usage: "the api file",
  158. },
  159. },
  160. Action: dartgen.DartCommand,
  161. },
  162. {
  163. Name: "kt",
  164. Usage: "generate kotlin code for provided api file",
  165. Flags: []cli.Flag{
  166. cli.StringFlag{
  167. Name: "dir",
  168. Usage: "the target directory",
  169. },
  170. cli.StringFlag{
  171. Name: "api",
  172. Usage: "the api file",
  173. },
  174. cli.StringFlag{
  175. Name: "pkg",
  176. Usage: "define package name for kotlin file",
  177. },
  178. },
  179. Action: ktgen.KtCommand,
  180. },
  181. {
  182. Name: "plugin",
  183. Usage: "custom file generator",
  184. Flags: []cli.Flag{
  185. cli.StringFlag{
  186. Name: "plugin, p",
  187. Usage: "the plugin file",
  188. },
  189. cli.StringFlag{
  190. Name: "dir",
  191. Usage: "the target directory",
  192. },
  193. cli.StringFlag{
  194. Name: "api",
  195. Usage: "the api file",
  196. },
  197. cli.StringFlag{
  198. Name: "style",
  199. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  200. },
  201. },
  202. Action: plugin.PluginCommand,
  203. },
  204. },
  205. },
  206. {
  207. Name: "docker",
  208. Usage: "generate Dockerfile",
  209. Flags: []cli.Flag{
  210. cli.StringFlag{
  211. Name: "go",
  212. Usage: "the file that contains main function",
  213. },
  214. cli.IntFlag{
  215. Name: "port",
  216. Usage: "the port to expose, default none",
  217. Value: 0,
  218. },
  219. },
  220. Action: docker.DockerCommand,
  221. },
  222. {
  223. Name: "kube",
  224. Usage: "generate kubernetes files",
  225. Subcommands: []cli.Command{
  226. {
  227. Name: "deploy",
  228. Usage: "generate deployment yaml file",
  229. Flags: []cli.Flag{
  230. cli.StringFlag{
  231. Name: "name",
  232. Usage: "the name of deployment",
  233. Required: true,
  234. },
  235. cli.StringFlag{
  236. Name: "namespace",
  237. Usage: "the namespace of deployment",
  238. Required: true,
  239. },
  240. cli.StringFlag{
  241. Name: "image",
  242. Usage: "the docker image of deployment",
  243. Required: true,
  244. },
  245. cli.StringFlag{
  246. Name: "secret",
  247. Usage: "the image pull secret",
  248. Required: true,
  249. },
  250. cli.IntFlag{
  251. Name: "requestCpu",
  252. Usage: "the request cpu to deploy",
  253. Value: 500,
  254. },
  255. cli.IntFlag{
  256. Name: "requestMem",
  257. Usage: "the request memory to deploy",
  258. Value: 512,
  259. },
  260. cli.IntFlag{
  261. Name: "limitCpu",
  262. Usage: "the limit cpu to deploy",
  263. Value: 1000,
  264. },
  265. cli.IntFlag{
  266. Name: "limitMem",
  267. Usage: "the limit memory to deploy",
  268. Value: 1024,
  269. },
  270. cli.StringFlag{
  271. Name: "o",
  272. Usage: "the output yaml file",
  273. Required: true,
  274. },
  275. cli.IntFlag{
  276. Name: "replicas",
  277. Usage: "the number of replicas to deploy",
  278. Value: 3,
  279. },
  280. cli.IntFlag{
  281. Name: "revisions",
  282. Usage: "the number of revision history to limit",
  283. Value: 5,
  284. },
  285. cli.IntFlag{
  286. Name: "port",
  287. Usage: "the port of the deployment to listen on pod",
  288. Required: true,
  289. },
  290. cli.IntFlag{
  291. Name: "nodePort",
  292. Usage: "the nodePort of the deployment to expose",
  293. Value: 0,
  294. },
  295. cli.IntFlag{
  296. Name: "minReplicas",
  297. Usage: "the min replicas to deploy",
  298. Value: 3,
  299. },
  300. cli.IntFlag{
  301. Name: "maxReplicas",
  302. Usage: "the max replicas of deploy",
  303. Value: 10,
  304. },
  305. },
  306. Action: kube.DeploymentCommand,
  307. },
  308. },
  309. },
  310. {
  311. Name: "rpc",
  312. Usage: "generate rpc code",
  313. Subcommands: []cli.Command{
  314. {
  315. Name: "new",
  316. Usage: `generate rpc demo service`,
  317. Flags: []cli.Flag{
  318. cli.StringFlag{
  319. Name: "style",
  320. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  321. },
  322. cli.BoolFlag{
  323. Name: "idea",
  324. Usage: "whether the command execution environment is from idea plugin. [optional]",
  325. },
  326. },
  327. Action: rpc.RpcNew,
  328. },
  329. {
  330. Name: "template",
  331. Usage: `generate proto template`,
  332. Flags: []cli.Flag{
  333. cli.StringFlag{
  334. Name: "out, o",
  335. Usage: "the target path of proto",
  336. },
  337. },
  338. Action: rpc.RpcTemplate,
  339. },
  340. {
  341. Name: "proto",
  342. Usage: `generate rpc from proto`,
  343. Flags: []cli.Flag{
  344. cli.StringFlag{
  345. Name: "src, s",
  346. Usage: "the file path of the proto source file",
  347. },
  348. cli.StringSliceFlag{
  349. Name: "proto_path, I",
  350. Usage: `native command of protoc, specify the directory in which to search for imports. [optional]`,
  351. },
  352. cli.StringFlag{
  353. Name: "dir, d",
  354. Usage: `the target path of the code`,
  355. },
  356. cli.StringFlag{
  357. Name: "style",
  358. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  359. },
  360. cli.BoolFlag{
  361. Name: "idea",
  362. Usage: "whether the command execution environment is from idea plugin. [optional]",
  363. },
  364. },
  365. Action: rpc.Rpc,
  366. },
  367. },
  368. },
  369. {
  370. Name: "model",
  371. Usage: "generate model code",
  372. Subcommands: []cli.Command{
  373. {
  374. Name: "mysql",
  375. Usage: `generate mysql model`,
  376. Subcommands: []cli.Command{
  377. {
  378. Name: "ddl",
  379. Usage: `generate mysql model from ddl`,
  380. Flags: []cli.Flag{
  381. cli.StringFlag{
  382. Name: "src, s",
  383. Usage: "the path or path globbing patterns of the ddl",
  384. },
  385. cli.StringFlag{
  386. Name: "dir, d",
  387. Usage: "the target dir",
  388. },
  389. cli.StringFlag{
  390. Name: "style",
  391. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  392. },
  393. cli.BoolFlag{
  394. Name: "cache, c",
  395. Usage: "generate code with cache [optional]",
  396. },
  397. cli.BoolFlag{
  398. Name: "idea",
  399. Usage: "for idea plugin [optional]",
  400. },
  401. },
  402. Action: model.MysqlDDL,
  403. },
  404. {
  405. Name: "datasource",
  406. Usage: `generate model from datasource`,
  407. Flags: []cli.Flag{
  408. cli.StringFlag{
  409. Name: "url",
  410. Usage: `the data source of database,like "root:password@tcp(127.0.0.1:3306)/database`,
  411. },
  412. cli.StringFlag{
  413. Name: "table, t",
  414. Usage: `the table or table globbing patterns in the database`,
  415. },
  416. cli.BoolFlag{
  417. Name: "cache, c",
  418. Usage: "generate code with cache [optional]",
  419. },
  420. cli.StringFlag{
  421. Name: "dir, d",
  422. Usage: "the target dir",
  423. },
  424. cli.StringFlag{
  425. Name: "style",
  426. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  427. },
  428. cli.BoolFlag{
  429. Name: "idea",
  430. Usage: "for idea plugin [optional]",
  431. },
  432. },
  433. Action: model.MyDataSource,
  434. },
  435. },
  436. },
  437. },
  438. },
  439. {
  440. Name: "config",
  441. Usage: "generate config json",
  442. Flags: []cli.Flag{
  443. cli.StringFlag{
  444. Name: "path, p",
  445. Usage: "the target config go file",
  446. },
  447. },
  448. Action: configgen.GenConfigCommand,
  449. },
  450. {
  451. Name: "template",
  452. Usage: "template operation",
  453. Subcommands: []cli.Command{
  454. {
  455. Name: "init",
  456. Usage: "initialize the all templates(force update)",
  457. Action: tpl.GenTemplates,
  458. },
  459. {
  460. Name: "clean",
  461. Usage: "clean the all cache templates",
  462. Action: tpl.CleanTemplates,
  463. },
  464. {
  465. Name: "update",
  466. Usage: "update template of the target category to the latest",
  467. Flags: []cli.Flag{
  468. cli.StringFlag{
  469. Name: "category,c",
  470. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  471. },
  472. },
  473. Action: tpl.UpdateTemplates,
  474. },
  475. {
  476. Name: "revert",
  477. Usage: "revert the target template to the latest",
  478. Flags: []cli.Flag{
  479. cli.StringFlag{
  480. Name: "category,c",
  481. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  482. },
  483. cli.StringFlag{
  484. Name: "name,n",
  485. Usage: "the target file name of template",
  486. },
  487. },
  488. Action: tpl.RevertTemplates,
  489. },
  490. },
  491. },
  492. }
  493. )
  494. func main() {
  495. logx.Disable()
  496. app := cli.NewApp()
  497. app.Usage = "a cli tool to generate code"
  498. app.Version = fmt.Sprintf("%s %s/%s", BuildVersion, runtime.GOOS, runtime.GOARCH)
  499. app.Commands = commands
  500. // cli already print error messages
  501. if err := app.Run(os.Args); err != nil {
  502. fmt.Println("error:", err)
  503. }
  504. }