goctl.go 17 KB

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