goctl.go 27 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/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. },
  160. Action: format.GoFormatApi,
  161. },
  162. {
  163. Name: "validate",
  164. Usage: "validate api file",
  165. Flags: []cli.Flag{
  166. cli.StringFlag{
  167. Name: "api",
  168. Usage: "validate target api file",
  169. },
  170. },
  171. Action: validate.GoValidateApi,
  172. },
  173. {
  174. Name: "doc",
  175. Usage: "generate doc files",
  176. Flags: []cli.Flag{
  177. cli.StringFlag{
  178. Name: "dir",
  179. Usage: "the target dir",
  180. },
  181. cli.StringFlag{
  182. Name: "o",
  183. Required: false,
  184. Usage: "the output markdown directory",
  185. },
  186. },
  187. Action: docgen.DocCommand,
  188. },
  189. {
  190. Name: "go",
  191. Usage: "generate go files for provided api in yaml file",
  192. Flags: []cli.Flag{
  193. cli.StringFlag{
  194. Name: "dir",
  195. Usage: "the target dir",
  196. },
  197. cli.StringFlag{
  198. Name: "api",
  199. Usage: "the api file",
  200. },
  201. cli.StringFlag{
  202. Name: "style",
  203. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  204. },
  205. cli.StringFlag{
  206. Name: "home",
  207. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  208. "if they are, --remote has higher priority",
  209. },
  210. cli.StringFlag{
  211. Name: "remote",
  212. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  213. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  214. "https://github.com/zeromicro/go-zero-template directory structure",
  215. },
  216. cli.StringFlag{
  217. Name: "branch",
  218. Usage: "the branch of the remote repo, it does work with --remote",
  219. },
  220. },
  221. Action: gogen.GoCommand,
  222. },
  223. {
  224. Name: "java",
  225. Usage: "generate java files for provided api in api file",
  226. Flags: []cli.Flag{
  227. cli.StringFlag{
  228. Name: "dir",
  229. Usage: "the target dir",
  230. },
  231. cli.StringFlag{
  232. Name: "api",
  233. Usage: "the api file",
  234. },
  235. },
  236. Action: javagen.JavaCommand,
  237. },
  238. {
  239. Name: "ts",
  240. Usage: "generate ts files for provided api in api file",
  241. Flags: []cli.Flag{
  242. cli.StringFlag{
  243. Name: "dir",
  244. Usage: "the target dir",
  245. },
  246. cli.StringFlag{
  247. Name: "api",
  248. Usage: "the api file",
  249. },
  250. cli.StringFlag{
  251. Name: "webapi",
  252. Usage: "the web api file path",
  253. },
  254. cli.StringFlag{
  255. Name: "caller",
  256. Usage: "the web api caller",
  257. },
  258. cli.BoolFlag{
  259. Name: "unwrap",
  260. Usage: "unwrap the webapi caller for import",
  261. },
  262. },
  263. Action: tsgen.TsCommand,
  264. },
  265. {
  266. Name: "dart",
  267. Usage: "generate dart files for provided api in api file",
  268. Flags: []cli.Flag{
  269. cli.StringFlag{
  270. Name: "dir",
  271. Usage: "the target dir",
  272. },
  273. cli.StringFlag{
  274. Name: "api",
  275. Usage: "the api file",
  276. },
  277. cli.BoolFlag{
  278. Name: "legacy",
  279. Usage: "legacy generator for flutter v1",
  280. },
  281. cli.StringFlag{
  282. Name: "hostname",
  283. Usage: "hostname of the server",
  284. },
  285. },
  286. Action: dartgen.DartCommand,
  287. },
  288. {
  289. Name: "kt",
  290. Usage: "generate kotlin code for provided api file",
  291. Flags: []cli.Flag{
  292. cli.StringFlag{
  293. Name: "dir",
  294. Usage: "the target directory",
  295. },
  296. cli.StringFlag{
  297. Name: "api",
  298. Usage: "the api file",
  299. },
  300. cli.StringFlag{
  301. Name: "pkg",
  302. Usage: "define package name for kotlin file",
  303. },
  304. },
  305. Action: ktgen.KtCommand,
  306. },
  307. {
  308. Name: "plugin",
  309. Usage: "custom file generator",
  310. Flags: []cli.Flag{
  311. cli.StringFlag{
  312. Name: "plugin, p",
  313. Usage: "the plugin file",
  314. },
  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: "style",
  325. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  326. },
  327. },
  328. Action: plugin.PluginCommand,
  329. },
  330. },
  331. },
  332. {
  333. Name: "docker",
  334. Usage: "generate Dockerfile",
  335. Flags: []cli.Flag{
  336. cli.StringFlag{
  337. Name: "go",
  338. Usage: "the file that contains main function",
  339. },
  340. cli.IntFlag{
  341. Name: "port",
  342. Usage: "the port to expose, default none",
  343. Value: 0,
  344. },
  345. cli.StringFlag{
  346. Name: "home",
  347. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  348. "if they are, --remote has higher priority",
  349. },
  350. cli.StringFlag{
  351. Name: "remote",
  352. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  353. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  354. "https://github.com/zeromicro/go-zero-template directory structure",
  355. },
  356. cli.StringFlag{
  357. Name: "branch",
  358. Usage: "the branch of the remote repo, it does work with --remote",
  359. },
  360. cli.StringFlag{
  361. Name: "version",
  362. Usage: "the goctl builder golang image version",
  363. },
  364. },
  365. Action: docker.DockerCommand,
  366. },
  367. {
  368. Name: "kube",
  369. Usage: "generate kubernetes files",
  370. Subcommands: []cli.Command{
  371. {
  372. Name: "deploy",
  373. Usage: "generate deployment yaml file",
  374. Flags: []cli.Flag{
  375. cli.StringFlag{
  376. Name: "name",
  377. Usage: "the name of deployment",
  378. Required: true,
  379. },
  380. cli.StringFlag{
  381. Name: "namespace",
  382. Usage: "the namespace of deployment",
  383. Required: true,
  384. },
  385. cli.StringFlag{
  386. Name: "image",
  387. Usage: "the docker image of deployment",
  388. Required: true,
  389. },
  390. cli.StringFlag{
  391. Name: "secret",
  392. Usage: "the secret to image pull from registry",
  393. },
  394. cli.IntFlag{
  395. Name: "requestCpu",
  396. Usage: "the request cpu to deploy",
  397. Value: 500,
  398. },
  399. cli.IntFlag{
  400. Name: "requestMem",
  401. Usage: "the request memory to deploy",
  402. Value: 512,
  403. },
  404. cli.IntFlag{
  405. Name: "limitCpu",
  406. Usage: "the limit cpu to deploy",
  407. Value: 1000,
  408. },
  409. cli.IntFlag{
  410. Name: "limitMem",
  411. Usage: "the limit memory to deploy",
  412. Value: 1024,
  413. },
  414. cli.StringFlag{
  415. Name: "o",
  416. Usage: "the output yaml file",
  417. Required: true,
  418. },
  419. cli.IntFlag{
  420. Name: "replicas",
  421. Usage: "the number of replicas to deploy",
  422. Value: 3,
  423. },
  424. cli.IntFlag{
  425. Name: "revisions",
  426. Usage: "the number of revision history to limit",
  427. Value: 5,
  428. },
  429. cli.IntFlag{
  430. Name: "port",
  431. Usage: "the port of the deployment to listen on pod",
  432. Required: true,
  433. },
  434. cli.IntFlag{
  435. Name: "nodePort",
  436. Usage: "the nodePort of the deployment to expose",
  437. Value: 0,
  438. },
  439. cli.IntFlag{
  440. Name: "minReplicas",
  441. Usage: "the min replicas to deploy",
  442. Value: 3,
  443. },
  444. cli.IntFlag{
  445. Name: "maxReplicas",
  446. Usage: "the max replicas of deploy",
  447. Value: 10,
  448. },
  449. cli.StringFlag{
  450. Name: "home",
  451. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  452. "if they are, --remote has higher priority",
  453. },
  454. cli.StringFlag{
  455. Name: "remote",
  456. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  457. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  458. "https://github.com/zeromicro/go-zero-template directory structure",
  459. },
  460. cli.StringFlag{
  461. Name: "branch",
  462. Usage: "the branch of the remote repo, it does work with --remote",
  463. },
  464. cli.StringFlag{
  465. Name: "serviceAccount",
  466. Usage: "the ServiceAccount for the deployment",
  467. },
  468. },
  469. Action: kube.DeploymentCommand,
  470. },
  471. },
  472. },
  473. {
  474. Name: "rpc",
  475. Usage: "generate rpc code",
  476. Subcommands: []cli.Command{
  477. {
  478. Name: "new",
  479. Usage: `generate rpc demo service`,
  480. Description: aurora.Yellow(`deprecated: zrpc code generation use "goctl rpc protoc" instead, for the details see "goctl rpc protoc --help"`).String(),
  481. Flags: []cli.Flag{
  482. cli.StringFlag{
  483. Name: "style",
  484. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  485. },
  486. cli.BoolFlag{
  487. Name: "idea",
  488. Usage: "whether the command execution environment is from idea plugin. [optional]",
  489. },
  490. cli.StringFlag{
  491. Name: "home",
  492. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  493. "if they are, --remote has higher priority",
  494. },
  495. cli.StringFlag{
  496. Name: "remote",
  497. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  498. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  499. "https://github.com/zeromicro/go-zero-template directory structure",
  500. },
  501. cli.StringFlag{
  502. Name: "branch",
  503. Usage: "the branch of the remote repo, it does work with --remote",
  504. },
  505. },
  506. Action: rpc.RPCNew,
  507. },
  508. {
  509. Name: "template",
  510. Usage: `generate proto template`,
  511. Flags: []cli.Flag{
  512. cli.StringFlag{
  513. Name: "out, o",
  514. Usage: "the target path of proto",
  515. },
  516. cli.StringFlag{
  517. Name: "home",
  518. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time," +
  519. " if they are, --remote has higher priority",
  520. },
  521. cli.StringFlag{
  522. Name: "remote",
  523. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  524. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  525. "https://github.com/zeromicro/go-zero-template directory structure",
  526. },
  527. cli.StringFlag{
  528. Name: "branch",
  529. Usage: "the branch of the remote repo, it does work with --remote",
  530. },
  531. },
  532. Action: rpc.RPCTemplate,
  533. },
  534. {
  535. Name: "protoc",
  536. Usage: "generate grpc code",
  537. UsageText: `example: goctl rpc protoc xx.proto --go_out=./pb --go-grpc_out=./pb --zrpc_out=.`,
  538. Description: "for details, see https://go-zero.dev/cn/goctl-rpc.html",
  539. Action: rpc.ZRPC,
  540. Flags: []cli.Flag{
  541. cli.StringFlag{
  542. Name: "go_out",
  543. Hidden: true,
  544. },
  545. cli.StringFlag{
  546. Name: "go-grpc_out",
  547. Hidden: true,
  548. },
  549. cli.StringFlag{
  550. Name: "go_opt",
  551. Hidden: true,
  552. },
  553. cli.StringFlag{
  554. Name: "go-grpc_opt",
  555. Hidden: true,
  556. },
  557. cli.StringFlag{
  558. Name: "zrpc_out",
  559. Usage: "the zrpc output directory",
  560. },
  561. cli.StringFlag{
  562. Name: "style",
  563. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  564. },
  565. cli.StringFlag{
  566. Name: "home",
  567. Usage: "the goctl home path of the template",
  568. },
  569. cli.StringFlag{
  570. Name: "remote",
  571. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  572. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  573. "https://github.com/zeromicro/go-zero-template directory structure",
  574. },
  575. cli.StringFlag{
  576. Name: "branch",
  577. Usage: "the branch of the remote repo, it does work with --remote",
  578. },
  579. },
  580. },
  581. {
  582. Name: "proto",
  583. Usage: `generate rpc from proto`,
  584. Description: aurora.Yellow(`deprecated: zrpc code generation use "goctl rpc protoc" instead, for the details see "goctl rpc protoc --help"`).String(),
  585. Flags: []cli.Flag{
  586. cli.StringFlag{
  587. Name: "src, s",
  588. Usage: "the file path of the proto source file",
  589. },
  590. cli.StringSliceFlag{
  591. Name: "proto_path, I",
  592. Usage: `native command of protoc, specify the directory in which to search for imports. [optional]`,
  593. },
  594. cli.StringSliceFlag{
  595. Name: "go_opt",
  596. Usage: `native command of protoc-gen-go, specify the mapping from proto to go, eg --go_opt=proto_import=go_package_import. [optional]`,
  597. },
  598. cli.StringFlag{
  599. Name: "dir, d",
  600. Usage: `the target path of the code`,
  601. },
  602. cli.StringFlag{
  603. Name: "style",
  604. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  605. },
  606. cli.BoolFlag{
  607. Name: "idea",
  608. Usage: "whether the command execution environment is from idea plugin. [optional]",
  609. },
  610. cli.StringFlag{
  611. Name: "home",
  612. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  613. "if they are, --remote has higher priority",
  614. },
  615. cli.StringFlag{
  616. Name: "remote",
  617. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  618. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  619. "https://github.com/zeromicro/go-zero-template directory structure",
  620. },
  621. cli.StringFlag{
  622. Name: "branch",
  623. Usage: "the branch of the remote repo, it does work with --remote",
  624. },
  625. },
  626. Action: rpc.RPC,
  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(errorx.Wrap(err).Error()))
  917. os.Exit(codeFailure)
  918. }
  919. }