mirror of
https://github.com/helmfile/helmfile.git
synced 2026-10-10 02:51:53 +02:00
Enhances Helmfile to print more helpful message on error while calling `exec` template function. Helmfile has been printing error messages like the below: ``` in ./helmfile.yaml: error during helmfile.yaml.part.0 parsing: template: stringTemplate:5:8: executing "stringTemplate" at <exec "./exectest.sha" (list)>: error calling exec: exit status 1 ``` Adding captured stdout and stderr, with some indentation to make it readable, it now produces the following message on missing executable: ``` $ make build && ./helmfile build go build in ./helmfile.yaml: error during helmfile.yaml.part.0 parsing: template: stringTemplate:5:8: executing "stringTemplate" at <exec "./exectest.sha" (list)>: error calling exec: fork/exec ./exectest.sha: no such file or directory COMMAND: ./exectest.sha ERROR: fork/exec ./exectest.sha: no such file or directory ``` On non-zero exit status without output: ``` $ make build && ./helmfile build go build in ./helmfile.yaml: error during helmfile.yaml.part.0 parsing: template: stringTemplate:5:8: executing "stringTemplate" at <exec "./exectest.sh" (list)>: error calling exec: exit status 1 COMMAND: ./exectest.sh ERROR: exit status 1 ``` On non-zero exit status with output: ``` $ make build && ./helmfile build go build in ./helmfile.yaml: error during helmfile.yaml.part.0 parsing: template: stringTemplate:5:8: executing "stringTemplate" at <exec "./exectest.sh" (list)>: error calling exec: exit status 2 COMMAND: ./exectest.sh ERROR: exit status 2 COMBINED OUTPUT: out1 err1 ``` Resolves #1158
42 lines
811 B
Go
42 lines
811 B
Go
package tmpl
|
|
|
|
import (
|
|
"bytes"
|
|
"github.com/Masterminds/sprig"
|
|
"text/template"
|
|
)
|
|
|
|
func (c *Context) newTemplate() *template.Template {
|
|
funcMap := sprig.TxtFuncMap()
|
|
for name, f := range c.createFuncMap() {
|
|
funcMap[name] = f
|
|
}
|
|
tmpl := template.New("stringTemplate").Funcs(funcMap)
|
|
if c.preRender {
|
|
tmpl = tmpl.Option("missingkey=zero")
|
|
} else {
|
|
tmpl = tmpl.Option("missingkey=error")
|
|
}
|
|
return tmpl
|
|
}
|
|
|
|
func (c *Context) RenderTemplateToBuffer(s string, data ...interface{}) (*bytes.Buffer, error) {
|
|
var t, parseErr = c.newTemplate().Parse(s)
|
|
if parseErr != nil {
|
|
return nil, parseErr
|
|
}
|
|
|
|
var tplString bytes.Buffer
|
|
var d interface{}
|
|
if len(data) > 0 {
|
|
d = data[0]
|
|
}
|
|
var execErr = t.Execute(&tplString, d)
|
|
|
|
if execErr != nil {
|
|
return &tplString, execErr
|
|
}
|
|
|
|
return &tplString, nil
|
|
}
|