template.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. modelCustomTemplateFile = "model_custom.tpl"
  11. modelTypesTemplateFile = "model_types.tpl"
  12. errTemplateFile = "err.tpl"
  13. )
  14. var templates = map[string]string{
  15. modelTemplateFile: template.ModelText,
  16. modelCustomTemplateFile: template.ModelCustomText,
  17. modelTypesTemplateFile: template.ModelTypesText,
  18. errTemplateFile: template.Error,
  19. }
  20. // Category returns the mongo category.
  21. func Category() string {
  22. return category
  23. }
  24. // Clean cleans the mongo templates.
  25. func Clean() error {
  26. return pathx.Clean(category)
  27. }
  28. // Templates initializes the mongo templates.
  29. func Templates() error {
  30. return pathx.InitTemplates(category, templates)
  31. }
  32. // RevertTemplate reverts the given template.
  33. func RevertTemplate(name string) error {
  34. content, ok := templates[name]
  35. if !ok {
  36. return fmt.Errorf("%s: no such file name", name)
  37. }
  38. return pathx.CreateTemplate(category, name, content)
  39. }
  40. // Update cleans and updates the templates.
  41. func Update() error {
  42. err := Clean()
  43. if err != nil {
  44. return err
  45. }
  46. return pathx.InitTemplates(category, templates)
  47. }