From 5cf9d53e80accaeede1b4e38772d7d53c0190549 Mon Sep 17 00:00:00 2001 From: Phillip Johnsen Date: Wed, 30 May 2018 21:43:31 +0200 Subject: feat(context): allow explicit variables to be defined as argument These changes allows variables to be defined when executing `kontemplate` via one or more `--variable` arguments. With this in place one can either define new variables or override existing variables loaded from a file: ``` $ kontemplate apply --variable version=v1.0 example/fancy-app.yaml ``` This avoids the need to write variables into a temporary file that is only needed to provide "external variables" into resource sets. Closes https://github.com/tazjin/kontemplate/issues/122 --- context/context.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'context/context.go') diff --git a/context/context.go b/context/context.go index 9996f31faa5f..1a2e5c88cc09 100644 --- a/context/context.go +++ b/context/context.go @@ -12,6 +12,7 @@ package context import ( "fmt" "path" + "strings" "github.com/tazjin/kontemplate/util" ) @@ -158,3 +159,23 @@ func loadDefaultValues(rs *ResourceSet, c *Context) *map[string]interface{} { // errors here. return &rs.Values } + +// New variables can be defined or default values overridden with command line arguments when executing kontemplate. +func (ctx *Context) SetVariablesFromArguments(vars *[]string) error { + // Resource set files might not have defined any global variables, if so we have to + // create that a map before potentially writing variables into it + if ctx.Global == nil { + ctx.Global = make(map[string]interface{}, len(*vars)) + } + + for _, v := range *vars { + varParts := strings.Split(v, "=") + if len(varParts) != 2 { + return fmt.Errorf(`invalid explicit variable provided (%s), name and value should be divided with "="`, v) + } + + ctx.Global[varParts[0]] = varParts[1] + } + + return nil +} -- cgit 1.4.1