template.go 2.3 KB

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