goctl.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. Subcommands: []cli.Command{
  103. {
  104. Name: "template",
  105. Usage: "initialize the api templates",
  106. Action: gogen.GenTemplates,
  107. },
  108. },
  109. },
  110. {
  111. Name: "java",
  112. Usage: "generate java files for provided api in api file",
  113. Flags: []cli.Flag{
  114. cli.StringFlag{
  115. Name: "dir",
  116. Usage: "the target dir",
  117. },
  118. cli.StringFlag{
  119. Name: "api",
  120. Usage: "the api file",
  121. },
  122. },
  123. Action: javagen.JavaCommand,
  124. },
  125. {
  126. Name: "ts",
  127. Usage: "generate ts files for provided api in api file",
  128. Flags: []cli.Flag{
  129. cli.StringFlag{
  130. Name: "dir",
  131. Usage: "the target dir",
  132. },
  133. cli.StringFlag{
  134. Name: "api",
  135. Usage: "the api file",
  136. },
  137. cli.StringFlag{
  138. Name: "webapi",
  139. Usage: "the web api file path",
  140. Required: false,
  141. },
  142. cli.StringFlag{
  143. Name: "caller",
  144. Usage: "the web api caller",
  145. Required: false,
  146. },
  147. cli.BoolFlag{
  148. Name: "unwrap",
  149. Usage: "unwrap the webapi caller for import",
  150. Required: false,
  151. },
  152. },
  153. Action: tsgen.TsCommand,
  154. },
  155. {
  156. Name: "dart",
  157. Usage: "generate dart files for provided api in api file",
  158. Flags: []cli.Flag{
  159. cli.StringFlag{
  160. Name: "dir",
  161. Usage: "the target dir",
  162. },
  163. cli.StringFlag{
  164. Name: "api",
  165. Usage: "the api file",
  166. },
  167. },
  168. Action: dartgen.DartCommand,
  169. },
  170. {
  171. Name: "kt",
  172. Usage: "generate kotlin code for provided api file",
  173. Flags: []cli.Flag{
  174. cli.StringFlag{
  175. Name: "dir",
  176. Usage: "the target directory",
  177. },
  178. cli.StringFlag{
  179. Name: "api",
  180. Usage: "the api file",
  181. },
  182. cli.StringFlag{
  183. Name: "pkg",
  184. Usage: "define package name for kotlin file",
  185. },
  186. },
  187. Action: ktgen.KtCommand,
  188. },
  189. },
  190. },
  191. {
  192. Name: "docker",
  193. Usage: "generate Dockerfile and Makefile",
  194. Flags: []cli.Flag{
  195. cli.StringFlag{
  196. Name: "go",
  197. Usage: "the file that contains main function",
  198. },
  199. cli.StringFlag{
  200. Name: "namespace, n",
  201. Usage: "which namespace of kubernetes to deploy the service",
  202. },
  203. },
  204. Action: docker.DockerCommand,
  205. },
  206. {
  207. Name: "rpc",
  208. Usage: "generate rpc code",
  209. Subcommands: []cli.Command{
  210. {
  211. Name: "new",
  212. Usage: `generate rpc demo service`,
  213. Flags: []cli.Flag{
  214. cli.BoolFlag{
  215. Name: "idea",
  216. Usage: "whether the command execution environment is from idea plugin. [option]",
  217. },
  218. },
  219. Action: rpc.RpcNew,
  220. },
  221. {
  222. Name: "template",
  223. Usage: `generate proto template`,
  224. Flags: []cli.Flag{
  225. cli.StringFlag{
  226. Name: "out, o",
  227. Usage: "the target path of proto",
  228. },
  229. cli.BoolFlag{
  230. Name: "idea",
  231. Usage: "whether the command execution environment is from idea plugin. [option]",
  232. },
  233. },
  234. Action: rpc.RpcTemplate,
  235. },
  236. {
  237. Name: "proto",
  238. Usage: `generate rpc from proto`,
  239. Flags: []cli.Flag{
  240. cli.StringFlag{
  241. Name: "src, s",
  242. Usage: "the file path of the proto source file",
  243. },
  244. cli.StringFlag{
  245. Name: "dir, d",
  246. Usage: `the target path of the code,default path is "${pwd}". [option]`,
  247. },
  248. cli.StringFlag{
  249. Name: "service, srv",
  250. Usage: `the name of rpc service. [option]`,
  251. },
  252. cli.BoolFlag{
  253. Name: "idea",
  254. Usage: "whether the command execution environment is from idea plugin. [option]",
  255. },
  256. },
  257. Action: rpc.Rpc,
  258. },
  259. },
  260. },
  261. {
  262. Name: "model",
  263. Usage: "generate model code",
  264. Subcommands: []cli.Command{
  265. {
  266. Name: "mysql",
  267. Usage: `generate mysql model`,
  268. Subcommands: []cli.Command{
  269. {
  270. Name: "ddl",
  271. Usage: `generate mysql model from ddl`,
  272. Flags: []cli.Flag{
  273. cli.StringFlag{
  274. Name: "src, s",
  275. Usage: "the file path of the ddl source file",
  276. },
  277. cli.StringFlag{
  278. Name: "dir, d",
  279. Usage: "the target dir",
  280. },
  281. cli.BoolFlag{
  282. Name: "cache, c",
  283. Usage: "generate code with cache [optional]",
  284. },
  285. cli.BoolFlag{
  286. Name: "idea",
  287. Usage: "for idea plugin [optional]",
  288. },
  289. },
  290. Action: model.MysqlDDL,
  291. },
  292. {
  293. Name: "datasource",
  294. Usage: `generate model from datasource`,
  295. Flags: []cli.Flag{
  296. cli.StringFlag{
  297. Name: "url",
  298. Usage: `the data source of database,like "root:password@tcp(127.0.0.1:3306)/database`,
  299. },
  300. cli.StringFlag{
  301. Name: "table, t",
  302. Usage: `source table,tables separated by commas,like "user,course`,
  303. },
  304. cli.BoolFlag{
  305. Name: "cache, c",
  306. Usage: "generate code with cache [optional]",
  307. },
  308. cli.StringFlag{
  309. Name: "dir, d",
  310. Usage: "the target dir",
  311. },
  312. cli.BoolFlag{
  313. Name: "idea",
  314. Usage: "for idea plugin [optional]",
  315. },
  316. },
  317. Action: model.MyDataSource,
  318. },
  319. },
  320. },
  321. },
  322. },
  323. {
  324. Name: "config",
  325. Usage: "generate config json",
  326. Flags: []cli.Flag{
  327. cli.StringFlag{
  328. Name: "path, p",
  329. Usage: "the target config go file",
  330. },
  331. },
  332. Action: configgen.GenConfigCommand,
  333. },
  334. {
  335. Name: "feature",
  336. Usage: "the features of the latest version",
  337. Action: feature.Feature,
  338. },
  339. }
  340. )
  341. func main() {
  342. logx.Disable()
  343. app := cli.NewApp()
  344. app.Usage = "a cli tool to generate code"
  345. app.Version = BuildTime
  346. app.Commands = commands
  347. // cli already print error messages
  348. if err := app.Run(os.Args); err != nil {
  349. fmt.Println("error:", err)
  350. }
  351. }