goctl.go 13 KB

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