goctl.go 16 KB

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