goctl.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "runtime"
  6. "github.com/logrusorgru/aurora"
  7. "github.com/tal-tech/go-zero/core/load"
  8. "github.com/tal-tech/go-zero/core/logx"
  9. "github.com/tal-tech/go-zero/core/stat"
  10. "github.com/tal-tech/go-zero/tools/goctl/api/apigen"
  11. "github.com/tal-tech/go-zero/tools/goctl/api/dartgen"
  12. "github.com/tal-tech/go-zero/tools/goctl/api/docgen"
  13. "github.com/tal-tech/go-zero/tools/goctl/api/format"
  14. "github.com/tal-tech/go-zero/tools/goctl/api/gogen"
  15. "github.com/tal-tech/go-zero/tools/goctl/api/javagen"
  16. "github.com/tal-tech/go-zero/tools/goctl/api/ktgen"
  17. "github.com/tal-tech/go-zero/tools/goctl/api/new"
  18. "github.com/tal-tech/go-zero/tools/goctl/api/tsgen"
  19. "github.com/tal-tech/go-zero/tools/goctl/api/validate"
  20. "github.com/tal-tech/go-zero/tools/goctl/docker"
  21. "github.com/tal-tech/go-zero/tools/goctl/internal/errorx"
  22. "github.com/tal-tech/go-zero/tools/goctl/internal/version"
  23. "github.com/tal-tech/go-zero/tools/goctl/kube"
  24. "github.com/tal-tech/go-zero/tools/goctl/model/mongo"
  25. model "github.com/tal-tech/go-zero/tools/goctl/model/sql/command"
  26. "github.com/tal-tech/go-zero/tools/goctl/plugin"
  27. rpc "github.com/tal-tech/go-zero/tools/goctl/rpc/cli"
  28. "github.com/tal-tech/go-zero/tools/goctl/tpl"
  29. "github.com/tal-tech/go-zero/tools/goctl/upgrade"
  30. "github.com/urfave/cli"
  31. )
  32. var commands = []cli.Command{
  33. {
  34. Name: "upgrade",
  35. Usage: "upgrade goctl to latest version",
  36. Action: upgrade.Upgrade,
  37. },
  38. {
  39. Name: "api",
  40. Usage: "generate api related files",
  41. Flags: []cli.Flag{
  42. cli.StringFlag{
  43. Name: "o",
  44. Usage: "the output api file",
  45. },
  46. cli.StringFlag{
  47. Name: "home",
  48. Usage: "the goctl home path of the template",
  49. },
  50. },
  51. Action: apigen.ApiCommand,
  52. Subcommands: []cli.Command{
  53. {
  54. Name: "new",
  55. Usage: "fast create api service",
  56. Action: new.CreateServiceCommand,
  57. },
  58. {
  59. Name: "format",
  60. Usage: "format api files",
  61. Flags: []cli.Flag{
  62. cli.StringFlag{
  63. Name: "dir",
  64. Usage: "the format target dir",
  65. },
  66. cli.BoolFlag{
  67. Name: "iu",
  68. Usage: "ignore update",
  69. },
  70. cli.BoolFlag{
  71. Name: "stdin",
  72. Usage: "use stdin to input api doc content, press \"ctrl + d\" to send EOF",
  73. },
  74. },
  75. Action: format.GoFormatApi,
  76. },
  77. {
  78. Name: "validate",
  79. Usage: "validate api file",
  80. Flags: []cli.Flag{
  81. cli.StringFlag{
  82. Name: "api",
  83. Usage: "validate target api file",
  84. },
  85. },
  86. Action: validate.GoValidateApi,
  87. },
  88. {
  89. Name: "doc",
  90. Usage: "generate doc files",
  91. Flags: []cli.Flag{
  92. cli.StringFlag{
  93. Name: "dir",
  94. Usage: "the target dir",
  95. },
  96. cli.StringFlag{
  97. Name: "o",
  98. Required: false,
  99. Usage: "the output markdown directory",
  100. },
  101. },
  102. Action: docgen.DocCommand,
  103. },
  104. {
  105. Name: "go",
  106. Usage: "generate go files for provided api in yaml file",
  107. Flags: []cli.Flag{
  108. cli.StringFlag{
  109. Name: "dir",
  110. Usage: "the target dir",
  111. },
  112. cli.StringFlag{
  113. Name: "api",
  114. Usage: "the api file",
  115. },
  116. cli.StringFlag{
  117. Name: "style",
  118. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  119. },
  120. cli.StringFlag{
  121. Name: "home",
  122. Usage: "the goctl home path of the template",
  123. },
  124. },
  125. Action: gogen.GoCommand,
  126. },
  127. {
  128. Name: "java",
  129. Usage: "generate java files for provided api in api file",
  130. Flags: []cli.Flag{
  131. cli.StringFlag{
  132. Name: "dir",
  133. Usage: "the target dir",
  134. },
  135. cli.StringFlag{
  136. Name: "api",
  137. Usage: "the api file",
  138. },
  139. },
  140. Action: javagen.JavaCommand,
  141. },
  142. {
  143. Name: "ts",
  144. Usage: "generate ts files for provided api in api file",
  145. Flags: []cli.Flag{
  146. cli.StringFlag{
  147. Name: "dir",
  148. Usage: "the target dir",
  149. },
  150. cli.StringFlag{
  151. Name: "api",
  152. Usage: "the api file",
  153. },
  154. cli.StringFlag{
  155. Name: "webapi",
  156. Usage: "the web api file path",
  157. },
  158. cli.StringFlag{
  159. Name: "caller",
  160. Usage: "the web api caller",
  161. },
  162. cli.BoolFlag{
  163. Name: "unwrap",
  164. Usage: "unwrap the webapi caller for import",
  165. },
  166. },
  167. Action: tsgen.TsCommand,
  168. },
  169. {
  170. Name: "dart",
  171. Usage: "generate dart files for provided api in api file",
  172. Flags: []cli.Flag{
  173. cli.StringFlag{
  174. Name: "dir",
  175. Usage: "the target dir",
  176. },
  177. cli.StringFlag{
  178. Name: "api",
  179. Usage: "the api file",
  180. },
  181. },
  182. Action: dartgen.DartCommand,
  183. },
  184. {
  185. Name: "kt",
  186. Usage: "generate kotlin code for provided api file",
  187. Flags: []cli.Flag{
  188. cli.StringFlag{
  189. Name: "dir",
  190. Usage: "the target directory",
  191. },
  192. cli.StringFlag{
  193. Name: "api",
  194. Usage: "the api file",
  195. },
  196. cli.StringFlag{
  197. Name: "pkg",
  198. Usage: "define package name for kotlin file",
  199. },
  200. },
  201. Action: ktgen.KtCommand,
  202. },
  203. {
  204. Name: "plugin",
  205. Usage: "custom file generator",
  206. Flags: []cli.Flag{
  207. cli.StringFlag{
  208. Name: "plugin, p",
  209. Usage: "the plugin file",
  210. },
  211. cli.StringFlag{
  212. Name: "dir",
  213. Usage: "the target directory",
  214. },
  215. cli.StringFlag{
  216. Name: "api",
  217. Usage: "the api file",
  218. },
  219. cli.StringFlag{
  220. Name: "style",
  221. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  222. },
  223. },
  224. Action: plugin.PluginCommand,
  225. },
  226. },
  227. },
  228. {
  229. Name: "docker",
  230. Usage: "generate Dockerfile",
  231. Flags: []cli.Flag{
  232. cli.StringFlag{
  233. Name: "go",
  234. Usage: "the file that contains main function",
  235. },
  236. cli.IntFlag{
  237. Name: "port",
  238. Usage: "the port to expose, default none",
  239. Value: 0,
  240. },
  241. cli.StringFlag{
  242. Name: "home",
  243. Usage: "the goctl home path of the template",
  244. },
  245. },
  246. Action: docker.DockerCommand,
  247. },
  248. {
  249. Name: "kube",
  250. Usage: "generate kubernetes files",
  251. Subcommands: []cli.Command{
  252. {
  253. Name: "deploy",
  254. Usage: "generate deployment yaml file",
  255. Flags: []cli.Flag{
  256. cli.StringFlag{
  257. Name: "name",
  258. Usage: "the name of deployment",
  259. Required: true,
  260. },
  261. cli.StringFlag{
  262. Name: "namespace",
  263. Usage: "the namespace of deployment",
  264. Required: true,
  265. },
  266. cli.StringFlag{
  267. Name: "image",
  268. Usage: "the docker image of deployment",
  269. Required: true,
  270. },
  271. cli.StringFlag{
  272. Name: "secret",
  273. Usage: "the secret to image pull from registry",
  274. },
  275. cli.IntFlag{
  276. Name: "requestCpu",
  277. Usage: "the request cpu to deploy",
  278. Value: 500,
  279. },
  280. cli.IntFlag{
  281. Name: "requestMem",
  282. Usage: "the request memory to deploy",
  283. Value: 512,
  284. },
  285. cli.IntFlag{
  286. Name: "limitCpu",
  287. Usage: "the limit cpu to deploy",
  288. Value: 1000,
  289. },
  290. cli.IntFlag{
  291. Name: "limitMem",
  292. Usage: "the limit memory to deploy",
  293. Value: 1024,
  294. },
  295. cli.StringFlag{
  296. Name: "o",
  297. Usage: "the output yaml file",
  298. Required: true,
  299. },
  300. cli.IntFlag{
  301. Name: "replicas",
  302. Usage: "the number of replicas to deploy",
  303. Value: 3,
  304. },
  305. cli.IntFlag{
  306. Name: "revisions",
  307. Usage: "the number of revision history to limit",
  308. Value: 5,
  309. },
  310. cli.IntFlag{
  311. Name: "port",
  312. Usage: "the port of the deployment to listen on pod",
  313. Required: true,
  314. },
  315. cli.IntFlag{
  316. Name: "nodePort",
  317. Usage: "the nodePort of the deployment to expose",
  318. Value: 0,
  319. },
  320. cli.IntFlag{
  321. Name: "minReplicas",
  322. Usage: "the min replicas to deploy",
  323. Value: 3,
  324. },
  325. cli.IntFlag{
  326. Name: "maxReplicas",
  327. Usage: "the max replicas of deploy",
  328. Value: 10,
  329. },
  330. cli.StringFlag{
  331. Name: "home",
  332. Usage: "the goctl home path of the template",
  333. },
  334. },
  335. Action: kube.DeploymentCommand,
  336. },
  337. },
  338. },
  339. {
  340. Name: "rpc",
  341. Usage: "generate rpc code",
  342. Subcommands: []cli.Command{
  343. {
  344. Name: "new",
  345. Usage: `generate rpc demo service`,
  346. Flags: []cli.Flag{
  347. cli.StringFlag{
  348. Name: "style",
  349. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  350. },
  351. cli.BoolFlag{
  352. Name: "idea",
  353. Usage: "whether the command execution environment is from idea plugin. [optional]",
  354. },
  355. cli.StringFlag{
  356. Name: "home",
  357. Usage: "the goctl home path of the template",
  358. },
  359. },
  360. Action: rpc.RPCNew,
  361. },
  362. {
  363. Name: "template",
  364. Usage: `generate proto template`,
  365. Flags: []cli.Flag{
  366. cli.StringFlag{
  367. Name: "out, o",
  368. Usage: "the target path of proto",
  369. },
  370. cli.StringFlag{
  371. Name: "home",
  372. Usage: "the goctl home path of the template",
  373. },
  374. },
  375. Action: rpc.RPCTemplate,
  376. },
  377. {
  378. Name: "proto",
  379. Usage: `generate rpc from proto`,
  380. Flags: []cli.Flag{
  381. cli.StringFlag{
  382. Name: "src, s",
  383. Usage: "the file path of the proto source file",
  384. },
  385. cli.StringSliceFlag{
  386. Name: "proto_path, I",
  387. Usage: `native command of protoc, specify the directory in which to search for imports. [optional]`,
  388. },
  389. cli.StringSliceFlag{
  390. Name: "go_opt",
  391. Usage: `native command of protoc-gen-go, specify the mapping from proto to go, eg --go_opt=proto_import=go_package_import. [optional]`,
  392. },
  393. cli.StringFlag{
  394. Name: "dir, d",
  395. Usage: `the target path of the code`,
  396. },
  397. cli.StringFlag{
  398. Name: "style",
  399. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  400. },
  401. cli.BoolFlag{
  402. Name: "idea",
  403. Usage: "whether the command execution environment is from idea plugin. [optional]",
  404. },
  405. cli.StringFlag{
  406. Name: "home",
  407. Usage: "the goctl home path of the template",
  408. },
  409. },
  410. Action: rpc.RPC,
  411. },
  412. },
  413. },
  414. {
  415. Name: "model",
  416. Usage: "generate model code",
  417. Subcommands: []cli.Command{
  418. {
  419. Name: "mysql",
  420. Usage: `generate mysql model`,
  421. Subcommands: []cli.Command{
  422. {
  423. Name: "ddl",
  424. Usage: `generate mysql model from ddl`,
  425. Flags: []cli.Flag{
  426. cli.StringFlag{
  427. Name: "src, s",
  428. Usage: "the path or path globbing patterns of the ddl",
  429. },
  430. cli.StringFlag{
  431. Name: "dir, d",
  432. Usage: "the target dir",
  433. },
  434. cli.StringFlag{
  435. Name: "style",
  436. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  437. },
  438. cli.BoolFlag{
  439. Name: "cache, c",
  440. Usage: "generate code with cache [optional]",
  441. },
  442. cli.BoolFlag{
  443. Name: "idea",
  444. Usage: "for idea plugin [optional]",
  445. },
  446. cli.StringFlag{
  447. Name: "database, db",
  448. Usage: "the name of database [optional]",
  449. },
  450. cli.StringFlag{
  451. Name: "home",
  452. Usage: "the goctl home path of the template",
  453. },
  454. },
  455. Action: model.MysqlDDL,
  456. },
  457. {
  458. Name: "datasource",
  459. Usage: `generate model from datasource`,
  460. Flags: []cli.Flag{
  461. cli.StringFlag{
  462. Name: "url",
  463. Usage: `the data source of database,like "root:password@tcp(127.0.0.1:3306)/database"`,
  464. },
  465. cli.StringFlag{
  466. Name: "table, t",
  467. Usage: `the table or table globbing patterns in the database`,
  468. },
  469. cli.BoolFlag{
  470. Name: "cache, c",
  471. Usage: "generate code with cache [optional]",
  472. },
  473. cli.StringFlag{
  474. Name: "dir, d",
  475. Usage: "the target dir",
  476. },
  477. cli.StringFlag{
  478. Name: "style",
  479. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  480. },
  481. cli.BoolFlag{
  482. Name: "idea",
  483. Usage: "for idea plugin [optional]",
  484. },
  485. cli.StringFlag{
  486. Name: "home",
  487. Usage: "the goctl home path of the template",
  488. },
  489. },
  490. Action: model.MySqlDataSource,
  491. },
  492. },
  493. },
  494. {
  495. Name: "pg",
  496. Usage: `generate postgresql model`,
  497. Subcommands: []cli.Command{
  498. {
  499. Name: "datasource",
  500. Usage: `generate model from datasource`,
  501. Flags: []cli.Flag{
  502. cli.StringFlag{
  503. Name: "url",
  504. Usage: `the data source of database,like "postgres://root:password@127.0.0.1:54332/database?sslmode=disable"`,
  505. },
  506. cli.StringFlag{
  507. Name: "table, t",
  508. Usage: `the table or table globbing patterns in the database`,
  509. },
  510. cli.StringFlag{
  511. Name: "schema, s",
  512. Usage: `the table schema, default is [public]`,
  513. },
  514. cli.BoolFlag{
  515. Name: "cache, c",
  516. Usage: "generate code with cache [optional]",
  517. },
  518. cli.StringFlag{
  519. Name: "dir, d",
  520. Usage: "the target dir",
  521. },
  522. cli.StringFlag{
  523. Name: "style",
  524. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  525. },
  526. cli.BoolFlag{
  527. Name: "idea",
  528. Usage: "for idea plugin [optional]",
  529. },
  530. cli.StringFlag{
  531. Name: "home",
  532. Usage: "the goctl home path of the template",
  533. },
  534. },
  535. Action: model.PostgreSqlDataSource,
  536. },
  537. },
  538. },
  539. {
  540. Name: "mongo",
  541. Usage: `generate mongo model`,
  542. Flags: []cli.Flag{
  543. cli.StringSliceFlag{
  544. Name: "type, t",
  545. Usage: "specified model type name",
  546. },
  547. cli.BoolFlag{
  548. Name: "cache, c",
  549. Usage: "generate code with cache [optional]",
  550. },
  551. cli.StringFlag{
  552. Name: "dir, d",
  553. Usage: "the target dir",
  554. },
  555. cli.StringFlag{
  556. Name: "style",
  557. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  558. },
  559. cli.StringFlag{
  560. Name: "home",
  561. Usage: "the goctl home path of the template",
  562. },
  563. },
  564. Action: mongo.Action,
  565. },
  566. },
  567. },
  568. {
  569. Name: "template",
  570. Usage: "template operation",
  571. Subcommands: []cli.Command{
  572. {
  573. Name: "init",
  574. Usage: "initialize the all templates(force update)",
  575. Flags: []cli.Flag{
  576. cli.StringFlag{
  577. Name: "home",
  578. Usage: "the goctl home path of the template",
  579. },
  580. },
  581. Action: tpl.GenTemplates,
  582. },
  583. {
  584. Name: "clean",
  585. Usage: "clean the all cache templates",
  586. Flags: []cli.Flag{
  587. cli.StringFlag{
  588. Name: "home",
  589. Usage: "the goctl home path of the template",
  590. },
  591. },
  592. Action: tpl.CleanTemplates,
  593. },
  594. {
  595. Name: "update",
  596. Usage: "update template of the target category to the latest",
  597. Flags: []cli.Flag{
  598. cli.StringFlag{
  599. Name: "category,c",
  600. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  601. },
  602. cli.StringFlag{
  603. Name: "home",
  604. Usage: "the goctl home path of the template",
  605. },
  606. },
  607. Action: tpl.UpdateTemplates,
  608. },
  609. {
  610. Name: "revert",
  611. Usage: "revert the target template to the latest",
  612. Flags: []cli.Flag{
  613. cli.StringFlag{
  614. Name: "category,c",
  615. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  616. },
  617. cli.StringFlag{
  618. Name: "name,n",
  619. Usage: "the target file name of template",
  620. },
  621. cli.StringFlag{
  622. Name: "home",
  623. Usage: "the goctl home path of the template",
  624. },
  625. },
  626. Action: tpl.RevertTemplates,
  627. },
  628. },
  629. },
  630. }
  631. func main() {
  632. logx.Disable()
  633. load.Disable()
  634. stat.DisableLog()
  635. app := cli.NewApp()
  636. app.Usage = "a cli tool to generate code"
  637. app.Version = fmt.Sprintf("%s %s/%s", version.BuildVersion, runtime.GOOS, runtime.GOARCH)
  638. app.Commands = commands
  639. // cli already print error messages
  640. if err := app.Run(os.Args); err != nil {
  641. fmt.Println(aurora.Red(errorx.Wrap(err).Error()))
  642. }
  643. }