feat(templater): override sprig default function with guarded alternative

These changes overrides the `default` function provided by sprig with
an alternative to retrieve variable values from variables that might
not have been declared at all.

Referencing a variable in a template that is not declared, will lead
to the underlying templating functionality raising an error, causing
kontemplate to exit.

The override alternative to `default` accepts a second string argument
with the variable name. If the variable in question has not been
declared the first argument's value would be returned, just as the
original `default` function does.
This commit is contained in:
Phillip Johnsen 2018-06-27 23:37:16 +02:00 committed by Vincent Ambo
parent 6b39254917
commit ab059ad41c

View file

@ -203,7 +203,13 @@ func templateFuncs(c *context.Context, rs *context.ResourceSet) template.FuncMap
return string(data), nil
}
m["default"] = func(defaultVal interface{}, varName string) interface{} {
if val, ok := rs.Values[varName]; ok {
return val
}
return defaultVal
}
return m
}