goctl.go 13 KB

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