templates.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // GenTemplates wtites the latest template text into file which is not exists
  16. func GenTemplates(ctx *cli.Context) error {
  17. if err := errorx.Chain(
  18. func() error {
  19. return gogen.GenTemplates(ctx)
  20. },
  21. func() error {
  22. return modelgen.GenTemplates(ctx)
  23. },
  24. func() error {
  25. return rpcgen.GenTemplates(ctx)
  26. },
  27. func() error {
  28. return docker.GenTemplates(ctx)
  29. },
  30. func() error {
  31. return kube.GenTemplates(ctx)
  32. },
  33. ); err != nil {
  34. return err
  35. }
  36. dir, err := util.GetTemplateDir(templateParentPath)
  37. if err != nil {
  38. return err
  39. }
  40. fmt.Printf("Templates are generated in %s, %s\n", aurora.Green(dir),
  41. aurora.Red("edit on your risk!"))
  42. return nil
  43. }
  44. // CleanTemplates deletes all templates
  45. func CleanTemplates(_ *cli.Context) error {
  46. err := errorx.Chain(
  47. func() error {
  48. return gogen.Clean()
  49. },
  50. func() error {
  51. return modelgen.Clean()
  52. },
  53. func() error {
  54. return rpcgen.Clean()
  55. },
  56. )
  57. if err != nil {
  58. return err
  59. }
  60. fmt.Printf("%s\n", aurora.Green("template are clean!"))
  61. return nil
  62. }
  63. // UpdateTemplates wtites the latest template text into file,
  64. // it will delete the oldler templates if there are exists
  65. func UpdateTemplates(ctx *cli.Context) (err error) {
  66. category := ctx.String("category")
  67. defer func() {
  68. if err == nil {
  69. fmt.Println(aurora.Green(fmt.Sprintf("%s template are update!", category)).String())
  70. }
  71. }()
  72. switch category {
  73. case docker.Category():
  74. return docker.Update()
  75. case gogen.Category():
  76. return gogen.Update()
  77. case kube.Category():
  78. return kube.Update()
  79. case rpcgen.Category():
  80. return rpcgen.Update()
  81. case modelgen.Category():
  82. return modelgen.Update()
  83. default:
  84. err = fmt.Errorf("unexpected category: %s", category)
  85. return
  86. }
  87. }
  88. // RevertTemplates will overwrite the old template content with the new template
  89. func RevertTemplates(ctx *cli.Context) (err error) {
  90. category := ctx.String("category")
  91. filename := ctx.String("name")
  92. defer func() {
  93. if err == nil {
  94. fmt.Println(aurora.Green(fmt.Sprintf("%s template are reverted!", filename)).String())
  95. }
  96. }()
  97. switch category {
  98. case docker.Category():
  99. return docker.RevertTemplate(filename)
  100. case kube.Category():
  101. return kube.RevertTemplate(filename)
  102. case gogen.Category():
  103. return gogen.RevertTemplate(filename)
  104. case rpcgen.Category():
  105. return rpcgen.RevertTemplate(filename)
  106. case modelgen.Category():
  107. return modelgen.RevertTemplate(filename)
  108. default:
  109. err = fmt.Errorf("unexpected category: %s", category)
  110. return
  111. }
  112. }