goctl.go 26 KB

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