templates.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package tpl
  2. import (
  3. "fmt"
  4. "github.com/logrusorgru/aurora"
  5. "github.com/tal-tech/go-zero/core/errorx"
  6. "github.com/tal-tech/go-zero/tools/goctl/api/gogen"
  7. "github.com/tal-tech/go-zero/tools/goctl/docker"
  8. "github.com/tal-tech/go-zero/tools/goctl/kube"
  9. modelgen "github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
  10. rpcgen "github.com/tal-tech/go-zero/tools/goctl/rpc/generator"
  11. "github.com/tal-tech/go-zero/tools/goctl/util"
  12. "github.com/urfave/cli"
  13. )
  14. const templateParentPath = "/"
  15. func GenTemplates(ctx *cli.Context) error {
  16. if err := errorx.Chain(
  17. func() error {
  18. return gogen.GenTemplates(ctx)
  19. },
  20. func() error {
  21. return modelgen.GenTemplates(ctx)
  22. },
  23. func() error {
  24. return rpcgen.GenTemplates(ctx)
  25. },
  26. func() error {
  27. return docker.GenTemplates(ctx)
  28. },
  29. func() error {
  30. return kube.GenTemplates(ctx)
  31. },
  32. ); err != nil {
  33. return err
  34. }
  35. dir, err := util.GetTemplateDir(templateParentPath)
  36. if err != nil {
  37. return err
  38. }
  39. fmt.Printf("Templates are generated in %s, %s\n", aurora.Green(dir),
  40. aurora.Red("edit on your risk!"))
  41. return nil
  42. }
  43. func CleanTemplates(_ *cli.Context) error {
  44. err := errorx.Chain(
  45. func() error {
  46. return gogen.Clean()
  47. },
  48. func() error {
  49. return modelgen.Clean()
  50. },
  51. func() error {
  52. return rpcgen.Clean()
  53. },
  54. )
  55. if err != nil {
  56. return err
  57. }
  58. fmt.Printf("%s\n", aurora.Green("template are clean!"))
  59. return nil
  60. }
  61. func UpdateTemplates(ctx *cli.Context) (err error) {
  62. category := ctx.String("category")
  63. defer func() {
  64. if err == nil {
  65. fmt.Println(aurora.Green(fmt.Sprintf("%s template are update!", category)).String())
  66. }
  67. }()
  68. switch category {
  69. case docker.Category():
  70. return docker.Update()
  71. case gogen.Category():
  72. return gogen.Update()
  73. case kube.Category():
  74. return kube.Update()
  75. case rpcgen.Category():
  76. return rpcgen.Update()
  77. case modelgen.Category():
  78. return modelgen.Update()
  79. default:
  80. err = fmt.Errorf("unexpected category: %s", category)
  81. return
  82. }
  83. }
  84. func RevertTemplates(ctx *cli.Context) (err error) {
  85. category := ctx.String("category")
  86. filename := ctx.String("name")
  87. defer func() {
  88. if err == nil {
  89. fmt.Println(aurora.Green(fmt.Sprintf("%s template are reverted!", filename)).String())
  90. }
  91. }()
  92. switch category {
  93. case docker.Category():
  94. return docker.RevertTemplate(filename)
  95. case kube.Category():
  96. return kube.RevertTemplate(filename)
  97. case gogen.Category():
  98. return gogen.RevertTemplate(filename)
  99. case rpcgen.Category():
  100. return rpcgen.RevertTemplate(filename)
  101. case modelgen.Category():
  102. return modelgen.RevertTemplate(filename)
  103. default:
  104. err = fmt.Errorf("unexpected category: %s", category)
  105. return
  106. }
  107. }