template.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package generate
  2. import (
  3. "fmt"
  4. "github.com/zeromicro/go-zero/tools/goctl/model/mongo/template"
  5. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  6. )
  7. const (
  8. category = "mongo"
  9. modelTemplateFile = "model.tpl"
  10. errTemplateFile = "err.tpl"
  11. )
  12. var templates = map[string]string{
  13. modelTemplateFile: template.Text,
  14. errTemplateFile: template.Error,
  15. }
  16. // Category returns the mongo category.
  17. func Category() string {
  18. return category
  19. }
  20. // Clean cleans the mongo templates.
  21. func Clean() error {
  22. return pathx.Clean(category)
  23. }
  24. // Templates initializes the mongo templates.
  25. func Templates() error {
  26. return pathx.InitTemplates(category, templates)
  27. }
  28. // RevertTemplate reverts the given template.
  29. func RevertTemplate(name string) error {
  30. content, ok := templates[name]
  31. if !ok {
  32. return fmt.Errorf("%s: no such file name", name)
  33. }
  34. return pathx.CreateTemplate(category, name, content)
  35. }
  36. // Update cleans and updates the templates.
  37. func Update() error {
  38. err := Clean()
  39. if err != nil {
  40. return err
  41. }
  42. return pathx.InitTemplates(category, templates)
  43. }