diff --git a/template/interpolate/funcs.go b/template/interpolate/funcs.go index f3c17d8b7..602702caf 100644 --- a/template/interpolate/funcs.go +++ b/template/interpolate/funcs.go @@ -9,6 +9,7 @@ import ( // Funcs are the interpolation funcs that are available within interpolations. var FuncGens = map[string]FuncGenerator{ "env": funcGenEnv, + "pwd": funcGenPwd, "user": funcGenUser, } @@ -39,6 +40,12 @@ func funcGenEnv(ctx *Context) interface{} { } } +func funcGenPwd(ctx *Context) interface{} { + return func() (string, error) { + return os.Getwd() + } +} + func funcGenUser(ctx *Context) interface{} { return func() string { return "" diff --git a/template/interpolate/funcs_test.go b/template/interpolate/funcs_test.go index 2bc70b0bf..7bd5c4647 100644 --- a/template/interpolate/funcs_test.go +++ b/template/interpolate/funcs_test.go @@ -64,3 +64,33 @@ func TestFuncEnv_disable(t *testing.T) { } } } + +func TestFuncPwd(t *testing.T) { + wd, err := os.Getwd() + if err != nil { + t.Fatalf("err: %s", err) + } + + cases := []struct { + Input string + Output string + }{ + { + `{{pwd}}`, + wd, + }, + } + + ctx := &Context{} + for _, tc := range cases { + i := &I{Value: tc.Input} + result, err := i.Render(ctx) + if err != nil { + t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err) + } + + if result != tc.Output { + t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result) + } + } +}