template.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package gen
  2. import (
  3. "fmt"
  4. "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
  5. "github.com/tal-tech/go-zero/tools/goctl/util"
  6. "github.com/urfave/cli"
  7. )
  8. const (
  9. category = "model"
  10. deleteTemplateFile = "delete.tpl"
  11. deleteMethodTemplateFile = "interface-delete.tpl"
  12. fieldTemplateFile = "field.tpl"
  13. findOneTemplateFile = "find-one.tpl"
  14. findOneMethodTemplateFile = "interface-find-one.tpl"
  15. findOneByFieldTemplateFile = "find-one-by-field.tpl"
  16. findOneByFieldMethodTemplateFile = "interface-find-one-by-field.tpl"
  17. findOneByFieldExtraMethodTemplateFile = "find-one-by-field-extra-method.tpl"
  18. importsTemplateFile = "import.tpl"
  19. importsWithNoCacheTemplateFile = "import-no-cache.tpl"
  20. insertTemplateFile = "insert.tpl"
  21. insertTemplateMethodFile = "interface-insert.tpl"
  22. modelTemplateFile = "model.tpl"
  23. modelNewTemplateFile = "model-new.tpl"
  24. tagTemplateFile = "tag.tpl"
  25. typesTemplateFile = "types.tpl"
  26. updateTemplateFile = "update.tpl"
  27. updateMethodTemplateFile = "interface-update.tpl"
  28. varTemplateFile = "var.tpl"
  29. errTemplateFile = "err.tpl"
  30. )
  31. var templates = map[string]string{
  32. deleteTemplateFile: template.Delete,
  33. deleteMethodTemplateFile: template.DeleteMethod,
  34. fieldTemplateFile: template.Field,
  35. findOneTemplateFile: template.FindOne,
  36. findOneMethodTemplateFile: template.FindOneMethod,
  37. findOneByFieldTemplateFile: template.FindOneByField,
  38. findOneByFieldMethodTemplateFile: template.FindOneByFieldMethod,
  39. findOneByFieldExtraMethodTemplateFile: template.FindOneByFieldExtraMethod,
  40. importsTemplateFile: template.Imports,
  41. importsWithNoCacheTemplateFile: template.ImportsNoCache,
  42. insertTemplateFile: template.Insert,
  43. insertTemplateMethodFile: template.InsertMethod,
  44. modelTemplateFile: template.Model,
  45. modelNewTemplateFile: template.New,
  46. tagTemplateFile: template.Tag,
  47. typesTemplateFile: template.Types,
  48. updateTemplateFile: template.Update,
  49. varTemplateFile: template.Vars,
  50. errTemplateFile: template.Error,
  51. }
  52. func Category() string {
  53. return category
  54. }
  55. func Clean() error {
  56. return util.Clean(category)
  57. }
  58. func GenTemplates(_ *cli.Context) error {
  59. return util.InitTemplates(category, templates)
  60. }
  61. func RevertTemplate(name string) error {
  62. content, ok := templates[name]
  63. if !ok {
  64. return fmt.Errorf("%s: no such file name", name)
  65. }
  66. return util.CreateTemplate(category, name, content)
  67. }
  68. func Update() error {
  69. err := Clean()
  70. if err != nil {
  71. return err
  72. }
  73. return util.InitTemplates(category, templates)
  74. }