goctl.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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/urfave/cli"
  25. )
  26. var (
  27. BuildVersion = "1.1.1"
  28. commands = []cli.Command{
  29. {
  30. Name: "api",
  31. Usage: "generate api related files",
  32. Flags: []cli.Flag{
  33. cli.StringFlag{
  34. Name: "o",
  35. Usage: "the output api file",
  36. },
  37. },
  38. Action: apigen.ApiCommand,
  39. Subcommands: []cli.Command{
  40. {
  41. Name: "new",
  42. Usage: "fast create api service",
  43. Action: new.NewService,
  44. },
  45. {
  46. Name: "format",
  47. Usage: "format api files",
  48. Flags: []cli.Flag{
  49. cli.StringFlag{
  50. Name: "dir",
  51. Usage: "the format target dir",
  52. },
  53. cli.BoolFlag{
  54. Name: "iu",
  55. Usage: "ignore update",
  56. Required: false,
  57. },
  58. cli.BoolFlag{
  59. Name: "stdin",
  60. Usage: "use stdin to input api doc content, press \"ctrl + d\" to send EOF",
  61. Required: false,
  62. },
  63. },
  64. Action: format.GoFormatApi,
  65. },
  66. {
  67. Name: "validate",
  68. Usage: "validate api file",
  69. Flags: []cli.Flag{
  70. cli.StringFlag{
  71. Name: "api",
  72. Usage: "validate target api file",
  73. },
  74. },
  75. Action: validate.GoValidateApi,
  76. },
  77. {
  78. Name: "doc",
  79. Usage: "generate doc files",
  80. Flags: []cli.Flag{
  81. cli.StringFlag{
  82. Name: "dir",
  83. Usage: "the target dir",
  84. },
  85. },
  86. Action: docgen.DocCommand,
  87. },
  88. {
  89. Name: "go",
  90. Usage: "generate go files for provided api in yaml file",
  91. Flags: []cli.Flag{
  92. cli.StringFlag{
  93. Name: "dir",
  94. Usage: "the target dir",
  95. },
  96. cli.StringFlag{
  97. Name: "api",
  98. Usage: "the api file",
  99. },
  100. cli.StringFlag{
  101. Name: "style",
  102. Required: false,
  103. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  104. },
  105. },
  106. Action: gogen.GoCommand,
  107. },
  108. {
  109. Name: "java",
  110. Usage: "generate java files for provided api in api file",
  111. Flags: []cli.Flag{
  112. cli.StringFlag{
  113. Name: "dir",
  114. Usage: "the target dir",
  115. },
  116. cli.StringFlag{
  117. Name: "api",
  118. Usage: "the api file",
  119. },
  120. },
  121. Action: javagen.JavaCommand,
  122. },
  123. {
  124. Name: "ts",
  125. Usage: "generate ts files for provided api in api file",
  126. Flags: []cli.Flag{
  127. cli.StringFlag{
  128. Name: "dir",
  129. Usage: "the target dir",
  130. },
  131. cli.StringFlag{
  132. Name: "api",
  133. Usage: "the api file",
  134. },
  135. cli.StringFlag{
  136. Name: "webapi",
  137. Usage: "the web api file path",
  138. Required: false,
  139. },
  140. cli.StringFlag{
  141. Name: "caller",
  142. Usage: "the web api caller",
  143. Required: false,
  144. },
  145. cli.BoolFlag{
  146. Name: "unwrap",
  147. Usage: "unwrap the webapi caller for import",
  148. Required: false,
  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. Required: false,
  206. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  207. },
  208. },
  209. Action: plugin.PluginCommand,
  210. },
  211. },
  212. },
  213. {
  214. Name: "docker",
  215. Usage: "generate Dockerfile",
  216. Flags: []cli.Flag{
  217. cli.StringFlag{
  218. Name: "go",
  219. Usage: "the file that contains main function",
  220. },
  221. },
  222. Action: docker.DockerCommand,
  223. },
  224. {
  225. Name: "kube",
  226. Usage: "generate kubernetes files",
  227. Subcommands: []cli.Command{
  228. {
  229. Name: "deploy",
  230. Usage: "generate deployment yaml file",
  231. Flags: []cli.Flag{
  232. cli.StringFlag{
  233. Name: "name",
  234. Usage: "the name of deployment",
  235. Required: true,
  236. },
  237. cli.StringFlag{
  238. Name: "namespace",
  239. Usage: "the namespace of deployment",
  240. Required: true,
  241. },
  242. cli.StringFlag{
  243. Name: "image",
  244. Usage: "the docker image of deployment",
  245. Required: true,
  246. },
  247. cli.StringFlag{
  248. Name: "secret",
  249. Usage: "the image pull secret",
  250. Required: true,
  251. },
  252. cli.IntFlag{
  253. Name: "requestCpu",
  254. Usage: "the request cpu to deploy",
  255. Value: 500,
  256. },
  257. cli.IntFlag{
  258. Name: "requestMem",
  259. Usage: "the request memory to deploy",
  260. Value: 512,
  261. },
  262. cli.IntFlag{
  263. Name: "limitCpu",
  264. Usage: "the limit cpu to deploy",
  265. Value: 1000,
  266. },
  267. cli.IntFlag{
  268. Name: "limitMem",
  269. Usage: "the limit memory to deploy",
  270. Value: 1024,
  271. },
  272. cli.StringFlag{
  273. Name: "o",
  274. Usage: "the output yaml file",
  275. Required: true,
  276. },
  277. cli.IntFlag{
  278. Name: "replicas",
  279. Usage: "the number of replicas to deploy",
  280. Value: 3,
  281. },
  282. cli.IntFlag{
  283. Name: "revisions",
  284. Usage: "the number of revision history to limit",
  285. Value: 5,
  286. },
  287. cli.IntFlag{
  288. Name: "port",
  289. Usage: "the port of the deployment to listen on pod",
  290. Required: true,
  291. },
  292. cli.IntFlag{
  293. Name: "nodePort",
  294. Usage: "the nodePort of the deployment to expose",
  295. Value: 0,
  296. },
  297. cli.IntFlag{
  298. Name: "minReplicas",
  299. Usage: "the min replicas to deploy",
  300. Value: 3,
  301. },
  302. cli.IntFlag{
  303. Name: "maxReplicas",
  304. Usage: "the max replicas of deploy",
  305. Value: 10,
  306. },
  307. },
  308. Action: kube.DeploymentCommand,
  309. },
  310. },
  311. },
  312. {
  313. Name: "rpc",
  314. Usage: "generate rpc code",
  315. Subcommands: []cli.Command{
  316. {
  317. Name: "new",
  318. Usage: `generate rpc demo service`,
  319. Flags: []cli.Flag{
  320. cli.StringFlag{
  321. Name: "style",
  322. Required: false,
  323. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  324. },
  325. cli.BoolFlag{
  326. Name: "idea",
  327. Usage: "whether the command execution environment is from idea plugin. [optional]",
  328. },
  329. },
  330. Action: rpc.RpcNew,
  331. },
  332. {
  333. Name: "template",
  334. Usage: `generate proto template`,
  335. Flags: []cli.Flag{
  336. cli.StringFlag{
  337. Name: "out, o",
  338. Usage: "the target path of proto",
  339. },
  340. },
  341. Action: rpc.RpcTemplate,
  342. },
  343. {
  344. Name: "proto",
  345. Usage: `generate rpc from proto`,
  346. Flags: []cli.Flag{
  347. cli.StringFlag{
  348. Name: "src, s",
  349. Usage: "the file path of the proto source file",
  350. },
  351. cli.StringSliceFlag{
  352. Name: "proto_path, I",
  353. Usage: `native command of protoc, specify the directory in which to search for imports. [optional]`,
  354. },
  355. cli.StringFlag{
  356. Name: "dir, d",
  357. Usage: `the target path of the code`,
  358. },
  359. cli.StringFlag{
  360. Name: "style",
  361. Required: false,
  362. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  363. },
  364. cli.BoolFlag{
  365. Name: "idea",
  366. Usage: "whether the command execution environment is from idea plugin. [optional]",
  367. },
  368. },
  369. Action: rpc.Rpc,
  370. },
  371. },
  372. },
  373. {
  374. Name: "model",
  375. Usage: "generate model code",
  376. Subcommands: []cli.Command{
  377. {
  378. Name: "mysql",
  379. Usage: `generate mysql model`,
  380. Subcommands: []cli.Command{
  381. {
  382. Name: "ddl",
  383. Usage: `generate mysql model from ddl`,
  384. Flags: []cli.Flag{
  385. cli.StringFlag{
  386. Name: "src, s",
  387. Usage: "the path or path globbing patterns of the ddl",
  388. },
  389. cli.StringFlag{
  390. Name: "dir, d",
  391. Usage: "the target dir",
  392. },
  393. cli.StringFlag{
  394. Name: "style",
  395. Required: false,
  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. Required: false,
  432. Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]",
  433. },
  434. cli.BoolFlag{
  435. Name: "idea",
  436. Usage: "for idea plugin [optional]",
  437. },
  438. },
  439. Action: model.MyDataSource,
  440. },
  441. },
  442. },
  443. },
  444. },
  445. {
  446. Name: "config",
  447. Usage: "generate config json",
  448. Flags: []cli.Flag{
  449. cli.StringFlag{
  450. Name: "path, p",
  451. Usage: "the target config go file",
  452. },
  453. },
  454. Action: configgen.GenConfigCommand,
  455. },
  456. {
  457. Name: "template",
  458. Usage: "template operation",
  459. Subcommands: []cli.Command{
  460. {
  461. Name: "init",
  462. Usage: "initialize the all templates(force update)",
  463. Action: tpl.GenTemplates,
  464. },
  465. {
  466. Name: "clean",
  467. Usage: "clean the all cache templates",
  468. Action: tpl.CleanTemplates,
  469. },
  470. {
  471. Name: "update",
  472. Usage: "update template of the target category to the latest",
  473. Flags: []cli.Flag{
  474. cli.StringFlag{
  475. Name: "category,c",
  476. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  477. },
  478. },
  479. Action: tpl.UpdateTemplates,
  480. },
  481. {
  482. Name: "revert",
  483. Usage: "revert the target template to the latest",
  484. Flags: []cli.Flag{
  485. cli.StringFlag{
  486. Name: "category,c",
  487. Usage: "the category of template, enum [api,rpc,model,docker,kube]",
  488. },
  489. cli.StringFlag{
  490. Name: "name,n",
  491. Usage: "the target file name of template",
  492. },
  493. },
  494. Action: tpl.RevertTemplates,
  495. },
  496. },
  497. },
  498. }
  499. )
  500. func main() {
  501. logx.Disable()
  502. app := cli.NewApp()
  503. app.Usage = "a cli tool to generate code"
  504. app.Version = fmt.Sprintf("%s %s/%s", BuildVersion, runtime.GOOS, runtime.GOARCH)
  505. app.Commands = commands
  506. // cli already print error messages
  507. if err := app.Run(os.Args); err != nil {
  508. fmt.Println("error:", err)
  509. }
  510. }