templates.go 2.2 KB

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