goctl.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/urfave/cli"
  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/tsgen"
  15. "github.com/tal-tech/go-zero/tools/goctl/api/validate"
  16. "github.com/tal-tech/go-zero/tools/goctl/configgen"
  17. "github.com/tal-tech/go-zero/tools/goctl/docker"
  18. "github.com/tal-tech/go-zero/tools/goctl/feature"
  19. model "github.com/tal-tech/go-zero/tools/goctl/model/sql/command"
  20. rpc "github.com/tal-tech/go-zero/tools/goctl/rpc/command"
  21. )
  22. var (
  23. BuildTime = "not set"
  24. commands = []cli.Command{
  25. {
  26. Name: "api",
  27. Usage: "generate api related files",
  28. Flags: []cli.Flag{
  29. cli.StringFlag{
  30. Name: "o",
  31. Usage: "the output api file",
  32. },
  33. },
  34. Action: apigen.ApiCommand,
  35. Subcommands: []cli.Command{
  36. {
  37. Name: "format",
  38. Usage: "format api files",
  39. Flags: []cli.Flag{
  40. cli.StringFlag{
  41. Name: "dir",
  42. Usage: "the format target dir",
  43. },
  44. cli.BoolFlag{
  45. Name: "p",
  46. Usage: "print result to console",
  47. },
  48. cli.BoolFlag{
  49. Name: "iu",
  50. Usage: "ignore update",
  51. Required: false,
  52. },
  53. },
  54. Action: format.GoFormatApi,
  55. },
  56. {
  57. Name: "validate",
  58. Usage: "validate api file",
  59. Flags: []cli.Flag{
  60. cli.StringFlag{
  61. Name: "api",
  62. Usage: "validate target api file",
  63. },
  64. },
  65. Action: validate.GoValidateApi,
  66. },
  67. {
  68. Name: "doc",
  69. Usage: "generate doc files",
  70. Flags: []cli.Flag{
  71. cli.StringFlag{
  72. Name: "dir",
  73. Usage: "the target dir",
  74. },
  75. },
  76. Action: docgen.DocCommand,
  77. },
  78. {
  79. Name: "go",
  80. Usage: "generate go files for provided api in yaml file",
  81. Flags: []cli.Flag{
  82. cli.StringFlag{
  83. Name: "dir",
  84. Usage: "the target dir",
  85. },
  86. cli.StringFlag{
  87. Name: "api",
  88. Usage: "the api file",
  89. },
  90. },
  91. Action: gogen.GoCommand,
  92. },
  93. {
  94. Name: "java",
  95. Usage: "generate java files for provided api in api file",
  96. Flags: []cli.Flag{
  97. cli.StringFlag{
  98. Name: "dir",
  99. Usage: "the target dir",
  100. },
  101. cli.StringFlag{
  102. Name: "api",
  103. Usage: "the api file",
  104. },
  105. },
  106. Action: javagen.JavaCommand,
  107. },
  108. {
  109. Name: "ts",
  110. Usage: "generate ts files for provided api in api file",
  111. Flags: []cli.Flag{
  112. cli.StringFlag{
  113. Name: "dir",
  114. Usage: "the target dir",
  115. },
  116. cli.StringFlag{
  117. Name: "api",
  118. Usage: "the api file",
  119. },
  120. cli.StringFlag{
  121. Name: "webapi",
  122. Usage: "the web api file path",
  123. Required: false,
  124. },
  125. cli.StringFlag{
  126. Name: "caller",
  127. Usage: "the web api caller",
  128. Required: false,
  129. },
  130. cli.BoolFlag{
  131. Name: "unwrap",
  132. Usage: "unwrap the webapi caller for import",
  133. Required: false,
  134. },
  135. },
  136. Action: tsgen.TsCommand,
  137. },
  138. {
  139. Name: "dart",
  140. Usage: "generate dart files for provided api in api file",
  141. Flags: []cli.Flag{
  142. cli.StringFlag{
  143. Name: "dir",
  144. Usage: "the target dir",
  145. },
  146. cli.StringFlag{
  147. Name: "api",
  148. Usage: "the api file",
  149. },
  150. },
  151. Action: dartgen.DartCommand,
  152. },
  153. {
  154. Name: "kt",
  155. Usage: "generate kotlin code for provided api file",
  156. Flags: []cli.Flag{
  157. cli.StringFlag{
  158. Name: "dir",
  159. Usage: "the target directory",
  160. },
  161. cli.StringFlag{
  162. Name: "api",
  163. Usage: "the api file",
  164. },
  165. cli.StringFlag{
  166. Name: "pkg",
  167. Usage: "define package name for kotlin file",
  168. },
  169. },
  170. Action: ktgen.KtCommand,
  171. },
  172. },
  173. },
  174. {
  175. Name: "docker",
  176. Usage: "generate Dockerfile and Makefile",
  177. Flags: []cli.Flag{
  178. cli.StringFlag{
  179. Name: "go",
  180. Usage: "the file that contains main function",
  181. },
  182. cli.StringFlag{
  183. Name: "namespace, n",
  184. Usage: "which namespace of kubernetes to deploy the service",
  185. },
  186. },
  187. Action: docker.DockerCommand,
  188. },
  189. {
  190. Name: "rpc",
  191. Usage: "generate rpc code",
  192. Subcommands: []cli.Command{
  193. {
  194. Name: "new",
  195. Usage: `generate rpc demo service`,
  196. Flags: []cli.Flag{
  197. cli.BoolFlag{
  198. Name: "idea",
  199. Usage: "whether the command execution environment is from idea plugin. [option]",
  200. },
  201. },
  202. Action: rpc.RpcNew,
  203. },
  204. {
  205. Name: "template",
  206. Usage: `generate proto template`,
  207. Flags: []cli.Flag{
  208. cli.StringFlag{
  209. Name: "out, o",
  210. Usage: "the target path of proto",
  211. },
  212. cli.BoolFlag{
  213. Name: "idea",
  214. Usage: "whether the command execution environment is from idea plugin. [option]",
  215. },
  216. },
  217. Action: rpc.RpcTemplate,
  218. },
  219. {
  220. Name: "proto",
  221. Usage: `generate rpc from proto`,
  222. Flags: []cli.Flag{
  223. cli.StringFlag{
  224. Name: "src, s",
  225. Usage: "the file path of the proto source file",
  226. },
  227. cli.StringFlag{
  228. Name: "dir, d",
  229. Usage: `the target path of the code,default path is "${pwd}". [option]`,
  230. },
  231. cli.StringFlag{
  232. Name: "service, srv",
  233. Usage: `the name of rpc service. [option]`,
  234. },
  235. cli.BoolFlag{
  236. Name: "idea",
  237. Usage: "whether the command execution environment is from idea plugin. [option]",
  238. },
  239. },
  240. Action: rpc.Rpc,
  241. },
  242. },
  243. },
  244. {
  245. Name: "model",
  246. Usage: "generate model code",
  247. Subcommands: []cli.Command{
  248. {
  249. Name: "mysql",
  250. Usage: `generate mysql model`,
  251. Subcommands: []cli.Command{
  252. {
  253. Name: "ddl",
  254. Usage: `generate mysql model from ddl`,
  255. Flags: []cli.Flag{
  256. cli.StringFlag{
  257. Name: "src, s",
  258. Usage: "the file path of the ddl source file",
  259. },
  260. cli.StringFlag{
  261. Name: "dir, d",
  262. Usage: "the target dir",
  263. },
  264. cli.BoolFlag{
  265. Name: "cache, c",
  266. Usage: "generate code with cache [optional]",
  267. },
  268. cli.BoolFlag{
  269. Name: "idea",
  270. Usage: "for idea plugin [optional]",
  271. },
  272. },
  273. Action: model.MysqlDDL,
  274. },
  275. {
  276. Name: "datasource",
  277. Usage: `generate model from datasource`,
  278. Flags: []cli.Flag{
  279. cli.StringFlag{
  280. Name: "url",
  281. Usage: `the data source of database,like "root:password@tcp(127.0.0.1:3306)/database`,
  282. },
  283. cli.StringFlag{
  284. Name: "table, t",
  285. Usage: `source table,tables separated by commas,like "user,course`,
  286. },
  287. cli.BoolFlag{
  288. Name: "cache, c",
  289. Usage: "generate code with cache [optional]",
  290. },
  291. cli.StringFlag{
  292. Name: "dir, d",
  293. Usage: "the target dir",
  294. },
  295. cli.BoolFlag{
  296. Name: "idea",
  297. Usage: "for idea plugin [optional]",
  298. },
  299. },
  300. Action: model.MyDataSource,
  301. },
  302. },
  303. },
  304. },
  305. },
  306. {
  307. Name: "config",
  308. Usage: "generate config json",
  309. Flags: []cli.Flag{
  310. cli.StringFlag{
  311. Name: "path, p",
  312. Usage: "the target config go file",
  313. },
  314. },
  315. Action: configgen.GenConfigCommand,
  316. },
  317. {
  318. Name: "feature",
  319. Usage: "the features of the latest version",
  320. Action: feature.Feature,
  321. },
  322. }
  323. )
  324. func main() {
  325. logx.Disable()
  326. app := cli.NewApp()
  327. app.Usage = "a cli tool to generate code"
  328. app.Version = BuildTime
  329. app.Commands = commands
  330. // cli already print error messages
  331. if err := app.Run(os.Args); err != nil {
  332. fmt.Println("error:", err)
  333. }
  334. }