template.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package gogen
  2. import (
  3. "fmt"
  4. "github.com/tal-tech/go-zero/tools/goctl/util"
  5. "github.com/urfave/cli"
  6. )
  7. const (
  8. category = "api"
  9. configTemplateFile = "config.tpl"
  10. contextTemplateFile = "context.tpl"
  11. etcTemplateFile = "etc.tpl"
  12. handlerTemplateFile = "handler.tpl"
  13. logicTemplateFile = "logic.tpl"
  14. mainTemplateFile = "main.tpl"
  15. )
  16. var templates = map[string]string{
  17. configTemplateFile: configTemplate,
  18. contextTemplateFile: contextTemplate,
  19. etcTemplateFile: etcTemplate,
  20. handlerTemplateFile: handlerTemplate,
  21. logicTemplateFile: logicTemplate,
  22. mainTemplateFile: mainTemplate,
  23. }
  24. // Category returns the category of the api files.
  25. func Category() string {
  26. return category
  27. }
  28. // Clean cleans the generated deployment files.
  29. func Clean() error {
  30. return util.Clean(category)
  31. }
  32. // GenTemplates generates api template files.
  33. func GenTemplates(_ *cli.Context) error {
  34. return util.InitTemplates(category, templates)
  35. }
  36. // RevertTemplate reverts the given template file to the default value.
  37. func RevertTemplate(name string) error {
  38. content, ok := templates[name]
  39. if !ok {
  40. return fmt.Errorf("%s: no such file name", name)
  41. }
  42. return util.CreateTemplate(category, name, content)
  43. }
  44. // Update updates the template files to the templates built in current goctl.
  45. func Update() error {
  46. err := Clean()
  47. if err != nil {
  48. return err
  49. }
  50. return util.InitTemplates(category, templates)
  51. }