goctl.go 24 KB

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