goctl.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "runtime"
  6. "github.com/logrusorgru/aurora"
  7. "github.com/tal-tech/go-zero/core/load"
  8. "github.com/tal-tech/go-zero/core/logx"
  9. "github.com/tal-tech/go-zero/core/stat"
  10. "github.com/tal-tech/go-zero/tools/goctl/api/apigen"
  11. "github.com/tal-tech/go-zero/tools/goctl/api/dartgen"
  12. "github.com/tal-tech/go-zero/tools/goctl/api/docgen"
  13. "github.com/tal-tech/go-zero/tools/goctl/api/format"
  14. "github.com/tal-tech/go-zero/tools/goctl/api/gogen"
  15. "github.com/tal-tech/go-zero/tools/goctl/api/javagen"
  16. "github.com/tal-tech/go-zero/tools/goctl/api/ktgen"
  17. "github.com/tal-tech/go-zero/tools/goctl/api/new"
  18. "github.com/tal-tech/go-zero/tools/goctl/api/tsgen"
  19. "github.com/tal-tech/go-zero/tools/goctl/api/validate"
  20. "github.com/tal-tech/go-zero/tools/goctl/configgen"
  21. "github.com/tal-tech/go-zero/tools/goctl/docker"
  22. "github.com/tal-tech/go-zero/tools/goctl/kube"
  23. "github.com/tal-tech/go-zero/tools/goctl/model/mongo"
  24. model "github.com/tal-tech/go-zero/tools/goctl/model/sql/command"
  25. "github.com/tal-tech/go-zero/tools/goctl/plugin"
  26. rpc "github.com/tal-tech/go-zero/tools/goctl/rpc/cli"
  27. "github.com/tal-tech/go-zero/tools/goctl/tpl"
  28. "github.com/tal-tech/go-zero/tools/goctl/upgrade"
  29. "github.com/urfave/cli"
  30. )
  31. var (
  32. buildVersion = "1.1.9-pre"
  33. commands = []cli.Command{
  34. {
  35. Name: "upgrade",
  36. Usage: "upgrade goctl to latest version",
  37. Action: upgrade.Upgrade,
  38. },
  39. {
  40. Name: "api",
  41. Usage: "generate api related files",
  42. Flags: []cli.Flag{
  43. cli.StringFlag{
  44. Name: "o",
  45. Usage: "the output api file",
  46. },
  47. },
  48. Action: apigen.ApiCommand,
  49. Subcommands: []cli.Command{
  50. {
  51. Name: "new",
  52. Usage: "fast create api service",
  53. Action: new.CreateServiceCommand,
  54. },
  55. {
  56. Name: "format",
  57. Usage: "format api files",
  58. Flags: []cli.Flag{
  59. cli.StringFlag{
  60. Name: "dir",
  61. Usage: "the format target dir",
  62. },
  63. cli.BoolFlag{
  64. Name: "iu",
  65. Usage: "ignore update",
  66. },
  67. cli.BoolFlag{
  68. Name: "stdin",
  69. Usage: "use stdin to input api doc content, press \"ctrl + d\" to send EOF",
  70. },
  71. },
  72. Action: format.GoFormatApi,
  73. },
  74. {
  75. Name: "validate",
  76. Usage: "validate api file",
  77. Flags: []cli.Flag{
  78. cli.StringFlag{
  79. Name: "api",
  80. Usage: "validate target api file",
  81. },
  82. },
  83. Action: validate.GoValidateApi,
  84. },
  85. {
  86. Name: "doc",
  87. Usage: "generate doc files",
  88. Flags: []cli.Flag{
  89. cli.StringFlag{
  90. Name: "dir",
  91. Usage: "the target dir",
  92. },
  93. cli.StringFlag{
  94. Name: "o",
  95. Required: false,
  96. Usage: "the output markdown directory",
  97. },
  98. },
  99. Action: docgen.DocCommand,
  100. },
  101. {
  102. Name: "go",
  103. Usage: "generate go files for provided api in yaml file",
  104. Flags: []cli.Flag{
  105. cli.StringFlag{
  106. Name: "dir",
  107. Usage: "the target dir",
  108. },
  109. cli.StringFlag{
  110. Name: "api",
  111. Usage: "the api file",
  112. },
  113. cli.StringFlag{
  114. Name: "style",
  115. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  116. },
  117. },
  118. Action: gogen.GoCommand,
  119. },
  120. {
  121. Name: "java",
  122. Usage: "generate java files for provided api in api file",
  123. Flags: []cli.Flag{
  124. cli.StringFlag{
  125. Name: "dir",
  126. Usage: "the target dir",
  127. },
  128. cli.StringFlag{
  129. Name: "api",
  130. Usage: "the api file",
  131. },
  132. },
  133. Action: javagen.JavaCommand,
  134. },
  135. {
  136. Name: "ts",
  137. Usage: "generate ts files for provided api in api file",
  138. Flags: []cli.Flag{
  139. cli.StringFlag{
  140. Name: "dir",
  141. Usage: "the target dir",
  142. },
  143. cli.StringFlag{
  144. Name: "api",
  145. Usage: "the api file",
  146. },
  147. cli.StringFlag{
  148. Name: "webapi",
  149. Usage: "the web api file path",
  150. },
  151. cli.StringFlag{
  152. Name: "caller",
  153. Usage: "the web api caller",
  154. },
  155. cli.BoolFlag{
  156. Name: "unwrap",
  157. Usage: "unwrap the webapi caller for import",
  158. },
  159. },
  160. Action: tsgen.TsCommand,
  161. },
  162. {
  163. Name: "dart",
  164. Usage: "generate dart files for provided api in api file",
  165. Flags: []cli.Flag{
  166. cli.StringFlag{
  167. Name: "dir",
  168. Usage: "the target dir",
  169. },
  170. cli.StringFlag{
  171. Name: "api",
  172. Usage: "the api file",
  173. },
  174. },
  175. Action: dartgen.DartCommand,
  176. },
  177. {
  178. Name: "kt",
  179. Usage: "generate kotlin code for provided api file",
  180. Flags: []cli.Flag{
  181. cli.StringFlag{
  182. Name: "dir",
  183. Usage: "the target directory",
  184. },
  185. cli.StringFlag{
  186. Name: "api",
  187. Usage: "the api file",
  188. },
  189. cli.StringFlag{
  190. Name: "pkg",
  191. Usage: "define package name for kotlin file",
  192. },
  193. },
  194. Action: ktgen.KtCommand,
  195. },
  196. {
  197. Name: "plugin",
  198. Usage: "custom file generator",
  199. Flags: []cli.Flag{
  200. cli.StringFlag{
  201. Name: "plugin, p",
  202. Usage: "the plugin file",
  203. },
  204. cli.StringFlag{
  205. Name: "dir",
  206. Usage: "the target directory",
  207. },
  208. cli.StringFlag{
  209. Name: "api",
  210. Usage: "the api file",
  211. },
  212. cli.StringFlag{
  213. Name: "style",
  214. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  215. },
  216. },
  217. Action: plugin.PluginCommand,
  218. },
  219. },
  220. },
  221. {
  222. Name: "docker",
  223. Usage: "generate Dockerfile",
  224. Flags: []cli.Flag{
  225. cli.StringFlag{
  226. Name: "go",
  227. Usage: "the file that contains main function",
  228. },
  229. cli.IntFlag{
  230. Name: "port",
  231. Usage: "the port to expose, default none",
  232. Value: 0,
  233. },
  234. },
  235. Action: docker.DockerCommand,
  236. },
  237. {
  238. Name: "kube",
  239. Usage: "generate kubernetes files",
  240. Subcommands: []cli.Command{
  241. {
  242. Name: "deploy",
  243. Usage: "generate deployment yaml file",
  244. Flags: []cli.Flag{
  245. cli.StringFlag{
  246. Name: "name",
  247. Usage: "the name of deployment",
  248. Required: true,
  249. },
  250. cli.StringFlag{
  251. Name: "namespace",
  252. Usage: "the namespace of deployment",
  253. Required: true,
  254. },
  255. cli.StringFlag{
  256. Name: "image",
  257. Usage: "the docker image of deployment",
  258. Required: true,
  259. },
  260. cli.StringFlag{
  261. Name: "secret",
  262. Usage: "the secret to image pull from registry",
  263. },
  264. cli.IntFlag{
  265. Name: "requestCpu",
  266. Usage: "the request cpu to deploy",
  267. Value: 500,
  268. },
  269. cli.IntFlag{
  270. Name: "requestMem",
  271. Usage: "the request memory to deploy",
  272. Value: 512,
  273. },
  274. cli.IntFlag{
  275. Name: "limitCpu",
  276. Usage: "the limit cpu to deploy",
  277. Value: 1000,
  278. },
  279. cli.IntFlag{
  280. Name: "limitMem",
  281. Usage: "the limit memory to deploy",
  282. Value: 1024,
  283. },
  284. cli.StringFlag{
  285. Name: "o",
  286. Usage: "the output yaml file",
  287. Required: true,
  288. },
  289. cli.IntFlag{
  290. Name: "replicas",
  291. Usage: "the number of replicas to deploy",
  292. Value: 3,
  293. },
  294. cli.IntFlag{
  295. Name: "revisions",
  296. Usage: "the number of revision history to limit",
  297. Value: 5,
  298. },
  299. cli.IntFlag{
  300. Name: "port",
  301. Usage: "the port of the deployment to listen on pod",
  302. Required: true,
  303. },
  304. cli.IntFlag{
  305. Name: "nodePort",
  306. Usage: "the nodePort of the deployment to expose",
  307. Value: 0,
  308. },
  309. cli.IntFlag{
  310. Name: "minReplicas",
  311. Usage: "the min replicas to deploy",
  312. Value: 3,
  313. },
  314. cli.IntFlag{
  315. Name: "maxReplicas",
  316. Usage: "the max replicas of deploy",
  317. Value: 10,
  318. },
  319. },
  320. Action: kube.DeploymentCommand,
  321. },
  322. },
  323. },
  324. {
  325. Name: "rpc",
  326. Usage: "generate rpc code",
  327. Subcommands: []cli.Command{
  328. {
  329. Name: "new",
  330. Usage: `generate rpc demo service`,
  331. Flags: []cli.Flag{
  332. cli.StringFlag{
  333. Name: "style",
  334. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  335. },
  336. cli.BoolFlag{
  337. Name: "idea",
  338. Usage: "whether the command execution environment is from idea plugin. [optional]",
  339. },
  340. },
  341. Action: rpc.RPCNew,
  342. },
  343. {
  344. Name: "template",
  345. Usage: `generate proto template`,
  346. Flags: []cli.Flag{
  347. cli.StringFlag{
  348. Name: "out, o",
  349. Usage: "the target path of proto",
  350. },
  351. },
  352. Action: rpc.RPCTemplate,
  353. },
  354. {
  355. Name: "proto",
  356. Usage: `generate rpc from proto`,
  357. Flags: []cli.Flag{
  358. cli.StringFlag{
  359. Name: "src, s",
  360. Usage: "the file path of the proto source file",
  361. },
  362. cli.StringSliceFlag{
  363. Name: "proto_path, I",
  364. Usage: `native command of protoc, specify the directory in which to search for imports. [optional]`,
  365. },
  366. cli.StringSliceFlag{
  367. Name: "go_opt",
  368. Usage: `native command of protoc-gen-go, specify the mapping from proto to go, eg --go_opt=proto_import=go_package_import. [optional]`,
  369. },
  370. cli.StringFlag{
  371. Name: "dir, d",
  372. Usage: `the target path of the code`,
  373. },
  374. cli.StringFlag{
  375. Name: "style",
  376. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  377. },
  378. cli.BoolFlag{
  379. Name: "idea",
  380. Usage: "whether the command execution environment is from idea plugin. [optional]",
  381. },
  382. },
  383. Action: rpc.RPC,
  384. },
  385. },
  386. },
  387. {
  388. Name: "model",
  389. Usage: "generate model code",
  390. Subcommands: []cli.Command{
  391. {
  392. Name: "mysql",
  393. Usage: `generate mysql model`,
  394. Subcommands: []cli.Command{
  395. {
  396. Name: "ddl",
  397. Usage: `generate mysql model from ddl`,
  398. Flags: []cli.Flag{
  399. cli.StringFlag{
  400. Name: "src, s",
  401. Usage: "the path or path globbing patterns of the ddl",
  402. },
  403. cli.StringFlag{
  404. Name: "dir, d",
  405. Usage: "the target dir",
  406. },
  407. cli.StringFlag{
  408. Name: "style",
  409. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  410. },
  411. cli.BoolFlag{
  412. Name: "cache, c",
  413. Usage: "generate code with cache [optional]",
  414. },
  415. cli.BoolFlag{
  416. Name: "idea",
  417. Usage: "for idea plugin [optional]",
  418. },
  419. cli.StringFlag{
  420. Name: "database, db",
  421. Usage: "the name of database [optional]",
  422. },
  423. },
  424. Action: model.MysqlDDL,
  425. },
  426. {
  427. Name: "datasource",
  428. Usage: `generate model from datasource`,
  429. Flags: []cli.Flag{
  430. cli.StringFlag{
  431. Name: "url",
  432. Usage: `the data source of database,like "root:password@tcp(127.0.0.1:3306)/database`,
  433. },
  434. cli.StringFlag{
  435. Name: "table, t",
  436. Usage: `the table or table globbing patterns in the database`,
  437. },
  438. cli.BoolFlag{
  439. Name: "cache, c",
  440. Usage: "generate code with cache [optional]",
  441. },
  442. cli.StringFlag{
  443. Name: "dir, d",
  444. Usage: "the target dir",
  445. },
  446. cli.StringFlag{
  447. Name: "style",
  448. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  449. },
  450. cli.BoolFlag{
  451. Name: "idea",
  452. Usage: "for idea plugin [optional]",
  453. },
  454. },
  455. Action: model.MySqlDataSource,
  456. },
  457. },
  458. },
  459. {
  460. Name: "postgresql",
  461. Usage: `generate postgresql model`,
  462. Subcommands: []cli.Command{
  463. {
  464. Name: "datasource",
  465. Usage: `generate model from datasource`,
  466. Flags: []cli.Flag{
  467. cli.StringFlag{
  468. Name: "url",
  469. Usage: `the data source of database,like "root:password@tcp(127.0.0.1:3306)/database`,
  470. },
  471. cli.StringFlag{
  472. Name: "table, t",
  473. Usage: `the table or table globbing patterns in the database`,
  474. },
  475. cli.StringFlag{
  476. Name: "schema, s",
  477. Usage: `the table schema, default is [public]`,
  478. },
  479. cli.BoolFlag{
  480. Name: "cache, c",
  481. Usage: "generate code with cache [optional]",
  482. },
  483. cli.StringFlag{
  484. Name: "dir, d",
  485. Usage: "the target dir",
  486. },
  487. cli.StringFlag{
  488. Name: "style",
  489. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  490. },
  491. cli.BoolFlag{
  492. Name: "idea",
  493. Usage: "for idea plugin [optional]",
  494. },
  495. },
  496. Action: model.PostgreSqlDataSource,
  497. },
  498. },
  499. },
  500. {
  501. Name: "mongo",
  502. Usage: `generate mongo model`,
  503. Flags: []cli.Flag{
  504. cli.StringSliceFlag{
  505. Name: "type, t",
  506. Usage: "specified model type name",
  507. },
  508. cli.BoolFlag{
  509. Name: "cache, c",
  510. Usage: "generate code with cache [optional]",
  511. },
  512. cli.StringFlag{
  513. Name: "dir, d",
  514. Usage: "the target dir",
  515. },
  516. cli.StringFlag{
  517. Name: "style",
  518. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  519. },
  520. },
  521. Action: mongo.Action,
  522. },
  523. },
  524. },
  525. {
  526. Name: "config",
  527. Usage: "generate config json",
  528. Flags: []cli.Flag{
  529. cli.StringFlag{
  530. Name: "path, p",
  531. Usage: "the target config go file",
  532. },
  533. },
  534. Action: configgen.GenConfigCommand,
  535. },
  536. {
  537. Name: "template",
  538. Usage: "template operation",
  539. Subcommands: []cli.Command{
  540. {
  541. Name: "init",
  542. Usage: "initialize the all templates(force update)",
  543. Action: tpl.GenTemplates,
  544. },
  545. {
  546. Name: "clean",
  547. Usage: "clean the all cache templates",
  548. Action: tpl.CleanTemplates,
  549. },
  550. {
  551. Name: "update",
  552. Usage: "update template of the target category to the latest",
  553. Flags: []cli.Flag{
  554. cli.StringFlag{
  555. Name: "category,c",
  556. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  557. },
  558. },
  559. Action: tpl.UpdateTemplates,
  560. },
  561. {
  562. Name: "revert",
  563. Usage: "revert the target template to the latest",
  564. Flags: []cli.Flag{
  565. cli.StringFlag{
  566. Name: "category,c",
  567. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  568. },
  569. cli.StringFlag{
  570. Name: "name,n",
  571. Usage: "the target file name of template",
  572. },
  573. },
  574. Action: tpl.RevertTemplates,
  575. },
  576. },
  577. },
  578. }
  579. )
  580. func main() {
  581. logx.Disable()
  582. load.Disable()
  583. stat.DisableLog()
  584. app := cli.NewApp()
  585. app.Usage = "a cli tool to generate code"
  586. app.Version = fmt.Sprintf("%s %s/%s", buildVersion, runtime.GOOS, runtime.GOARCH)
  587. app.Commands = commands
  588. // cli already print error messages
  589. if err := app.Run(os.Args); err != nil {
  590. fmt.Println(aurora.Red("error: " + err.Error()))
  591. }
  592. }