goctl.go 24 KB

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