Skip to content

Commit

Permalink
Merge pull request #2 from fujiwara/funcmap-withname
Browse files Browse the repository at this point in the history
Add FuncMapWithName()
  • Loading branch information
fujiwara authored Jan 23, 2025
2 parents 866faba + e7d02b9 commit d91ee41
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ssm/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions ssm/funcs_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}

0 comments on commit d91ee41

Please sign in to comment.