2020-05-25 19:48:59 +02:00
|
|
|
package piperutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ExecuteTemplate parses the provided template, substitutes values and returns the output
|
|
|
|
func ExecuteTemplate(txtTemplate string, context interface{}) (string, error) {
|
|
|
|
return ExecuteTemplateFunctions(txtTemplate, nil, context)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecuteTemplateFunctions parses the provided template, applies the transformation functions, substitutes values and returns the output
|
|
|
|
func ExecuteTemplateFunctions(txtTemplate string, functionMap template.FuncMap, context interface{}) (string, error) {
|
|
|
|
template := template.New("tmp")
|
|
|
|
if functionMap != nil {
|
|
|
|
template = template.Funcs(functionMap)
|
|
|
|
}
|
|
|
|
template, err := template.Parse(txtTemplate)
|
|
|
|
if err != nil {
|
2020-09-24 07:41:06 +02:00
|
|
|
return "<nil>", fmt.Errorf("Failed to parse template definition %v: %w", txtTemplate, err)
|
2020-05-25 19:48:59 +02:00
|
|
|
}
|
|
|
|
var output bytes.Buffer
|
|
|
|
err = template.Execute(&output, context)
|
|
|
|
if err != nil {
|
2020-09-24 07:41:06 +02:00
|
|
|
return "<nil>", fmt.Errorf("Failed to transform template definition %v: %w", txtTemplate, err)
|
2020-05-25 19:48:59 +02:00
|
|
|
}
|
|
|
|
return output.String(), nil
|
|
|
|
}
|