goctl.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/tsgen"
  13. "github.com/tal-tech/go-zero/tools/goctl/api/validate"
  14. "github.com/tal-tech/go-zero/tools/goctl/configgen"
  15. "github.com/tal-tech/go-zero/tools/goctl/docker"
  16. "github.com/tal-tech/go-zero/tools/goctl/feature"
  17. "github.com/urfave/cli"
  18. )
  19. var (
  20. BuildTime = "not set"
  21. commands = []cli.Command{
  22. {
  23. Name: "api",
  24. Usage: "generate api related files",
  25. Flags: []cli.Flag{
  26. cli.StringFlag{
  27. Name: "o",
  28. Usage: "the output api file",
  29. },
  30. },
  31. Action: apigen.ApiCommand,
  32. Subcommands: []cli.Command{
  33. {
  34. Name: "format",
  35. Usage: "format api files",
  36. Flags: []cli.Flag{
  37. cli.StringFlag{
  38. Name: "dir",
  39. Usage: "the format target dir",
  40. },
  41. cli.BoolFlag{
  42. Name: "p",
  43. Usage: "print result to console",
  44. },
  45. cli.BoolFlag{
  46. Name: "iu",
  47. Usage: "ignore update",
  48. Required: false,
  49. },
  50. },
  51. Action: format.GoFormatApi,
  52. },
  53. {
  54. Name: "validate",
  55. Usage: "validate api file",
  56. Flags: []cli.Flag{
  57. cli.StringFlag{
  58. Name: "api",
  59. Usage: "validate target api file",
  60. },
  61. },
  62. Action: validate.GoValidateApi,
  63. },
  64. {
  65. Name: "doc",
  66. Usage: "generate doc files",
  67. Flags: []cli.Flag{
  68. cli.StringFlag{
  69. Name: "dir",
  70. Usage: "the target dir",
  71. },
  72. },
  73. Action: docgen.DocCommand,
  74. },
  75. {
  76. Name: "go",
  77. Usage: "generate go files for provided api in yaml file",
  78. Flags: []cli.Flag{
  79. cli.StringFlag{
  80. Name: "dir",
  81. Usage: "the target dir",
  82. },
  83. cli.StringFlag{
  84. Name: "api",
  85. Usage: "the api file",
  86. },
  87. },
  88. Action: gogen.GoCommand,
  89. },
  90. {
  91. Name: "java",
  92. Usage: "generate java files for provided api in api file",
  93. Flags: []cli.Flag{
  94. cli.StringFlag{
  95. Name: "dir",
  96. Usage: "the target dir",
  97. },
  98. cli.StringFlag{
  99. Name: "api",
  100. Usage: "the api file",
  101. },
  102. },
  103. Action: javagen.JavaCommand,
  104. },
  105. {
  106. Name: "ts",
  107. Usage: "generate ts files for provided api in api file",
  108. Flags: []cli.Flag{
  109. cli.StringFlag{
  110. Name: "dir",
  111. Usage: "the target dir",
  112. },
  113. cli.StringFlag{
  114. Name: "api",
  115. Usage: "the api file",
  116. },
  117. cli.StringFlag{
  118. Name: "webapi",
  119. Usage: "the web api file path",
  120. Required: false,
  121. },
  122. cli.StringFlag{
  123. Name: "caller",
  124. Usage: "the web api caller",
  125. Required: false,
  126. },
  127. cli.BoolFlag{
  128. Name: "unwrap",
  129. Usage: "unwrap the webapi caller for import",
  130. Required: false,
  131. },
  132. },
  133. Action: tsgen.TsCommand,
  134. },
  135. {
  136. Name: "dart",
  137. Usage: "generate dart files for provided api in api file",
  138. Flags: []cli.Flag{
  139. cli.StringFlag{
  140. Name: "dir",
  141. Usage: "the target dir",
  142. },
  143. cli.StringFlag{
  144. Name: "api",
  145. Usage: "the api file",
  146. },
  147. },
  148. Action: dartgen.DartCommand,
  149. },
  150. },
  151. },
  152. {
  153. Name: "docker",
  154. Usage: "generate Dockerfile and Makefile",
  155. Flags: []cli.Flag{
  156. cli.StringFlag{
  157. Name: "go",
  158. Usage: "the file that contains main function",
  159. },
  160. cli.StringFlag{
  161. Name: "namespace, n",
  162. Usage: "which namespace of kubernetes to deploy the service",
  163. },
  164. },
  165. Action: docker.DockerCommand,
  166. },
  167. {
  168. Name: "model",
  169. Usage: "generate sql model",
  170. Flags: []cli.Flag{
  171. cli.StringFlag{
  172. Name: "config, c",
  173. Usage: "the file that contains main function",
  174. },
  175. cli.StringFlag{
  176. Name: "dir, d",
  177. Usage: "the target dir",
  178. },
  179. },
  180. },
  181. {
  182. Name: "config",
  183. Usage: "generate config json",
  184. Flags: []cli.Flag{
  185. cli.StringFlag{
  186. Name: "path, p",
  187. Usage: "the target config go file",
  188. },
  189. },
  190. Action: configgen.GenConfigCommand,
  191. },
  192. {
  193. Name: "feature",
  194. Usage: "the features of the latest version",
  195. Action: feature.Feature,
  196. },
  197. }
  198. )
  199. func main() {
  200. logx.Disable()
  201. app := cli.NewApp()
  202. app.Usage = "a cli tool to generate code"
  203. app.Version = BuildTime
  204. app.Commands = commands
  205. // cli already print error messages
  206. if err := app.Run(os.Args); err != nil {
  207. fmt.Println("error:", err)
  208. }
  209. }