Merge pull request #1230 from technosophos/feat/1225-wqtoyaml

feat(tiller): add toYaml template function
pull/1217/merge
Matt Butcher 8 years ago committed by GitHub
commit e9a69f3752

@ -24,6 +24,8 @@ import (
"text/template"
"github.com/Masterminds/sprig"
"github.com/ghodss/yaml"
"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/proto/hapi/chart"
)
@ -49,11 +51,23 @@ func New() *Engine {
f := sprig.TxtFuncMap()
delete(f, "env")
delete(f, "expandenv")
// Add a function to convert to YAML:
f["toYaml"] = toYaml
return &Engine{
FuncMap: f,
}
}
func toYaml(v interface{}) string {
data, err := yaml.Marshal(v)
if err != nil {
// Swallow errors inside of a template.
return ""
}
return string(data)
}
// Render takes a chart, optional values, and value overrides, and attempts to render the Go templates.
//
// Render can be called repeatedly on the same engine.

@ -27,6 +27,19 @@ import (
"github.com/golang/protobuf/ptypes/any"
)
func TestToYaml(t *testing.T) {
expect := "foo: bar\n"
v := struct {
Foo string `json:"foo"`
}{
Foo: "bar",
}
if got := toYaml(v); got != expect {
t.Errorf("Expected %q, got %q", expect, got)
}
}
func TestEngine(t *testing.T) {
e := New()

Loading…
Cancel
Save