goctl.go 27 KB

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