goctl.go 25 KB

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