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