1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-04 10:34:55 +02:00
lazygit/pkg/utils/template.go

34 lines
794 B
Go
Raw Normal View History

2021-05-30 07:22:04 +02:00
package utils
import (
"bytes"
"strings"
"text/template"
)
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
}