template.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // Category returns model const value
  53. func Category() string {
  54. return category
  55. }
  56. // Clean deletes all template files
  57. func Clean() error {
  58. return util.Clean(category)
  59. }
  60. // GenTemplates creates template files if not exists
  61. func GenTemplates(_ *cli.Context) error {
  62. return util.InitTemplates(category, templates)
  63. }
  64. // RevertTemplate recovers the delete template files
  65. func RevertTemplate(name string) error {
  66. content, ok := templates[name]
  67. if !ok {
  68. return fmt.Errorf("%s: no such file name", name)
  69. }
  70. return util.CreateTemplate(category, name, content)
  71. }
  72. // Update provides template clean and init
  73. func Update() error {
  74. err := Clean()
  75. if err != nil {
  76. return err
  77. }
  78. return util.InitTemplates(category, templates)
  79. }