template.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package template
  2. import (
  3. "bytes"
  4. "git.sxidc.com/go-tools/utils/fileutils"
  5. "github.com/Masterminds/sprig/v3"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "text/template"
  10. )
  11. type ParseTemplateOption func(tpl *template.Template)
  12. func WithDelim(leftDelim string, rightDelim string) ParseTemplateOption {
  13. return func(tpl *template.Template) {
  14. tpl.Delims(leftDelim, rightDelim)
  15. }
  16. }
  17. func ParseTemplatesDir(templateDir string, outputDir string, params any, opts ...ParseTemplateOption) error {
  18. files, err := fileutils.GetDirFiles(templateDir)
  19. if err != nil {
  20. return err
  21. }
  22. for _, file := range files {
  23. subFileName := strings.ReplaceAll(file, templateDir, "")
  24. subFileName = strings.TrimSuffix(subFileName, ".tpl")
  25. outputFile := filepath.Join(outputDir, subFileName)
  26. err := ParseTemplateFileToFile(file, outputFile, params, opts...)
  27. if err != nil {
  28. return err
  29. }
  30. }
  31. return nil
  32. }
  33. func ParseTemplateFileToFile(inputFilePath string, outputFilePath string, params any, opts ...ParseTemplateOption) error {
  34. parsedBytes, err := ParseTemplateFileToBytes(inputFilePath, params, opts...)
  35. if err != nil {
  36. return err
  37. }
  38. outputDir := filepath.Dir(outputFilePath)
  39. if !fileutils.PathExists(outputDir) {
  40. err := os.MkdirAll(outputDir, os.ModeDir|os.ModePerm)
  41. if err != nil {
  42. return err
  43. }
  44. }
  45. err = os.WriteFile(outputFilePath, parsedBytes, os.ModePerm)
  46. if err != nil {
  47. return err
  48. }
  49. return nil
  50. }
  51. func ParseTemplateFileToString(inputFilePath string, params any, opts ...ParseTemplateOption) (string, error) {
  52. inputBytes, err := os.ReadFile(inputFilePath)
  53. if err != nil {
  54. return "", err
  55. }
  56. parsedBytes, err := parseTemplateStringToBytes(newTemplate(inputFilePath, opts...), string(inputBytes), params)
  57. if err != nil {
  58. return "", err
  59. }
  60. return string(parsedBytes), nil
  61. }
  62. func ParseTemplateFileToBytes(inputFilePath string, params any, opts ...ParseTemplateOption) ([]byte, error) {
  63. inputBytes, err := os.ReadFile(inputFilePath)
  64. if err != nil {
  65. return nil, err
  66. }
  67. return parseTemplateStringToBytes(newTemplate(inputFilePath, opts...), string(inputBytes), params)
  68. }
  69. func ParseTemplateStringToString(inputStr string, params any, opts ...ParseTemplateOption) (string, error) {
  70. parsedBytes, err := parseTemplateStringToBytes(newTemplate(inputStr, opts...), inputStr, params)
  71. if err != nil {
  72. return "", err
  73. }
  74. return string(parsedBytes), nil
  75. }
  76. func ParseTemplateStringToBytes(inputStr string, params any, opts ...ParseTemplateOption) ([]byte, error) {
  77. return parseTemplateStringToBytes(newTemplate(inputStr, opts...), inputStr, params)
  78. }
  79. func newTemplate(name string, opts ...ParseTemplateOption) *template.Template {
  80. tpl := template.New(name)
  81. for _, opt := range opts {
  82. opt(tpl)
  83. }
  84. tpl.Funcs(sprig.FuncMap())
  85. tpl.Funcs(templateFuncMap)
  86. return tpl
  87. }
  88. func parseTemplateStringToBytes(tpl *template.Template, inputStr string, params any) ([]byte, error) {
  89. tpl, err := tpl.Parse(inputStr)
  90. if err != nil {
  91. return nil, err
  92. }
  93. buffer := &bytes.Buffer{}
  94. err = tpl.Execute(buffer, params)
  95. if err != nil {
  96. return nil, err
  97. }
  98. return buffer.Bytes(), nil
  99. }