2021-05-30 07:22:04 +02:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
2022-09-30 14:10:56 +02:00
|
|
|
func ResolveTemplate(templateStr string, object interface{}, funcs template.FuncMap) (string, error) {
|
|
|
|
tmpl, err := template.New("template").Funcs(funcs).Option("missingkey=error").Parse(templateStr)
|
2021-05-30 07:22:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := tmpl.Execute(&buf, object); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolvePlaceholderString populates a template with values
|
|
|
|
func ResolvePlaceholderString(str string, arguments map[string]string) string {
|
2023-01-18 13:56:22 +02:00
|
|
|
oldnews := make([]string, 0, len(arguments)*4)
|
2021-05-30 07:22:04 +02:00
|
|
|
for key, value := range arguments {
|
2023-01-18 13:56:22 +02:00
|
|
|
oldnews = append(oldnews,
|
|
|
|
"{{"+key+"}}", value,
|
|
|
|
"{{."+key+"}}", value,
|
|
|
|
)
|
2021-05-30 07:22:04 +02:00
|
|
|
}
|
2023-01-18 13:56:22 +02:00
|
|
|
return strings.NewReplacer(oldnews...).Replace(str)
|
2021-05-30 07:22:04 +02:00
|
|
|
}
|