goctl.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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. cli.StringFlag{
  365. Name: "tz",
  366. Usage: "the timezone of the container",
  367. Value: "Asia/Shanghai",
  368. },
  369. },
  370. Action: docker.DockerCommand,
  371. },
  372. {
  373. Name: "kube",
  374. Usage: "generate kubernetes files",
  375. Subcommands: []cli.Command{
  376. {
  377. Name: "deploy",
  378. Usage: "generate deployment yaml file",
  379. Flags: []cli.Flag{
  380. cli.StringFlag{
  381. Name: "name",
  382. Usage: "the name of deployment",
  383. Required: true,
  384. },
  385. cli.StringFlag{
  386. Name: "namespace",
  387. Usage: "the namespace of deployment",
  388. Required: true,
  389. },
  390. cli.StringFlag{
  391. Name: "image",
  392. Usage: "the docker image of deployment",
  393. Required: true,
  394. },
  395. cli.StringFlag{
  396. Name: "secret",
  397. Usage: "the secret to image pull from registry",
  398. },
  399. cli.IntFlag{
  400. Name: "requestCpu",
  401. Usage: "the request cpu to deploy",
  402. Value: 500,
  403. },
  404. cli.IntFlag{
  405. Name: "requestMem",
  406. Usage: "the request memory to deploy",
  407. Value: 512,
  408. },
  409. cli.IntFlag{
  410. Name: "limitCpu",
  411. Usage: "the limit cpu to deploy",
  412. Value: 1000,
  413. },
  414. cli.IntFlag{
  415. Name: "limitMem",
  416. Usage: "the limit memory to deploy",
  417. Value: 1024,
  418. },
  419. cli.StringFlag{
  420. Name: "o",
  421. Usage: "the output yaml file",
  422. Required: true,
  423. },
  424. cli.IntFlag{
  425. Name: "replicas",
  426. Usage: "the number of replicas to deploy",
  427. Value: 3,
  428. },
  429. cli.IntFlag{
  430. Name: "revisions",
  431. Usage: "the number of revision history to limit",
  432. Value: 5,
  433. },
  434. cli.IntFlag{
  435. Name: "port",
  436. Usage: "the port of the deployment to listen on pod",
  437. Required: true,
  438. },
  439. cli.IntFlag{
  440. Name: "nodePort",
  441. Usage: "the nodePort of the deployment to expose",
  442. Value: 0,
  443. },
  444. cli.IntFlag{
  445. Name: "minReplicas",
  446. Usage: "the min replicas to deploy",
  447. Value: 3,
  448. },
  449. cli.IntFlag{
  450. Name: "maxReplicas",
  451. Usage: "the max replicas of deploy",
  452. Value: 10,
  453. },
  454. cli.StringFlag{
  455. Name: "home",
  456. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  457. "if they are, --remote has higher priority",
  458. },
  459. cli.StringFlag{
  460. Name: "remote",
  461. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  462. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  463. "https://github.com/zeromicro/go-zero-template directory structure",
  464. },
  465. cli.StringFlag{
  466. Name: "branch",
  467. Usage: "the branch of the remote repo, it does work with --remote",
  468. },
  469. cli.StringFlag{
  470. Name: "serviceAccount",
  471. Usage: "the ServiceAccount for the deployment",
  472. },
  473. },
  474. Action: kube.DeploymentCommand,
  475. },
  476. },
  477. },
  478. {
  479. Name: "rpc",
  480. Usage: "generate rpc code",
  481. Subcommands: []cli.Command{
  482. {
  483. Name: "new",
  484. Usage: `generate rpc demo service`,
  485. Description: aurora.Yellow(`deprecated: zrpc code generation use "goctl rpc protoc" instead, for the details see "goctl rpc protoc --help"`).String(),
  486. Flags: []cli.Flag{
  487. cli.StringFlag{
  488. Name: "style",
  489. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  490. },
  491. cli.BoolFlag{
  492. Name: "idea",
  493. Usage: "whether the command execution environment is from idea plugin. [optional]",
  494. },
  495. cli.StringFlag{
  496. Name: "home",
  497. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  498. "if they are, --remote has higher priority",
  499. },
  500. cli.StringFlag{
  501. Name: "remote",
  502. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  503. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  504. "https://github.com/zeromicro/go-zero-template directory structure",
  505. },
  506. cli.StringFlag{
  507. Name: "branch",
  508. Usage: "the branch of the remote repo, it does work with --remote",
  509. },
  510. },
  511. Action: rpc.RPCNew,
  512. },
  513. {
  514. Name: "template",
  515. Usage: `generate proto template`,
  516. Flags: []cli.Flag{
  517. cli.StringFlag{
  518. Name: "out, o",
  519. Usage: "the target path of proto",
  520. },
  521. cli.StringFlag{
  522. Name: "home",
  523. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time," +
  524. " if they are, --remote has higher priority",
  525. },
  526. cli.StringFlag{
  527. Name: "remote",
  528. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  529. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  530. "https://github.com/zeromicro/go-zero-template directory structure",
  531. },
  532. cli.StringFlag{
  533. Name: "branch",
  534. Usage: "the branch of the remote repo, it does work with --remote",
  535. },
  536. },
  537. Action: rpc.RPCTemplate,
  538. },
  539. {
  540. Name: "protoc",
  541. Usage: "generate grpc code",
  542. UsageText: `example: goctl rpc protoc xx.proto --go_out=./pb --go-grpc_out=./pb --zrpc_out=.`,
  543. Description: "for details, see https://go-zero.dev/cn/goctl-rpc.html",
  544. Action: rpc.ZRPC,
  545. Flags: []cli.Flag{
  546. cli.StringSliceFlag{
  547. Name: "go_out",
  548. Hidden: true,
  549. },
  550. cli.StringSliceFlag{
  551. Name: "go-grpc_out",
  552. Hidden: true,
  553. },
  554. cli.StringSliceFlag{
  555. Name: "go_opt",
  556. Hidden: true,
  557. },
  558. cli.StringSliceFlag{
  559. Name: "go-grpc_opt",
  560. Hidden: true,
  561. },
  562. cli.StringSliceFlag{
  563. Name: "plugin",
  564. Hidden: true,
  565. },
  566. cli.StringSliceFlag{
  567. Name: "proto_path,I",
  568. Hidden: true,
  569. },
  570. cli.StringFlag{
  571. Name: "zrpc_out",
  572. Usage: "the zrpc output directory",
  573. },
  574. cli.StringFlag{
  575. Name: "style",
  576. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  577. },
  578. cli.StringFlag{
  579. Name: "home",
  580. Usage: "the goctl home path of the template",
  581. },
  582. cli.StringFlag{
  583. Name: "remote",
  584. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  585. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  586. "https://github.com/zeromicro/go-zero-template directory structure",
  587. },
  588. cli.StringFlag{
  589. Name: "branch",
  590. Usage: "the branch of the remote repo, it does work with --remote",
  591. },
  592. },
  593. },
  594. {
  595. Name: "proto",
  596. Usage: `generate rpc from proto`,
  597. Description: aurora.Yellow(`deprecated: zrpc code generation use "goctl rpc protoc" instead, for the details see "goctl rpc protoc --help"`).String(),
  598. Flags: []cli.Flag{
  599. cli.StringFlag{
  600. Name: "src, s",
  601. Usage: "the file path of the proto source file",
  602. },
  603. cli.StringSliceFlag{
  604. Name: "proto_path, I",
  605. Usage: `native command of protoc, specify the directory in which to search for imports. [optional]`,
  606. },
  607. cli.StringSliceFlag{
  608. Name: "go_opt",
  609. Usage: `native command of protoc-gen-go, specify the mapping from proto to go, eg --go_opt=proto_import=go_package_import. [optional]`,
  610. },
  611. cli.StringFlag{
  612. Name: "dir, d",
  613. Usage: `the target path of the code`,
  614. },
  615. cli.StringFlag{
  616. Name: "style",
  617. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  618. },
  619. cli.BoolFlag{
  620. Name: "idea",
  621. Usage: "whether the command execution environment is from idea plugin. [optional]",
  622. },
  623. cli.StringFlag{
  624. Name: "home",
  625. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  626. "if they are, --remote has higher priority",
  627. },
  628. cli.StringFlag{
  629. Name: "remote",
  630. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  631. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  632. "https://github.com/zeromicro/go-zero-template directory structure",
  633. },
  634. cli.StringFlag{
  635. Name: "branch",
  636. Usage: "the branch of the remote repo, it does work with --remote",
  637. },
  638. },
  639. Action: rpc.RPC,
  640. },
  641. },
  642. },
  643. {
  644. Name: "model",
  645. Usage: "generate model code",
  646. Subcommands: []cli.Command{
  647. {
  648. Name: "mysql",
  649. Usage: `generate mysql model`,
  650. Subcommands: []cli.Command{
  651. {
  652. Name: "ddl",
  653. Usage: `generate mysql model from ddl`,
  654. Flags: []cli.Flag{
  655. cli.StringFlag{
  656. Name: "src, s",
  657. Usage: "the path or path globbing patterns of the ddl",
  658. },
  659. cli.StringFlag{
  660. Name: "dir, d",
  661. Usage: "the target dir",
  662. },
  663. cli.StringFlag{
  664. Name: "style",
  665. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  666. },
  667. cli.BoolFlag{
  668. Name: "cache, c",
  669. Usage: "generate code with cache [optional]",
  670. },
  671. cli.BoolFlag{
  672. Name: "idea",
  673. Usage: "for idea plugin [optional]",
  674. },
  675. cli.StringFlag{
  676. Name: "database, db",
  677. Usage: "the name of database [optional]",
  678. },
  679. cli.StringFlag{
  680. Name: "home",
  681. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  682. "if they are, --remote has higher priority",
  683. },
  684. cli.StringFlag{
  685. Name: "remote",
  686. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  687. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  688. "https://github.com/zeromicro/go-zero-template directory structure",
  689. },
  690. cli.StringFlag{
  691. Name: "branch",
  692. Usage: "the branch of the remote repo, it does work with --remote",
  693. },
  694. },
  695. Action: model.MysqlDDL,
  696. },
  697. {
  698. Name: "datasource",
  699. Usage: `generate model from datasource`,
  700. Flags: []cli.Flag{
  701. cli.StringFlag{
  702. Name: "url",
  703. Usage: `the data source of database,like "root:password@tcp(127.0.0.1:3306)/database"`,
  704. },
  705. cli.StringFlag{
  706. Name: "table, t",
  707. Usage: `the table or table globbing patterns in the database`,
  708. },
  709. cli.BoolFlag{
  710. Name: "cache, c",
  711. Usage: "generate code with cache [optional]",
  712. },
  713. cli.StringFlag{
  714. Name: "dir, d",
  715. Usage: "the target dir",
  716. },
  717. cli.StringFlag{
  718. Name: "style",
  719. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  720. },
  721. cli.BoolFlag{
  722. Name: "idea",
  723. Usage: "for idea plugin [optional]",
  724. },
  725. cli.StringFlag{
  726. Name: "home",
  727. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  728. "if they are, --remote has higher priority",
  729. },
  730. cli.StringFlag{
  731. Name: "remote",
  732. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  733. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  734. "https://github.com/zeromicro/go-zero-template directory structure",
  735. },
  736. cli.StringFlag{
  737. Name: "branch",
  738. Usage: "the branch of the remote repo, it does work with --remote",
  739. },
  740. },
  741. Action: model.MySqlDataSource,
  742. },
  743. },
  744. },
  745. {
  746. Name: "pg",
  747. Usage: `generate postgresql model`,
  748. Subcommands: []cli.Command{
  749. {
  750. Name: "datasource",
  751. Usage: `generate model from datasource`,
  752. Flags: []cli.Flag{
  753. cli.StringFlag{
  754. Name: "url",
  755. Usage: `the data source of database,like "postgres://root:password@127.0.0.1:5432/database?sslmode=disable"`,
  756. },
  757. cli.StringFlag{
  758. Name: "table, t",
  759. Usage: `the table or table globbing patterns in the database`,
  760. },
  761. cli.StringFlag{
  762. Name: "schema, s",
  763. Usage: `the table schema, default is [public]`,
  764. },
  765. cli.BoolFlag{
  766. Name: "cache, c",
  767. Usage: "generate code with cache [optional]",
  768. },
  769. cli.StringFlag{
  770. Name: "dir, d",
  771. Usage: "the target dir",
  772. },
  773. cli.StringFlag{
  774. Name: "style",
  775. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  776. },
  777. cli.BoolFlag{
  778. Name: "idea",
  779. Usage: "for idea plugin [optional]",
  780. },
  781. cli.StringFlag{
  782. Name: "home",
  783. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
  784. "if they are, --remote has higher priority",
  785. },
  786. cli.StringFlag{
  787. Name: "remote",
  788. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  789. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  790. "https://github.com/zeromicro/go-zero-template directory structure",
  791. },
  792. cli.StringFlag{
  793. Name: "branch",
  794. Usage: "the branch of the remote repo, it does work with --remote",
  795. },
  796. },
  797. Action: model.PostgreSqlDataSource,
  798. },
  799. },
  800. },
  801. {
  802. Name: "mongo",
  803. Usage: `generate mongo model`,
  804. Flags: []cli.Flag{
  805. cli.StringSliceFlag{
  806. Name: "type, t",
  807. Usage: "specified model type name",
  808. },
  809. cli.BoolFlag{
  810. Name: "cache, c",
  811. Usage: "generate code with cache [optional]",
  812. },
  813. cli.StringFlag{
  814. Name: "dir, d",
  815. Usage: "the target dir",
  816. },
  817. cli.StringFlag{
  818. Name: "style",
  819. Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]",
  820. },
  821. cli.StringFlag{
  822. Name: "home",
  823. Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time," +
  824. " if they are, --remote has higher priority",
  825. },
  826. cli.StringFlag{
  827. Name: "remote",
  828. Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
  829. "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " +
  830. "https://github.com/zeromicro/go-zero-template directory structure",
  831. },
  832. cli.StringFlag{
  833. Name: "branch",
  834. Usage: "the branch of the remote repo, it does work with --remote",
  835. },
  836. },
  837. Action: mongo.Action,
  838. },
  839. },
  840. },
  841. {
  842. Name: "template",
  843. Usage: "template operation",
  844. Subcommands: []cli.Command{
  845. {
  846. Name: "init",
  847. Usage: "initialize the all templates(force update)",
  848. Flags: []cli.Flag{
  849. cli.StringFlag{
  850. Name: "home",
  851. Usage: "the goctl home path of the template",
  852. },
  853. },
  854. Action: tpl.GenTemplates,
  855. },
  856. {
  857. Name: "clean",
  858. Usage: "clean the all cache templates",
  859. Flags: []cli.Flag{
  860. cli.StringFlag{
  861. Name: "home",
  862. Usage: "the goctl home path of the template",
  863. },
  864. },
  865. Action: tpl.CleanTemplates,
  866. },
  867. {
  868. Name: "update",
  869. Usage: "update template of the target category to the latest",
  870. Flags: []cli.Flag{
  871. cli.StringFlag{
  872. Name: "category,c",
  873. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  874. },
  875. cli.StringFlag{
  876. Name: "home",
  877. Usage: "the goctl home path of the template",
  878. },
  879. },
  880. Action: tpl.UpdateTemplates,
  881. },
  882. {
  883. Name: "revert",
  884. Usage: "revert the target template to the latest",
  885. Flags: []cli.Flag{
  886. cli.StringFlag{
  887. Name: "category,c",
  888. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  889. },
  890. cli.StringFlag{
  891. Name: "name,n",
  892. Usage: "the target file name of template",
  893. },
  894. cli.StringFlag{
  895. Name: "home",
  896. Usage: "the goctl home path of the template",
  897. },
  898. },
  899. Action: tpl.RevertTemplates,
  900. },
  901. },
  902. },
  903. {
  904. Name: "completion",
  905. Usage: "generation completion script, it only works for unix-like OS",
  906. Action: completion.Completion,
  907. Flags: []cli.Flag{
  908. cli.StringFlag{
  909. Name: "name, n",
  910. Usage: "the filename of auto complete script, default is [goctl_autocomplete]",
  911. },
  912. },
  913. },
  914. }
  915. func main() {
  916. logx.Disable()
  917. load.Disable()
  918. cli.BashCompletionFlag = cli.BoolFlag{
  919. Name: completion.BashCompletionFlag,
  920. Hidden: true,
  921. }
  922. app := cli.NewApp()
  923. app.EnableBashCompletion = true
  924. app.Usage = "a cli tool to generate code"
  925. app.Version = fmt.Sprintf("%s %s/%s", version.BuildVersion, runtime.GOOS, runtime.GOARCH)
  926. app.Commands = commands
  927. // cli already print error messages.
  928. if err := app.Run(os.Args); err != nil {
  929. fmt.Println(aurora.Red(errorx.Wrap(err).Error()))
  930. os.Exit(codeFailure)
  931. }
  932. }