goctl.go 26 KB

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