From 5d205ec1fcabd860befe5ed11c04a6750d0323be Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 15 May 2015 21:10:12 -0700 Subject: [PATCH] template/interpolate: wd --- template/interpolate/funcs.go | 7 +++++++ template/interpolate/funcs_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) 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) + } + } +}