goctl.go 12 KB

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