goctl.go 17 KB

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