goctl.go 16 KB

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