goctl.go 22 KB

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