goctl.go 9.1 KB

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