template.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package gateway
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/pathx"
  6. )
  7. const (
  8. category = "gateway"
  9. etcTemplateFileFile = "etc.tpl"
  10. mainTemplateFile = "main.tpl"
  11. )
  12. //go:embed conf.yml
  13. var etcTemplate string
  14. //go:embed gateway.tpl
  15. var mainTemplate string
  16. var templates = map[string]string{
  17. etcTemplateFileFile: etcTemplate,
  18. mainTemplateFile: mainTemplate,
  19. }
  20. // GenTemplates is the entry for command goctl template,
  21. // it will create the specified category
  22. func GenTemplates() error {
  23. return pathx.InitTemplates(category, templates)
  24. }
  25. // RevertTemplate restores the deleted template files
  26. func RevertTemplate(name string) error {
  27. content, ok := templates[name]
  28. if !ok {
  29. return fmt.Errorf("%s: no such file name", name)
  30. }
  31. return pathx.CreateTemplate(category, name, content)
  32. }
  33. // Clean deletes all template files
  34. func Clean() error {
  35. return pathx.Clean(category)
  36. }
  37. // Update is used to update the template files, it will delete the existing old templates at first,
  38. // and then create the latest template files
  39. func Update() error {
  40. err := Clean()
  41. if err != nil {
  42. return err
  43. }
  44. return pathx.InitTemplates(category, templates)
  45. }
  46. // Category returns a const string value for rpc template category
  47. func Category() string {
  48. return category
  49. }