goctl.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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. cli.StringFlag{
  408. Name: "serviceAccount",
  409. Usage: "the ServiceAccount for the deployment",
  410. },
  411. },
  412. Action: kube.DeploymentCommand,
  413. },
  414. },
  415. },
  416. {
  417. Name: "rpc",
  418. Usage: "generate rpc code",
  419. Subcommands: []cli.Command{
  420. {
  421. Name: "new",
  422. Usage: `generate rpc demo service`,
  423. Description: aurora.Yellow(`deprecated: zrpc code generation use "goctl rpc protoc" instead, for the details see "goctl rpc protoc --help"`).String(),
  424. Flags: []cli.Flag{
  425. cli.StringFlag{
  426. Name: "style",
  427. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  428. },
  429. cli.BoolFlag{
  430. Name: "idea",
  431. Usage: "whether the command execution environment is from idea plugin. [optional]",
  432. },
  433. cli.StringFlag{
  434. Name: "home",
  435. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  436. "if they are, --remote has higher priority",
  437. },
  438. cli.StringFlag{
  439. Name: "remote",
  440. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  441. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  442. "https://github.com/zeromicro/go-zero-template directory structure",
  443. },
  444. },
  445. Action: rpc.RPCNew,
  446. },
  447. {
  448. Name: "template",
  449. Usage: `generate proto template`,
  450. Flags: []cli.Flag{
  451. cli.StringFlag{
  452. Name: "out, o",
  453. Usage: "the target path of proto",
  454. },
  455. cli.StringFlag{
  456. Name: "home",
  457. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time," +
  458. " if they are, --remote has higher priority",
  459. },
  460. cli.StringFlag{
  461. Name: "remote",
  462. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  463. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  464. "https://github.com/zeromicro/go-zero-template directory structure",
  465. },
  466. },
  467. Action: rpc.RPCTemplate,
  468. },
  469. {
  470. Name: "protoc",
  471. Usage: "generate grpc code",
  472. UsageText: `example: goctl rpc protoc xx.proto --go_out=./pb --go-grpc_out=./pb --zrpc_out=.`,
  473. Description: "for details, see https://go-zero.dev/cn/goctl-rpc.html",
  474. Action: rpc.ZRPC,
  475. Flags: []cli.Flag{
  476. cli.StringFlag{
  477. Name: "go_out",
  478. Hidden: true,
  479. },
  480. cli.StringFlag{
  481. Name: "go-grpc_out",
  482. Hidden: true,
  483. },
  484. cli.StringFlag{
  485. Name: "go_opt",
  486. Hidden: true,
  487. },
  488. cli.StringFlag{
  489. Name: "go-grpc_opt",
  490. Hidden: true,
  491. },
  492. cli.StringFlag{
  493. Name: "zrpc_out",
  494. Usage: "the zrpc output directory",
  495. },
  496. cli.StringFlag{
  497. Name: "style",
  498. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  499. },
  500. cli.StringFlag{
  501. Name: "home",
  502. Usage: "the goctl home path of the template",
  503. },
  504. cli.StringFlag{
  505. Name: "remote",
  506. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  507. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  508. "https://github.com/zeromicro/go-zero-template directory structure",
  509. },
  510. },
  511. },
  512. {
  513. Name: "proto",
  514. Usage: `generate rpc from proto`,
  515. Description: aurora.Yellow(`deprecated: zrpc code generation use "goctl rpc protoc" instead, for the details see "goctl rpc protoc --help"`).String(),
  516. Flags: []cli.Flag{
  517. cli.StringFlag{
  518. Name: "src, s",
  519. Usage: "the file path of the proto source file",
  520. },
  521. cli.StringSliceFlag{
  522. Name: "proto_path, I",
  523. Usage: `native command of protoc, specify the directory in which to search for imports. [optional]`,
  524. },
  525. cli.StringSliceFlag{
  526. Name: "go_opt",
  527. Usage: `native command of protoc-gen-go, specify the mapping from proto to go, eg --go_opt=proto_import=go_package_import. [optional]`,
  528. },
  529. cli.StringFlag{
  530. Name: "dir, d",
  531. Usage: `the target path of the code`,
  532. },
  533. cli.StringFlag{
  534. Name: "style",
  535. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  536. },
  537. cli.BoolFlag{
  538. Name: "idea",
  539. Usage: "whether the command execution environment is from idea plugin. [optional]",
  540. },
  541. cli.StringFlag{
  542. Name: "home",
  543. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  544. "if they are, --remote has higher priority",
  545. },
  546. cli.StringFlag{
  547. Name: "remote",
  548. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  549. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  550. "https://github.com/zeromicro/go-zero-template directory structure",
  551. },
  552. },
  553. Action: rpc.RPC,
  554. },
  555. },
  556. },
  557. {
  558. Name: "model",
  559. Usage: "generate model code",
  560. Subcommands: []cli.Command{
  561. {
  562. Name: "mysql",
  563. Usage: `generate mysql model`,
  564. Subcommands: []cli.Command{
  565. {
  566. Name: "ddl",
  567. Usage: `generate mysql model from ddl`,
  568. Flags: []cli.Flag{
  569. cli.StringFlag{
  570. Name: "src, s",
  571. Usage: "the path or path globbing patterns of the ddl",
  572. },
  573. cli.StringFlag{
  574. Name: "dir, d",
  575. Usage: "the target dir",
  576. },
  577. cli.StringFlag{
  578. Name: "style",
  579. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  580. },
  581. cli.BoolFlag{
  582. Name: "cache, c",
  583. Usage: "generate code with cache [optional]",
  584. },
  585. cli.BoolFlag{
  586. Name: "idea",
  587. Usage: "for idea plugin [optional]",
  588. },
  589. cli.StringFlag{
  590. Name: "database, db",
  591. Usage: "the name of database [optional]",
  592. },
  593. cli.StringFlag{
  594. Name: "home",
  595. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  596. "if they are, --remote has higher priority",
  597. },
  598. cli.StringFlag{
  599. Name: "remote",
  600. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  601. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  602. "https://github.com/zeromicro/go-zero-template directory structure",
  603. },
  604. },
  605. Action: model.MysqlDDL,
  606. },
  607. {
  608. Name: "datasource",
  609. Usage: `generate model from datasource`,
  610. Flags: []cli.Flag{
  611. cli.StringFlag{
  612. Name: "url",
  613. Usage: `the data source of database,like "root:password@tcp(127.0.0.1:3306)/database"`,
  614. },
  615. cli.StringFlag{
  616. Name: "table, t",
  617. Usage: `the table or table globbing patterns in the database`,
  618. },
  619. cli.BoolFlag{
  620. Name: "cache, c",
  621. Usage: "generate code with cache [optional]",
  622. },
  623. cli.StringFlag{
  624. Name: "dir, d",
  625. Usage: "the target dir",
  626. },
  627. cli.StringFlag{
  628. Name: "style",
  629. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  630. },
  631. cli.BoolFlag{
  632. Name: "idea",
  633. Usage: "for idea plugin [optional]",
  634. },
  635. cli.StringFlag{
  636. Name: "home",
  637. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  638. "if they are, --remote has higher priority",
  639. },
  640. cli.StringFlag{
  641. Name: "remote",
  642. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  643. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  644. "https://github.com/zeromicro/go-zero-template directory structure",
  645. },
  646. },
  647. Action: model.MySqlDataSource,
  648. },
  649. },
  650. },
  651. {
  652. Name: "pg",
  653. Usage: `generate postgresql model`,
  654. Subcommands: []cli.Command{
  655. {
  656. Name: "datasource",
  657. Usage: `generate model from datasource`,
  658. Flags: []cli.Flag{
  659. cli.StringFlag{
  660. Name: "url",
  661. Usage: `the data source of database,like "postgres://root:password@127.0.0.1:54332/database?sslmode=disable"`,
  662. },
  663. cli.StringFlag{
  664. Name: "table, t",
  665. Usage: `the table or table globbing patterns in the database`,
  666. },
  667. cli.StringFlag{
  668. Name: "schema, s",
  669. Usage: `the table schema, default is [public]`,
  670. },
  671. cli.BoolFlag{
  672. Name: "cache, c",
  673. Usage: "generate code with cache [optional]",
  674. },
  675. cli.StringFlag{
  676. Name: "dir, d",
  677. Usage: "the target dir",
  678. },
  679. cli.StringFlag{
  680. Name: "style",
  681. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  682. },
  683. cli.BoolFlag{
  684. Name: "idea",
  685. Usage: "for idea plugin [optional]",
  686. },
  687. cli.StringFlag{
  688. Name: "home",
  689. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  690. "if they are, --remote has higher priority",
  691. },
  692. cli.StringFlag{
  693. Name: "remote",
  694. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  695. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  696. "https://github.com/zeromicro/go-zero-template directory structure",
  697. },
  698. },
  699. Action: model.PostgreSqlDataSource,
  700. },
  701. },
  702. },
  703. {
  704. Name: "mongo",
  705. Usage: `generate mongo model`,
  706. Flags: []cli.Flag{
  707. cli.StringSliceFlag{
  708. Name: "type, t",
  709. Usage: "specified model type name",
  710. },
  711. cli.BoolFlag{
  712. Name: "cache, c",
  713. Usage: "generate code with cache [optional]",
  714. },
  715. cli.StringFlag{
  716. Name: "dir, d",
  717. Usage: "the target dir",
  718. },
  719. cli.StringFlag{
  720. Name: "style",
  721. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  722. },
  723. cli.StringFlag{
  724. Name: "home",
  725. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time," +
  726. " if they are, --remote has higher priority",
  727. },
  728. cli.StringFlag{
  729. Name: "remote",
  730. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  731. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  732. "https://github.com/zeromicro/go-zero-template directory structure",
  733. },
  734. },
  735. Action: mongo.Action,
  736. },
  737. },
  738. },
  739. {
  740. Name: "template",
  741. Usage: "template operation",
  742. Subcommands: []cli.Command{
  743. {
  744. Name: "init",
  745. Usage: "initialize the all templates(force update)",
  746. Flags: []cli.Flag{
  747. cli.StringFlag{
  748. Name: "home",
  749. Usage: "the goctl home path of the template",
  750. },
  751. },
  752. Action: tpl.GenTemplates,
  753. },
  754. {
  755. Name: "clean",
  756. Usage: "clean the all cache templates",
  757. Flags: []cli.Flag{
  758. cli.StringFlag{
  759. Name: "home",
  760. Usage: "the goctl home path of the template",
  761. },
  762. },
  763. Action: tpl.CleanTemplates,
  764. },
  765. {
  766. Name: "update",
  767. Usage: "update template of the target category to the latest",
  768. Flags: []cli.Flag{
  769. cli.StringFlag{
  770. Name: "category,c",
  771. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  772. },
  773. cli.StringFlag{
  774. Name: "home",
  775. Usage: "the goctl home path of the template",
  776. },
  777. },
  778. Action: tpl.UpdateTemplates,
  779. },
  780. {
  781. Name: "revert",
  782. Usage: "revert the target template to the latest",
  783. Flags: []cli.Flag{
  784. cli.StringFlag{
  785. Name: "category,c",
  786. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  787. },
  788. cli.StringFlag{
  789. Name: "name,n",
  790. Usage: "the target file name of template",
  791. },
  792. cli.StringFlag{
  793. Name: "home",
  794. Usage: "the goctl home path of the template",
  795. },
  796. },
  797. Action: tpl.RevertTemplates,
  798. },
  799. },
  800. },
  801. {
  802. Name: "completion",
  803. Usage: "generation completion script, it only works for unix-like OS",
  804. Action: completion.Completion,
  805. Flags: []cli.Flag{
  806. cli.StringFlag{
  807. Name: "name, n",
  808. Usage: "the filename of auto complete script, default is [goctl_autocomplete]",
  809. },
  810. },
  811. },
  812. }
  813. func main() {
  814. logx.Disable()
  815. load.Disable()
  816. cli.BashCompletionFlag = cli.BoolFlag{
  817. Name: completion.BashCompletionFlag,
  818. Hidden: true,
  819. }
  820. app := cli.NewApp()
  821. app.EnableBashCompletion = true
  822. app.Usage = "a cli tool to generate code"
  823. app.Version = fmt.Sprintf("%s %s/%s", version.BuildVersion, runtime.GOOS, runtime.GOARCH)
  824. app.Commands = commands
  825. // cli already print error messages
  826. if err := app.Run(os.Args); err != nil {
  827. fmt.Println(aurora.Red(errorx.Wrap(err).Error()))
  828. os.Exit(codeFailure)
  829. }
  830. }