goctl.go 12 KB

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