From e7d02b9dd264980a48f1afe796fcf9ef43d9bba1 Mon Sep 17 00:00:00 2001 From: fujiwara Date: Thu, 23 Jan 2025 15:36:44 +0900 Subject: [PATCH] Add FuncMapWithName() --- ssm/funcs.go | 6 +++++- ssm/funcs_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 ssm/funcs_test.go diff --git a/ssm/funcs.go b/ssm/funcs.go index 0e2d93e..11dee82 100644 --- a/ssm/funcs.go +++ b/ssm/funcs.go @@ -16,8 +16,12 @@ func FuncMap(ctx context.Context, cfg aws.Config) (template.FuncMap, error) { } func (app *App) FuncMap(ctx context.Context) template.FuncMap { + return app.FuncMapWithName(ctx, "ssm") +} + +func (app *App) FuncMapWithName(ctx context.Context, name string) template.FuncMap { return template.FuncMap{ - "ssm": func(paramName string, index ...int) (string, error) { + name: func(paramName string, index ...int) (string, error) { value, err := app.Lookup(ctx, paramName, index...) if err != nil { return "", fmt.Errorf("failed to lookup ssm parameter: %w", err) diff --git a/ssm/funcs_test.go b/ssm/funcs_test.go new file mode 100644 index 0000000..4b5fbbd --- /dev/null +++ b/ssm/funcs_test.go @@ -0,0 +1,26 @@ +package ssm_test + +import ( + "context" + "strings" + "testing" + "text/template" +) + +func TestFuncMap(t *testing.T) { + ctx := context.Background() + app := newMockApp(mockGetParameter) + tmpl := template.New("test") + tmpl.Funcs(app.FuncMap(ctx)) + tmpl.Funcs(app.FuncMapWithName(ctx, "my_ssm")) + tmpl = template.Must(tmpl.Parse(`{{ssm "/string"}}:{{my_ssm "/string"}}`)) + + buf := &strings.Builder{} + err := tmpl.Execute(buf, nil) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if buf.String() != "string value:string value" { + t.Errorf("unexpected result: %s", buf.String()) + } +}