templates.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package tpl
  2. import (
  3. "fmt"
  4. "git.i2edu.net/i2/go-zero/core/errorx"
  5. "git.i2edu.net/i2/go-zero/tools/goctl/api/gogen"
  6. "git.i2edu.net/i2/go-zero/tools/goctl/docker"
  7. "git.i2edu.net/i2/go-zero/tools/goctl/kube"
  8. mongogen "git.i2edu.net/i2/go-zero/tools/goctl/model/mongo/generate"
  9. modelgen "git.i2edu.net/i2/go-zero/tools/goctl/model/sql/gen"
  10. rpcgen "git.i2edu.net/i2/go-zero/tools/goctl/rpc/generator"
  11. "git.i2edu.net/i2/go-zero/tools/goctl/util"
  12. "github.com/logrusorgru/aurora"
  13. "github.com/urfave/cli"
  14. )
  15. const templateParentPath = "/"
  16. // GenTemplates writes the latest template text into file which is not exists
  17. func GenTemplates(ctx *cli.Context) error {
  18. if err := errorx.Chain(
  19. func() error {
  20. return gogen.GenTemplates(ctx)
  21. },
  22. func() error {
  23. return modelgen.GenTemplates(ctx)
  24. },
  25. func() error {
  26. return rpcgen.GenTemplates(ctx)
  27. },
  28. func() error {
  29. return docker.GenTemplates(ctx)
  30. },
  31. func() error {
  32. return kube.GenTemplates(ctx)
  33. },
  34. func() error {
  35. return mongogen.Templates(ctx)
  36. },
  37. ); err != nil {
  38. return err
  39. }
  40. dir, err := util.GetTemplateDir(templateParentPath)
  41. if err != nil {
  42. return err
  43. }
  44. fmt.Printf("Templates are generated in %s, %s\n", aurora.Green(dir),
  45. aurora.Red("edit on your risk!"))
  46. return nil
  47. }
  48. // CleanTemplates deletes all templates
  49. func CleanTemplates(_ *cli.Context) error {
  50. err := errorx.Chain(
  51. func() error {
  52. return gogen.Clean()
  53. },
  54. func() error {
  55. return modelgen.Clean()
  56. },
  57. func() error {
  58. return rpcgen.Clean()
  59. },
  60. func() error {
  61. return docker.Clean()
  62. },
  63. func() error {
  64. return kube.Clean()
  65. },
  66. func() error {
  67. return mongogen.Clean()
  68. },
  69. )
  70. if err != nil {
  71. return err
  72. }
  73. fmt.Printf("%s\n", aurora.Green("template are clean!"))
  74. return nil
  75. }
  76. // UpdateTemplates writes the latest template text into file,
  77. // it will delete the older templates if there are exists
  78. func UpdateTemplates(ctx *cli.Context) (err error) {
  79. category := ctx.String("category")
  80. defer func() {
  81. if err == nil {
  82. fmt.Println(aurora.Green(fmt.Sprintf("%s template are update!", category)).String())
  83. }
  84. }()
  85. switch category {
  86. case docker.Category():
  87. return docker.Update()
  88. case gogen.Category():
  89. return gogen.Update()
  90. case kube.Category():
  91. return kube.Update()
  92. case rpcgen.Category():
  93. return rpcgen.Update()
  94. case modelgen.Category():
  95. return modelgen.Update()
  96. case mongogen.Category():
  97. return mongogen.Update()
  98. default:
  99. err = fmt.Errorf("unexpected category: %s", category)
  100. return
  101. }
  102. }
  103. // RevertTemplates will overwrite the old template content with the new template
  104. func RevertTemplates(ctx *cli.Context) (err error) {
  105. category := ctx.String("category")
  106. filename := ctx.String("name")
  107. defer func() {
  108. if err == nil {
  109. fmt.Println(aurora.Green(fmt.Sprintf("%s template are reverted!", filename)).String())
  110. }
  111. }()
  112. switch category {
  113. case docker.Category():
  114. return docker.RevertTemplate(filename)
  115. case kube.Category():
  116. return kube.RevertTemplate(filename)
  117. case gogen.Category():
  118. return gogen.RevertTemplate(filename)
  119. case rpcgen.Category():
  120. return rpcgen.RevertTemplate(filename)
  121. case modelgen.Category():
  122. return modelgen.RevertTemplate(filename)
  123. case mongogen.Category():
  124. return mongogen.RevertTemplate(filename)
  125. default:
  126. err = fmt.Errorf("unexpected category: %s", category)
  127. return
  128. }
  129. }