goctl.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. Flags: []cli.Flag{
  513. cli.StringFlag{
  514. Name: "style",
  515. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  516. },
  517. cli.BoolFlag{
  518. Name: "idea",
  519. Usage: "whether the command execution environment is from idea plugin. [optional]",
  520. },
  521. cli.StringFlag{
  522. Name: "home",
  523. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  524. "if they are, --remote has higher priority",
  525. },
  526. cli.StringFlag{
  527. Name: "remote",
  528. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  529. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  530. "https://github.com/zeromicro/go-zero-template directory structure",
  531. },
  532. cli.StringFlag{
  533. Name: "branch",
  534. Usage: "the branch of the remote repo, it does work with --remote",
  535. },
  536. cli.BoolFlag{
  537. Name: "verbose, v",
  538. Usage: "enable log output",
  539. },
  540. },
  541. Action: rpc.RPCNew,
  542. },
  543. {
  544. Name: "template",
  545. Usage: `generate proto template`,
  546. Flags: []cli.Flag{
  547. cli.StringFlag{
  548. Name: "out, o",
  549. Usage: "the target path of proto",
  550. },
  551. cli.StringFlag{
  552. Name: "home",
  553. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time," +
  554. " if they are, --remote has higher priority",
  555. },
  556. cli.StringFlag{
  557. Name: "remote",
  558. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  559. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  560. "https://github.com/zeromicro/go-zero-template directory structure",
  561. },
  562. cli.StringFlag{
  563. Name: "branch",
  564. Usage: "the branch of the remote repo, it does work with --remote",
  565. },
  566. },
  567. Action: rpc.RPCTemplate,
  568. },
  569. {
  570. Name: "protoc",
  571. Usage: "generate grpc code",
  572. UsageText: `example: goctl rpc protoc xx.proto --go_out=./pb --go-grpc_out=./pb --zrpc_out=.`,
  573. Description: "for details, see https://go-zero.dev/cn/goctl-rpc.html",
  574. Action: rpc.ZRPC,
  575. Flags: []cli.Flag{
  576. cli.StringSliceFlag{
  577. Name: "go_out",
  578. Hidden: true,
  579. },
  580. cli.StringSliceFlag{
  581. Name: "go-grpc_out",
  582. Hidden: true,
  583. },
  584. cli.StringSliceFlag{
  585. Name: "go_opt",
  586. Hidden: true,
  587. },
  588. cli.StringSliceFlag{
  589. Name: "go-grpc_opt",
  590. Hidden: true,
  591. },
  592. cli.StringSliceFlag{
  593. Name: "plugin",
  594. Hidden: true,
  595. },
  596. cli.StringSliceFlag{
  597. Name: "proto_path,I",
  598. Hidden: true,
  599. },
  600. cli.StringFlag{
  601. Name: "zrpc_out",
  602. Usage: "the zrpc output directory",
  603. },
  604. cli.StringFlag{
  605. Name: "style",
  606. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  607. },
  608. cli.StringFlag{
  609. Name: "home",
  610. Usage: "the goctl home path of the template",
  611. },
  612. cli.StringFlag{
  613. Name: "remote",
  614. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  615. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  616. "https://github.com/zeromicro/go-zero-template directory structure",
  617. },
  618. cli.StringFlag{
  619. Name: "branch",
  620. Usage: "the branch of the remote repo, it does work with --remote",
  621. },
  622. cli.BoolFlag{
  623. Name: "verbose, v",
  624. Usage: "enable log output",
  625. },
  626. },
  627. },
  628. },
  629. },
  630. {
  631. Name: "model",
  632. Usage: "generate model code",
  633. Subcommands: []cli.Command{
  634. {
  635. Name: "mysql",
  636. Usage: `generate mysql model`,
  637. Subcommands: []cli.Command{
  638. {
  639. Name: "ddl",
  640. Usage: `generate mysql model from ddl`,
  641. Flags: []cli.Flag{
  642. cli.StringFlag{
  643. Name: "src, s",
  644. Usage: "the path or path globbing patterns of the ddl",
  645. },
  646. cli.StringFlag{
  647. Name: "dir, d",
  648. Usage: "the target dir",
  649. },
  650. cli.StringFlag{
  651. Name: "style",
  652. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  653. },
  654. cli.BoolFlag{
  655. Name: "cache, c",
  656. Usage: "generate code with cache [optional]",
  657. },
  658. cli.BoolFlag{
  659. Name: "idea",
  660. Usage: "for idea plugin [optional]",
  661. },
  662. cli.StringFlag{
  663. Name: "database, db",
  664. Usage: "the name of database [optional]",
  665. },
  666. cli.StringFlag{
  667. Name: "home",
  668. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  669. "if they are, --remote has higher priority",
  670. },
  671. cli.StringFlag{
  672. Name: "remote",
  673. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  674. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  675. "https://github.com/zeromicro/go-zero-template directory structure",
  676. },
  677. cli.StringFlag{
  678. Name: "branch",
  679. Usage: "the branch of the remote repo, it does work with --remote",
  680. },
  681. },
  682. Action: model.MysqlDDL,
  683. },
  684. {
  685. Name: "datasource",
  686. Usage: `generate model from datasource`,
  687. Flags: []cli.Flag{
  688. cli.StringFlag{
  689. Name: "url",
  690. Usage: `the data source of database,like "root:password@tcp(127.0.0.1:3306)/database"`,
  691. },
  692. cli.StringFlag{
  693. Name: "table, t",
  694. Usage: `the table or table globbing patterns in the database`,
  695. },
  696. cli.BoolFlag{
  697. Name: "cache, c",
  698. Usage: "generate code with cache [optional]",
  699. },
  700. cli.StringFlag{
  701. Name: "dir, d",
  702. Usage: "the target dir",
  703. },
  704. cli.StringFlag{
  705. Name: "style",
  706. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  707. },
  708. cli.BoolFlag{
  709. Name: "idea",
  710. Usage: "for idea plugin [optional]",
  711. },
  712. cli.StringFlag{
  713. Name: "home",
  714. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  715. "if they are, --remote has higher priority",
  716. },
  717. cli.StringFlag{
  718. Name: "remote",
  719. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  720. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  721. "https://github.com/zeromicro/go-zero-template directory structure",
  722. },
  723. cli.StringFlag{
  724. Name: "branch",
  725. Usage: "the branch of the remote repo, it does work with --remote",
  726. },
  727. },
  728. Action: model.MySqlDataSource,
  729. },
  730. },
  731. },
  732. {
  733. Name: "pg",
  734. Usage: `generate postgresql model`,
  735. Subcommands: []cli.Command{
  736. {
  737. Name: "datasource",
  738. Usage: `generate model from datasource`,
  739. Flags: []cli.Flag{
  740. cli.StringFlag{
  741. Name: "url",
  742. Usage: `the data source of database,like "postgres://root:password@127.0.0.1:5432/database?sslmode=disable"`,
  743. },
  744. cli.StringFlag{
  745. Name: "table, t",
  746. Usage: `the table or table globbing patterns in the database`,
  747. },
  748. cli.StringFlag{
  749. Name: "schema, s",
  750. Usage: `the table schema, default is [public]`,
  751. },
  752. cli.BoolFlag{
  753. Name: "cache, c",
  754. Usage: "generate code with cache [optional]",
  755. },
  756. cli.StringFlag{
  757. Name: "dir, d",
  758. Usage: "the target dir",
  759. },
  760. cli.StringFlag{
  761. Name: "style",
  762. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  763. },
  764. cli.BoolFlag{
  765. Name: "idea",
  766. Usage: "for idea plugin [optional]",
  767. },
  768. cli.StringFlag{
  769. Name: "home",
  770. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  771. "if they are, --remote has higher priority",
  772. },
  773. cli.StringFlag{
  774. Name: "remote",
  775. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  776. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  777. "https://github.com/zeromicro/go-zero-template directory structure",
  778. },
  779. cli.StringFlag{
  780. Name: "branch",
  781. Usage: "the branch of the remote repo, it does work with --remote",
  782. },
  783. },
  784. Action: model.PostgreSqlDataSource,
  785. },
  786. },
  787. },
  788. {
  789. Name: "mongo",
  790. Usage: `generate mongo model`,
  791. Flags: []cli.Flag{
  792. cli.StringSliceFlag{
  793. Name: "type, t",
  794. Usage: "specified model type name",
  795. },
  796. cli.BoolFlag{
  797. Name: "cache, c",
  798. Usage: "generate code with cache [optional]",
  799. },
  800. cli.StringFlag{
  801. Name: "dir, d",
  802. Usage: "the target dir",
  803. },
  804. cli.StringFlag{
  805. Name: "style",
  806. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  807. },
  808. cli.StringFlag{
  809. Name: "home",
  810. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time," +
  811. " if they are, --remote has higher priority",
  812. },
  813. cli.StringFlag{
  814. Name: "remote",
  815. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  816. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  817. "https://github.com/zeromicro/go-zero-template directory structure",
  818. },
  819. cli.StringFlag{
  820. Name: "branch",
  821. Usage: "the branch of the remote repo, it does work with --remote",
  822. },
  823. },
  824. Action: mongo.Action,
  825. },
  826. },
  827. },
  828. {
  829. Name: "template",
  830. Usage: "template operation",
  831. Subcommands: []cli.Command{
  832. {
  833. Name: "init",
  834. Usage: "initialize the all templates(force update)",
  835. Flags: []cli.Flag{
  836. cli.StringFlag{
  837. Name: "home",
  838. Usage: "the goctl home path of the template",
  839. },
  840. },
  841. Action: tpl.GenTemplates,
  842. },
  843. {
  844. Name: "clean",
  845. Usage: "clean the all cache templates",
  846. Flags: []cli.Flag{
  847. cli.StringFlag{
  848. Name: "home",
  849. Usage: "the goctl home path of the template",
  850. },
  851. },
  852. Action: tpl.CleanTemplates,
  853. },
  854. {
  855. Name: "update",
  856. Usage: "update template of the target category to the latest",
  857. Flags: []cli.Flag{
  858. cli.StringFlag{
  859. Name: "category,c",
  860. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  861. },
  862. cli.StringFlag{
  863. Name: "home",
  864. Usage: "the goctl home path of the template",
  865. },
  866. },
  867. Action: tpl.UpdateTemplates,
  868. },
  869. {
  870. Name: "revert",
  871. Usage: "revert the target template to the latest",
  872. Flags: []cli.Flag{
  873. cli.StringFlag{
  874. Name: "category,c",
  875. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  876. },
  877. cli.StringFlag{
  878. Name: "name,n",
  879. Usage: "the target file name of template",
  880. },
  881. cli.StringFlag{
  882. Name: "home",
  883. Usage: "the goctl home path of the template",
  884. },
  885. },
  886. Action: tpl.RevertTemplates,
  887. },
  888. },
  889. },
  890. {
  891. Name: "completion",
  892. Usage: "generation completion script, it only works for unix-like OS",
  893. Action: completion.Completion,
  894. Flags: []cli.Flag{
  895. cli.StringFlag{
  896. Name: "name, n",
  897. Usage: "the filename of auto complete script, default is [goctl_autocomplete]",
  898. },
  899. },
  900. },
  901. }
  902. func main() {
  903. logx.Disable()
  904. load.Disable()
  905. cli.BashCompletionFlag = cli.BoolFlag{
  906. Name: completion.BashCompletionFlag,
  907. Hidden: true,
  908. }
  909. app := cli.NewApp()
  910. app.EnableBashCompletion = true
  911. app.Usage = "a cli tool to generate code"
  912. app.Version = fmt.Sprintf("%s %s/%s", version.BuildVersion, runtime.GOOS, runtime.GOARCH)
  913. app.Commands = commands
  914. // cli already print error messages.
  915. if err := app.Run(os.Args); err != nil {
  916. fmt.Println(aurora.Red(err.Error()))
  917. os.Exit(codeFailure)
  918. }
  919. }