goctl.go 26 KB

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