goctl.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. model "github.com/tal-tech/go-zero/tools/goctl/model/sql/command"
  20. rpc "github.com/tal-tech/go-zero/tools/goctl/rpc/cli"
  21. "github.com/tal-tech/go-zero/tools/goctl/tpl"
  22. "github.com/urfave/cli"
  23. )
  24. var (
  25. BuildVersion = "20201021"
  26. commands = []cli.Command{
  27. {
  28. Name: "api",
  29. Usage: "generate api related files",
  30. Flags: []cli.Flag{
  31. cli.StringFlag{
  32. Name: "o",
  33. Usage: "the output api file",
  34. },
  35. },
  36. Action: apigen.ApiCommand,
  37. Subcommands: []cli.Command{
  38. {
  39. Name: "new",
  40. Usage: "fast create api service",
  41. Action: new.NewService,
  42. },
  43. {
  44. Name: "format",
  45. Usage: "format api files",
  46. Flags: []cli.Flag{
  47. cli.StringFlag{
  48. Name: "dir",
  49. Usage: "the format target dir",
  50. },
  51. cli.BoolFlag{
  52. Name: "iu",
  53. Usage: "ignore update",
  54. Required: false,
  55. },
  56. cli.BoolFlag{
  57. Name: "stdin",
  58. Usage: "use stdin to input api doc content, press \"ctrl + d\" to send EOF",
  59. Required: false,
  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.BoolFlag{
  99. Name: "force",
  100. Usage: "force override the exist files",
  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. Required: false,
  136. },
  137. cli.StringFlag{
  138. Name: "caller",
  139. Usage: "the web api caller",
  140. Required: false,
  141. },
  142. cli.BoolFlag{
  143. Name: "unwrap",
  144. Usage: "unwrap the webapi caller for import",
  145. Required: false,
  146. },
  147. },
  148. Action: tsgen.TsCommand,
  149. },
  150. {
  151. Name: "dart",
  152. Usage: "generate dart files for provided api in api file",
  153. Flags: []cli.Flag{
  154. cli.StringFlag{
  155. Name: "dir",
  156. Usage: "the target dir",
  157. },
  158. cli.StringFlag{
  159. Name: "api",
  160. Usage: "the api file",
  161. },
  162. },
  163. Action: dartgen.DartCommand,
  164. },
  165. {
  166. Name: "kt",
  167. Usage: "generate kotlin code for provided api file",
  168. Flags: []cli.Flag{
  169. cli.StringFlag{
  170. Name: "dir",
  171. Usage: "the target directory",
  172. },
  173. cli.StringFlag{
  174. Name: "api",
  175. Usage: "the api file",
  176. },
  177. cli.StringFlag{
  178. Name: "pkg",
  179. Usage: "define package name for kotlin file",
  180. },
  181. },
  182. Action: ktgen.KtCommand,
  183. },
  184. },
  185. },
  186. {
  187. Name: "docker",
  188. Usage: "generate Dockerfile and Makefile",
  189. Flags: []cli.Flag{
  190. cli.StringFlag{
  191. Name: "go",
  192. Usage: "the file that contains main function",
  193. },
  194. cli.StringFlag{
  195. Name: "namespace, n",
  196. Usage: "which namespace of kubernetes to deploy the service",
  197. },
  198. },
  199. Action: docker.DockerCommand,
  200. },
  201. {
  202. Name: "rpc",
  203. Usage: "generate rpc code",
  204. Subcommands: []cli.Command{
  205. {
  206. Name: "new",
  207. Usage: `generate rpc demo service`,
  208. Flags: []cli.Flag{
  209. cli.BoolFlag{
  210. Name: "idea",
  211. Usage: "whether the command execution environment is from idea plugin. [optional]",
  212. },
  213. },
  214. Action: rpc.RpcNew,
  215. },
  216. {
  217. Name: "template",
  218. Usage: `generate proto template`,
  219. Flags: []cli.Flag{
  220. cli.StringFlag{
  221. Name: "out, o",
  222. Usage: "the target path of proto",
  223. },
  224. cli.BoolFlag{
  225. Name: "idea",
  226. Usage: "whether the command execution environment is from idea plugin. [optional]",
  227. },
  228. },
  229. Action: rpc.RpcTemplate,
  230. },
  231. {
  232. Name: "proto",
  233. Usage: `generate rpc from proto`,
  234. Flags: []cli.Flag{
  235. cli.StringFlag{
  236. Name: "src, s",
  237. Usage: "the file path of the proto source file",
  238. },
  239. cli.StringSliceFlag{
  240. Name: "proto_path, I",
  241. Usage: `native command of protoc, specify the directory in which to search for imports. [optional]`,
  242. },
  243. cli.StringFlag{
  244. Name: "dir, d",
  245. Usage: `the target path of the code`,
  246. },
  247. cli.BoolFlag{
  248. Name: "idea",
  249. Usage: "whether the command execution environment is from idea plugin. [optional]",
  250. },
  251. },
  252. Action: rpc.Rpc,
  253. },
  254. },
  255. },
  256. {
  257. Name: "model",
  258. Usage: "generate model code",
  259. Subcommands: []cli.Command{
  260. {
  261. Name: "mysql",
  262. Usage: `generate mysql model`,
  263. Subcommands: []cli.Command{
  264. {
  265. Name: "ddl",
  266. Usage: `generate mysql model from ddl`,
  267. Flags: []cli.Flag{
  268. cli.StringFlag{
  269. Name: "src, s",
  270. Usage: "the path or path globbing patterns of the ddl",
  271. },
  272. cli.StringFlag{
  273. Name: "dir, d",
  274. Usage: "the target dir",
  275. },
  276. cli.StringFlag{
  277. Name: "style",
  278. Usage: "the file naming style, lower|camel|underline,default is lower",
  279. },
  280. cli.BoolFlag{
  281. Name: "cache, c",
  282. Usage: "generate code with cache [optional]",
  283. },
  284. cli.BoolFlag{
  285. Name: "idea",
  286. Usage: "for idea plugin [optional]",
  287. },
  288. },
  289. Action: model.MysqlDDL,
  290. },
  291. {
  292. Name: "datasource",
  293. Usage: `generate model from datasource`,
  294. Flags: []cli.Flag{
  295. cli.StringFlag{
  296. Name: "url",
  297. Usage: `the data source of database,like "root:password@tcp(127.0.0.1:3306)/database`,
  298. },
  299. cli.StringFlag{
  300. Name: "table, t",
  301. Usage: `the table or table globbing patterns in the database`,
  302. },
  303. cli.BoolFlag{
  304. Name: "cache, c",
  305. Usage: "generate code with cache [optional]",
  306. },
  307. cli.StringFlag{
  308. Name: "dir, d",
  309. Usage: "the target dir",
  310. },
  311. cli.StringFlag{
  312. Name: "style",
  313. Usage: "the file naming style, lower|camel|snake, default is lower",
  314. },
  315. cli.BoolFlag{
  316. Name: "idea",
  317. Usage: "for idea plugin [optional]",
  318. },
  319. },
  320. Action: model.MyDataSource,
  321. },
  322. },
  323. },
  324. },
  325. },
  326. {
  327. Name: "config",
  328. Usage: "generate config json",
  329. Flags: []cli.Flag{
  330. cli.StringFlag{
  331. Name: "path, p",
  332. Usage: "the target config go file",
  333. },
  334. },
  335. Action: configgen.GenConfigCommand,
  336. },
  337. {
  338. Name: "template",
  339. Usage: "template operation",
  340. Subcommands: []cli.Command{
  341. {
  342. Name: "init",
  343. Usage: "initialize the all templates(force update)",
  344. Action: tpl.GenTemplates,
  345. },
  346. {
  347. Name: "clean",
  348. Usage: "clean the all cache templates",
  349. Action: tpl.CleanTemplates,
  350. },
  351. {
  352. Name: "update",
  353. Usage: "update template of the target category to the latest",
  354. Flags: []cli.Flag{
  355. cli.StringFlag{
  356. Name: "category,c",
  357. Usage: "the category of template, enum [api,rpc,model]",
  358. },
  359. },
  360. Action: tpl.UpdateTemplates,
  361. },
  362. {
  363. Name: "revert",
  364. Usage: "revert the target template to the latest",
  365. Flags: []cli.Flag{
  366. cli.StringFlag{
  367. Name: "category,c",
  368. Usage: "the category of template, enum [api,rpc,model]",
  369. },
  370. cli.StringFlag{
  371. Name: "name,n",
  372. Usage: "the target file name of template",
  373. },
  374. },
  375. Action: tpl.RevertTemplates,
  376. },
  377. },
  378. },
  379. }
  380. )
  381. func main() {
  382. logx.Disable()
  383. app := cli.NewApp()
  384. app.Usage = "a cli tool to generate code"
  385. app.Version = fmt.Sprintf("%s %s/%s", BuildVersion, runtime.GOOS, runtime.GOARCH)
  386. app.Commands = commands
  387. // cli already print error messages
  388. if err := app.Run(os.Args); err != nil {
  389. fmt.Println("error:", err)
  390. }
  391. }