goctl.go 8.0 KB

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