goctl.go 9.8 KB

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