goctl.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/tal-tech/go-zero/core/logx"
  6. "github.com/tal-tech/go-zero/tools/goctl/api/apigen"
  7. "github.com/tal-tech/go-zero/tools/goctl/api/dartgen"
  8. "github.com/tal-tech/go-zero/tools/goctl/api/docgen"
  9. "github.com/tal-tech/go-zero/tools/goctl/api/format"
  10. "github.com/tal-tech/go-zero/tools/goctl/api/gogen"
  11. "github.com/tal-tech/go-zero/tools/goctl/api/javagen"
  12. "github.com/tal-tech/go-zero/tools/goctl/api/ktgen"
  13. "github.com/tal-tech/go-zero/tools/goctl/api/tsgen"
  14. "github.com/tal-tech/go-zero/tools/goctl/api/validate"
  15. "github.com/tal-tech/go-zero/tools/goctl/configgen"
  16. "github.com/tal-tech/go-zero/tools/goctl/docker"
  17. "github.com/tal-tech/go-zero/tools/goctl/feature"
  18. model "github.com/tal-tech/go-zero/tools/goctl/model/sql/command"
  19. rpc "github.com/tal-tech/go-zero/tools/goctl/rpc/command"
  20. "github.com/urfave/cli"
  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: "template",
  195. Usage: `generate proto template`,
  196. Flags: []cli.Flag{
  197. cli.StringFlag{
  198. Name: "out, o",
  199. Usage: "the target path of proto",
  200. },
  201. cli.BoolFlag{
  202. Name: "idea",
  203. Usage: "whether the command execution environment is from idea plugin. [option]",
  204. },
  205. },
  206. Action: rpc.RpcTemplate,
  207. },
  208. {
  209. Name: "proto",
  210. Usage: `generate rpc from proto`,
  211. Flags: []cli.Flag{
  212. cli.StringFlag{
  213. Name: "src, s",
  214. Usage: "the file path of the proto source file",
  215. },
  216. cli.StringFlag{
  217. Name: "dir, d",
  218. Usage: `the target path of the code,default path is "${pwd}". [option]`,
  219. },
  220. cli.StringFlag{
  221. Name: "service, srv",
  222. Usage: `the name of rpc service. [option]`,
  223. },
  224. cli.StringFlag{
  225. Name: "shared",
  226. Usage: `the dir of the shared file,default path is "${pwd}/shared. [option]`,
  227. },
  228. cli.BoolFlag{
  229. Name: "idea",
  230. Usage: "whether the command execution environment is from idea plugin. [option]",
  231. },
  232. },
  233. Action: rpc.Rpc,
  234. },
  235. },
  236. },
  237. {
  238. Name: "model",
  239. Usage: "generate model code",
  240. Subcommands: []cli.Command{
  241. {
  242. Name: "mysql",
  243. Usage: `generate mysql model`,
  244. Subcommands: []cli.Command{
  245. {
  246. Name: "ddl",
  247. Usage: `generate mysql model from ddl`,
  248. Flags: []cli.Flag{
  249. cli.StringFlag{
  250. Name: "src, s",
  251. Usage: "the file path of the ddl source file",
  252. },
  253. cli.StringFlag{
  254. Name: "dir, d",
  255. Usage: "the target dir",
  256. },
  257. cli.BoolFlag{
  258. Name: "cache, c",
  259. Usage: "generate code with cache [optional]",
  260. },
  261. cli.BoolFlag{
  262. Name: "idea",
  263. Usage: "for idea plugin [optional]",
  264. },
  265. },
  266. Action: model.MysqlDDL,
  267. },
  268. {
  269. Name: "datasource",
  270. Usage: `generate model from datasource`,
  271. Flags: []cli.Flag{
  272. cli.StringFlag{
  273. Name: "url",
  274. Usage: `the data source of database,like "root:password@tcp(127.0.0.1:3306)/database`,
  275. },
  276. cli.StringFlag{
  277. Name: "table, t",
  278. Usage: `source table,tables separated by commas,like "user,course`,
  279. },
  280. cli.BoolFlag{
  281. Name: "cache, c",
  282. Usage: "generate code with cache [optional]",
  283. },
  284. cli.StringFlag{
  285. Name: "dir, d",
  286. Usage: "the target dir",
  287. },
  288. cli.BoolFlag{
  289. Name: "idea",
  290. Usage: "for idea plugin [optional]",
  291. },
  292. },
  293. Action: model.MyDataSource,
  294. },
  295. },
  296. },
  297. },
  298. },
  299. {
  300. Name: "config",
  301. Usage: "generate config json",
  302. Flags: []cli.Flag{
  303. cli.StringFlag{
  304. Name: "path, p",
  305. Usage: "the target config go file",
  306. },
  307. },
  308. Action: configgen.GenConfigCommand,
  309. },
  310. {
  311. Name: "feature",
  312. Usage: "the features of the latest version",
  313. Action: feature.Feature,
  314. },
  315. }
  316. )
  317. func main() {
  318. logx.Disable()
  319. app := cli.NewApp()
  320. app.Usage = "a cli tool to generate code"
  321. app.Version = BuildTime
  322. app.Commands = commands
  323. // cli already print error messages
  324. if err := app.Run(os.Args); err != nil {
  325. fmt.Println("error:", err)
  326. }
  327. }