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
This commit is contained in:
Phillip Johnsen 2018-05-30 21:43:31 +02:00 committed by Vincent Ambo
parent 09869cf8fc
commit 5cf9d53e80
3 changed files with 49 additions and 0 deletions

View file

@ -37,6 +37,7 @@ var (
// Global flags
includes = app.Flag("include", "Resource sets to include explicitly").Short('i').Strings()
excludes = app.Flag("exclude", "Resource sets to exclude explicitly").Short('e').Strings()
variables = app.Flag("var", "Provide variables to templates explicitly").Strings()
// Commands
template = app.Command("template", "Template resource sets and print them")
@ -188,6 +189,11 @@ func loadContextAndResources(file *string) (*context.Context, *[]templater.Rende
app.Fatalf("Error loading context: %v\n", err)
}
err = ctx.SetVariablesFromArguments(variables)
if err != nil {
app.Fatalf("Error setting explicit variables in context: %v\n", err)
}
resources, err := templater.LoadAndApplyTemplates(includes, excludes, ctx)
if err != nil {
app.Fatalf("Error templating resource sets: %v\n", err)