goctl.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 = "20201108"
  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. },
  99. Action: gogen.GoCommand,
  100. },
  101. {
  102. Name: "java",
  103. Usage: "generate java files for provided api in api file",
  104. Flags: []cli.Flag{
  105. cli.StringFlag{
  106. Name: "dir",
  107. Usage: "the target dir",
  108. },
  109. cli.StringFlag{
  110. Name: "api",
  111. Usage: "the api file",
  112. },
  113. },
  114. Action: javagen.JavaCommand,
  115. },
  116. {
  117. Name: "ts",
  118. Usage: "generate ts 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. cli.StringFlag{
  129. Name: "webapi",
  130. Usage: "the web api file path",
  131. Required: false,
  132. },
  133. cli.StringFlag{
  134. Name: "caller",
  135. Usage: "the web api caller",
  136. Required: false,
  137. },
  138. cli.BoolFlag{
  139. Name: "unwrap",
  140. Usage: "unwrap the webapi caller for import",
  141. Required: false,
  142. },
  143. },
  144. Action: tsgen.TsCommand,
  145. },
  146. {
  147. Name: "dart",
  148. Usage: "generate dart files for provided api in api file",
  149. Flags: []cli.Flag{
  150. cli.StringFlag{
  151. Name: "dir",
  152. Usage: "the target dir",
  153. },
  154. cli.StringFlag{
  155. Name: "api",
  156. Usage: "the api file",
  157. },
  158. },
  159. Action: dartgen.DartCommand,
  160. },
  161. {
  162. Name: "kt",
  163. Usage: "generate kotlin code for provided api file",
  164. Flags: []cli.Flag{
  165. cli.StringFlag{
  166. Name: "dir",
  167. Usage: "the target directory",
  168. },
  169. cli.StringFlag{
  170. Name: "api",
  171. Usage: "the api file",
  172. },
  173. cli.StringFlag{
  174. Name: "pkg",
  175. Usage: "define package name for kotlin file",
  176. },
  177. },
  178. Action: ktgen.KtCommand,
  179. },
  180. },
  181. },
  182. {
  183. Name: "docker",
  184. Usage: "generate Dockerfile",
  185. Flags: []cli.Flag{
  186. cli.StringFlag{
  187. Name: "go",
  188. Usage: "the file that contains main function",
  189. },
  190. },
  191. Action: docker.DockerCommand,
  192. },
  193. {
  194. Name: "rpc",
  195. Usage: "generate rpc code",
  196. Subcommands: []cli.Command{
  197. {
  198. Name: "new",
  199. Usage: `generate rpc demo service`,
  200. Flags: []cli.Flag{
  201. cli.StringFlag{
  202. Name: "style",
  203. Usage: "the file naming style, lower|camel|snake,default is lower",
  204. },
  205. cli.BoolFlag{
  206. Name: "idea",
  207. Usage: "whether the command execution environment is from idea plugin. [optional]",
  208. },
  209. },
  210. Action: rpc.RpcNew,
  211. },
  212. {
  213. Name: "template",
  214. Usage: `generate proto template`,
  215. Flags: []cli.Flag{
  216. cli.StringFlag{
  217. Name: "out, o",
  218. Usage: "the target path of proto",
  219. },
  220. },
  221. Action: rpc.RpcTemplate,
  222. },
  223. {
  224. Name: "proto",
  225. Usage: `generate rpc from proto`,
  226. Flags: []cli.Flag{
  227. cli.StringFlag{
  228. Name: "src, s",
  229. Usage: "the file path of the proto source file",
  230. },
  231. cli.StringSliceFlag{
  232. Name: "proto_path, I",
  233. Usage: `native command of protoc, specify the directory in which to search for imports. [optional]`,
  234. },
  235. cli.StringFlag{
  236. Name: "dir, d",
  237. Usage: `the target path of the code`,
  238. },
  239. cli.StringFlag{
  240. Name: "style",
  241. Usage: "the file naming style, lower|camel|snake,default is lower",
  242. },
  243. cli.BoolFlag{
  244. Name: "idea",
  245. Usage: "whether the command execution environment is from idea plugin. [optional]",
  246. },
  247. },
  248. Action: rpc.Rpc,
  249. },
  250. },
  251. },
  252. {
  253. Name: "model",
  254. Usage: "generate model code",
  255. Subcommands: []cli.Command{
  256. {
  257. Name: "mysql",
  258. Usage: `generate mysql model`,
  259. Subcommands: []cli.Command{
  260. {
  261. Name: "ddl",
  262. Usage: `generate mysql model from ddl`,
  263. Flags: []cli.Flag{
  264. cli.StringFlag{
  265. Name: "src, s",
  266. Usage: "the path or path globbing patterns of the ddl",
  267. },
  268. cli.StringFlag{
  269. Name: "dir, d",
  270. Usage: "the target dir",
  271. },
  272. cli.StringFlag{
  273. Name: "style",
  274. Usage: "the file naming style, lower|camel|snake,default is lower",
  275. },
  276. cli.BoolFlag{
  277. Name: "cache, c",
  278. Usage: "generate code with cache [optional]",
  279. },
  280. cli.BoolFlag{
  281. Name: "idea",
  282. Usage: "for idea plugin [optional]",
  283. },
  284. },
  285. Action: model.MysqlDDL,
  286. },
  287. {
  288. Name: "datasource",
  289. Usage: `generate model from datasource`,
  290. Flags: []cli.Flag{
  291. cli.StringFlag{
  292. Name: "url",
  293. Usage: `the data source of database,like "root:password@tcp(127.0.0.1:3306)/database`,
  294. },
  295. cli.StringFlag{
  296. Name: "table, t",
  297. Usage: `the table or table globbing patterns in the database`,
  298. },
  299. cli.BoolFlag{
  300. Name: "cache, c",
  301. Usage: "generate code with cache [optional]",
  302. },
  303. cli.StringFlag{
  304. Name: "dir, d",
  305. Usage: "the target dir",
  306. },
  307. cli.StringFlag{
  308. Name: "style",
  309. Usage: "the file naming style, lower|camel|snake, default is lower",
  310. },
  311. cli.BoolFlag{
  312. Name: "idea",
  313. Usage: "for idea plugin [optional]",
  314. },
  315. },
  316. Action: model.MyDataSource,
  317. },
  318. },
  319. },
  320. },
  321. },
  322. {
  323. Name: "config",
  324. Usage: "generate config json",
  325. Flags: []cli.Flag{
  326. cli.StringFlag{
  327. Name: "path, p",
  328. Usage: "the target config go file",
  329. },
  330. },
  331. Action: configgen.GenConfigCommand,
  332. },
  333. {
  334. Name: "template",
  335. Usage: "template operation",
  336. Subcommands: []cli.Command{
  337. {
  338. Name: "init",
  339. Usage: "initialize the all templates(force update)",
  340. Action: tpl.GenTemplates,
  341. },
  342. {
  343. Name: "clean",
  344. Usage: "clean the all cache templates",
  345. Action: tpl.CleanTemplates,
  346. },
  347. {
  348. Name: "update",
  349. Usage: "update template of the target category to the latest",
  350. Flags: []cli.Flag{
  351. cli.StringFlag{
  352. Name: "category,c",
  353. Usage: "the category of template, enum [api,rpc,model]",
  354. },
  355. },
  356. Action: tpl.UpdateTemplates,
  357. },
  358. {
  359. Name: "revert",
  360. Usage: "revert the target template to the latest",
  361. Flags: []cli.Flag{
  362. cli.StringFlag{
  363. Name: "category,c",
  364. Usage: "the category of template, enum [api,rpc,model]",
  365. },
  366. cli.StringFlag{
  367. Name: "name,n",
  368. Usage: "the target file name of template",
  369. },
  370. },
  371. Action: tpl.RevertTemplates,
  372. },
  373. },
  374. },
  375. }
  376. )
  377. func main() {
  378. logx.Disable()
  379. app := cli.NewApp()
  380. app.Usage = "a cli tool to generate code"
  381. app.Version = fmt.Sprintf("%s %s/%s", BuildVersion, runtime.GOOS, runtime.GOARCH)
  382. app.Commands = commands
  383. // cli already print error messages
  384. if err := app.Run(os.Args); err != nil {
  385. fmt.Println("error:", err)
  386. }
  387. }