template.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package generator
  2. import (
  3. "fmt"
  4. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/pathx"
  5. )
  6. const (
  7. category = "rpc"
  8. callTemplateFile = "call.tpl"
  9. callInterfaceFunctionTemplateFile = "call-interface-func.tpl"
  10. callFunctionTemplateFile = "call-func.tpl"
  11. configTemplateFileFile = "config.tpl"
  12. etcTemplateFileFile = "etc.tpl"
  13. logicTemplateFileFile = "logic.tpl"
  14. logicFuncTemplateFileFile = "logic-func.tpl"
  15. mainTemplateFile = "main.tpl"
  16. serverTemplateFile = "server.tpl"
  17. serverFuncTemplateFile = "server-func.tpl"
  18. svcTemplateFile = "svc.tpl"
  19. rpcTemplateFile = "template.tpl"
  20. )
  21. var templates = map[string]string{
  22. callTemplateFile: callTemplateText,
  23. configTemplateFileFile: configTemplate,
  24. etcTemplateFileFile: etcTemplate,
  25. logicTemplateFileFile: logicTemplate,
  26. logicFuncTemplateFileFile: logicFunctionTemplate,
  27. mainTemplateFile: mainTemplate,
  28. serverTemplateFile: serverTemplate,
  29. serverFuncTemplateFile: functionTemplate,
  30. svcTemplateFile: svcTemplate,
  31. rpcTemplateFile: rpcTemplateText,
  32. }
  33. // GenTemplates is the entry for command goctl template,
  34. // it will create the specified category
  35. func GenTemplates() error {
  36. return pathx.InitTemplates(category, templates)
  37. }
  38. // RevertTemplate restores the deleted template files
  39. func RevertTemplate(name string) error {
  40. content, ok := templates[name]
  41. if !ok {
  42. return fmt.Errorf("%s: no such file name", name)
  43. }
  44. return pathx.CreateTemplate(category, name, content)
  45. }
  46. // Clean deletes all template files
  47. func Clean() error {
  48. return pathx.Clean(category)
  49. }
  50. // Update is used to update the template files, it will delete the existing old templates at first,
  51. // and then create the latest template files
  52. func Update() error {
  53. err := Clean()
  54. if err != nil {
  55. return err
  56. }
  57. return pathx.InitTemplates(category, templates)
  58. }
  59. // Category returns a const string value for rpc template category
  60. func Category() string {
  61. return category
  62. }