feat: allow arguments to be passed to templates

This commit is contained in:
Chi-En Wu
2020-04-22 10:26:17 +08:00
parent bddaab6942
commit 9aecdacf39
15 changed files with 137 additions and 143 deletions

View File

@ -3,32 +3,22 @@
{{- /*
Merge one or more YAML templates and output the result.
This takes an list of values:
- the first item is the top context
- the rest items are template names of the templates, the former one will override the latter.
- the top context
- [optional] zero or more template args
- [optional] the template name of the overrides (destination)
- the template name of the base (source)
*/ -}}
{{- define "common.utils.merge" -}}
{{- $top := first . -}}
{{- $dest := dict -}}
{{- range (rest .) -}}
{{- $src := fromYaml (include . $top) | default (dict) -}}
{{- $dest = merge $dest $src -}}
{{- $tplName := last . -}}
{{- $args := initial . -}}
{{- if typeIs "string" (last $args) -}}
{{- $overridesName := last $args -}}
{{- $args = initial $args -}}
{{- $tpl := fromYaml (include $tplName $args) | default (dict) -}}
{{- $overrides := fromYaml (include $overridesName $args) | default (dict) -}}
{{- toYaml (merge $overrides $tpl) -}}
{{- else -}}
{{- include $tplName $args -}}
{{- end -}}
{{- toYaml $dest -}}
{{- end -}}
{{- /*
Flatten list of arguments before rendering the given template.
This takes an list of values:
- the first item is the template name to be rendered
- the second item is either an list of arguments or a single argument
- the rest items are the appended arguments
*/ -}}
{{- define "common.utils.flattenCall" -}}
{{- $tpl := first . -}}
{{- $args := index . 1 -}}
{{- if not (typeIs "[]interface {}" $args) -}}
{{- $args = list $args -}}
{{- end -}}
{{- $args = concat $args (slice . 2) -}}
{{- include $tpl $args -}}
{{- end -}}