goctl.go 27 KB

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