goctl.go 25 KB

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