diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index a0ceeeec4..85e69e14c 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -41,6 +41,7 @@ func Funcs() map[string]ast.Function { "split": interpolationFuncSplit(), "sha1": interpolationFuncSha1(), "sha256": interpolationFuncSha256(), + "trimspace": interpolationFuncTrimSpace(), "base64encode": interpolationFuncBase64Encode(), "base64decode": interpolationFuncBase64Decode(), "upper": interpolationFuncUpper(), @@ -617,3 +618,14 @@ func interpolationFuncSha256() ast.Function { }, } } + +func interpolationFuncTrimSpace() ast.Function { + return ast.Function{ + ArgTypes: []ast.Type{ast.TypeString}, + ReturnType: ast.TypeString, + Callback: func(args []interface{}) (interface{}, error) { + trimSpace := args[0].(string) + return strings.TrimSpace(trimSpace), nil + }, + } +} diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 78139aa2d..a2c2a0fdd 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -858,6 +858,18 @@ func TestInterpolateFuncSha256(t *testing.T) { }) } +func TestInterpolateFuncTrimSpace(t *testing.T) { + testFunction(t, testFunctionConfig{ + Cases: []testFunctionCase{ + { + `${trimspace(" test ")}`, + "test", + false, + }, + }, + }) +} + type testFunctionConfig struct { Cases []testFunctionCase Vars map[string]ast.Variable diff --git a/website/source/docs/configuration/interpolation.html.md b/website/source/docs/configuration/interpolation.html.md index 77c40e59e..b1f2b9fe6 100644 --- a/website/source/docs/configuration/interpolation.html.md +++ b/website/source/docs/configuration/interpolation.html.md @@ -176,6 +176,8 @@ The supported built-in functions are: `a_resource_param = ["${split(",", var.CSV_STRING)}"]`. Example: `split(",", module.amod.server_ids)` + * `trimspace(string)` - Returns a copy of the string with all leading and trailing white spaces removed. + * `upper(string)` - Returns a copy of the string with all Unicode letters mapped to their upper case. ## Templates