goctl.go 9.0 KB

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