From eb3b2776db494ad37d9f7c96f23dbb1e857f78d8 Mon Sep 17 00:00:00 2001 From: Alexander Yastrebov Date: Mon, 3 Jun 2024 14:27:53 +0200 Subject: [PATCH] provisioner: add list template function (#776) This is useful for defining a fixed list instead of splitting a comma-separated string. See also previous #711 Signed-off-by: Alexander Yastrebov --- provisioner/template.go | 5 +++++ provisioner/template_test.go | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/provisioner/template.go b/provisioner/template.go index 1b7cacfa..f5512fe7 100644 --- a/provisioner/template.go +++ b/provisioner/template.go @@ -127,6 +127,7 @@ func renderTemplate(context *templateContext, file string) (string, error) { "awsValidID": awsValidID, "indent": sprig.GenericFuncMap()["indent"], "dict": dict, + "list": list, "scaleQuantity": scaleQuantity, "instanceTypeCPUQuantity": func(instanceType string) (string, error) { return instanceTypeCPUQuantity(context, instanceType) @@ -275,6 +276,10 @@ func dict(args ...interface{}) (map[string]interface{}, error) { return dict, nil } +func list(args ...interface{}) []interface{} { + return args +} + // accountID returns just the ID part of an account func accountID(account string) (string, error) { items := strings.Split(account, ":") diff --git a/provisioner/template_test.go b/provisioner/template_test.go index 55ec40cd..46b0d637 100644 --- a/provisioner/template_test.go +++ b/provisioner/template_test.go @@ -1253,6 +1253,29 @@ func TestDictInvalidArgs(t *testing.T) { } } +func TestList(t *testing.T) { + result, err := renderSingle( + t, + ` +{{- $alist := list + "foo" + "bar" + 1 +}} +{{- range $i, $v := $alist }} +{{ $i }}={{ $v }} +{{- end }} +`, + nil) + + require.NoError(t, err) + require.EqualValues(t, ` +0=foo +1=bar +2=1 +`, result) +} + func TestScaleQuantity(t *testing.T) { for _, tc := range []struct { name string