goctl.go 13 KB

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