goctl.go 8.1 KB

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