about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPhillip Johnsen <johphi@gmail.com>2018-06-27T21·37+0200
committerVincent Ambo <github@tazj.in>2018-07-14T21·21+0200
commitab059ad41c7195e5c4a3f456b7f590d881884dcf (patch)
tree63b0b512b747a135c3f1565b922c2a85970c19b8
parent6b39254917a89a854060aac5a87ad88a6d4831fa (diff)
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.
-rw-r--r--templater/templater.go6
1 files changed, 6 insertions, 0 deletions
diff --git a/templater/templater.go b/templater/templater.go
index 192aac849804..ff4e0efde7b2 100644
--- a/templater/templater.go
+++ b/templater/templater.go
@@ -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
 }