Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Add pathexpand interpolation function #11277

Merged
merged 1 commit into from
Jan 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config/interpolate_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func Funcs() map[string]ast.Function {
"md5": interpolationFuncMd5(),
"merge": interpolationFuncMerge(),
"min": interpolationFuncMin(),
"pathexpand": interpolationFuncPathExpand(),
"uuid": interpolationFuncUUID(),
"replace": interpolationFuncReplace(),
"sha1": interpolationFuncSha1(),
Expand Down Expand Up @@ -431,6 +432,17 @@ func interpolationFuncMin() ast.Function {
}
}

// interpolationFuncPathExpand will expand any `~`'s found with the full file path
func interpolationFuncPathExpand() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
return homedir.Expand(args[0].(string))
},
}
}

// interpolationFuncCeil returns the the least integer value greater than or equal to the argument
func interpolationFuncCeil() ast.Function {
return ast.Function{
Expand Down
39 changes: 39 additions & 0 deletions config/interpolate_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
"testing"
"time"

"path/filepath"

"github.com/hashicorp/hil"
"github.com/hashicorp/hil/ast"
"github.com/mitchellh/go-homedir"
)

func TestInterpolateFuncZipMap(t *testing.T) {
Expand Down Expand Up @@ -1972,3 +1975,39 @@ func testFunction(t *testing.T, config testFunctionConfig) {
}
}
}

func TestInterpolateFuncPathExpand(t *testing.T) {
homePath, err := homedir.Dir()
if err != nil {
t.Fatalf("Error getting home directory: %v", err)
}
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
{
`${pathexpand("~/test-file")}`,
filepath.Join(homePath, "test-file"),
false,
},
{
`${pathexpand("~/another/test/file")}`,
filepath.Join(homePath, "another/test/file"),
false,
},
{
`${pathexpand("/root/file")}`,
"/root/file",
false,
},
{
`${pathexpand("/")}`,
"/",
false,
},
{
`${pathexpand()}`,
nil,
true,
},
},
})
}
2 changes: 2 additions & 0 deletions website/source/docs/configuration/interpolation.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ The supported built-in functions are:
* `md5(string)` - Returns a (conventional) hexadecimal representation of the
MD5 hash of the given string.

* `pathexpand(string)` - Returns a filepath string with `~` expanded to the home directory.

* `replace(string, search, replace)` - Does a search and replace on the
given string. All instances of `search` are replaced with the value
of `replace`. If `search` is wrapped in forward slashes, it is treated
Expand Down