diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index a0ceeeec4..1757da79a 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(), + "strip": interpolationFuncStrip(), "base64encode": interpolationFuncBase64Encode(), "base64decode": interpolationFuncBase64Decode(), "upper": interpolationFuncUpper(), @@ -617,3 +618,14 @@ func interpolationFuncSha256() ast.Function { }, } } + +func interpolationFuncTrim() ast.Function { + return ast.Function{ + ArgTypes: []ast.Type{ast.TypeString}, + ReturnType: ast.TypeString, + Callback: func(args []interface{}) (interface{}, error) { + trim := args[0].(string) + return strings.TrimSpace(trim), nil + }, + } +} diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 78139aa2d..b80d7456b 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -858,6 +858,18 @@ func TestInterpolateFuncSha256(t *testing.T) { }) } +func TestInterpolateFuncTrim(t *testing.T) { + testFunction(t, testFunctionConfig{ + Cases: []testFunctionCase{ + { + `${trim(" 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..e43f524b3 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)` + * `trim(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