template.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. func GenTemplates(_ *cli.Context) error {
  37. return util.InitTemplates(category, templates)
  38. }
  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 util.CreateTemplate(category, name, content)
  45. }
  46. func Clean() error {
  47. return util.Clean(category)
  48. }
  49. func Update() error {
  50. err := Clean()
  51. if err != nil {
  52. return err
  53. }
  54. return util.InitTemplates(category, templates)
  55. }
  56. func Category() string {
  57. return category
  58. }