templates.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 gogen.GetCategory():
  70. return gogen.Update(category)
  71. case rpcgen.GetCategory():
  72. return rpcgen.Update(category)
  73. case modelgen.GetCategory():
  74. return modelgen.Update(category)
  75. default:
  76. err = fmt.Errorf("unexpected category: %s", category)
  77. return
  78. }
  79. }
  80. func RevertTemplates(ctx *cli.Context) (err error) {
  81. category := ctx.String("category")
  82. filename := ctx.String("name")
  83. defer func() {
  84. if err == nil {
  85. fmt.Println(aurora.Green(fmt.Sprintf("%s template are reverted!", filename)).String())
  86. }
  87. }()
  88. switch category {
  89. case gogen.GetCategory():
  90. return gogen.RevertTemplate(filename)
  91. case rpcgen.GetCategory():
  92. return rpcgen.RevertTemplate(filename)
  93. case modelgen.GetCategory():
  94. return modelgen.RevertTemplate(filename)
  95. default:
  96. err = fmt.Errorf("unexpected category: %s", category)
  97. return
  98. }
  99. }