templates.go 2.3 KB

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