feat: add template common.container

This commit is contained in:
Chi-En Wu
2020-04-15 16:02:35 +08:00
parent 66727cc7cb
commit ca615dee81
2 changed files with 49 additions and 0 deletions

34
templates/_utils.tpl Normal file
View File

@ -0,0 +1,34 @@
{{/* vim: set filetype=mustache: */}}
{{- /*
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.
*/ -}}
{{- define "common.utils.merge" -}}
{{- $top := first . -}}
{{- $dest := dict -}}
{{- range (rest .) -}}
{{- $src := fromYaml (include . $top) | default (dict) -}}
{{- $dest = merge $dest $src -}}
{{- 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 -}}