core: Add pathexpand interpolation function
Adds the `pathexpand` interpolation function to allow users to expand `~` to the home directory in filepath strings.
This commit is contained in:
parent
dac527be47
commit
0b73b92830
|
@ -79,6 +79,7 @@ func Funcs() map[string]ast.Function {
|
||||||
"md5": interpolationFuncMd5(),
|
"md5": interpolationFuncMd5(),
|
||||||
"merge": interpolationFuncMerge(),
|
"merge": interpolationFuncMerge(),
|
||||||
"min": interpolationFuncMin(),
|
"min": interpolationFuncMin(),
|
||||||
|
"pathexpand": interpolationFuncPathExpand(),
|
||||||
"uuid": interpolationFuncUUID(),
|
"uuid": interpolationFuncUUID(),
|
||||||
"replace": interpolationFuncReplace(),
|
"replace": interpolationFuncReplace(),
|
||||||
"sha1": interpolationFuncSha1(),
|
"sha1": interpolationFuncSha1(),
|
||||||
|
@ -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
|
// interpolationFuncCeil returns the the least integer value greater than or equal to the argument
|
||||||
func interpolationFuncCeil() ast.Function {
|
func interpolationFuncCeil() ast.Function {
|
||||||
return ast.Function{
|
return ast.Function{
|
||||||
|
|
|
@ -8,8 +8,11 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/hashicorp/hil"
|
"github.com/hashicorp/hil"
|
||||||
"github.com/hashicorp/hil/ast"
|
"github.com/hashicorp/hil/ast"
|
||||||
|
"github.com/mitchellh/go-homedir"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestInterpolateFuncZipMap(t *testing.T) {
|
func TestInterpolateFuncZipMap(t *testing.T) {
|
||||||
|
@ -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,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -273,6 +273,8 @@ The supported built-in functions are:
|
||||||
* `md5(string)` - Returns a (conventional) hexadecimal representation of the
|
* `md5(string)` - Returns a (conventional) hexadecimal representation of the
|
||||||
MD5 hash of the given string.
|
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
|
* `replace(string, search, replace)` - Does a search and replace on the
|
||||||
given string. All instances of `search` are replaced with the value
|
given string. All instances of `search` are replaced with the value
|
||||||
of `replace`. If `search` is wrapped in forward slashes, it is treated
|
of `replace`. If `search` is wrapped in forward slashes, it is treated
|
||||||
|
|
Loading…
Reference in New Issue