goctl.go 25 KB

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