goctl.go 13 KB

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