From 059a1b2c0f3340d89053a9e0ac6fa4a97c9178ec Mon Sep 17 00:00:00 2001 From: Joern Barthel Date: Thu, 6 Apr 2017 13:14:09 +0200 Subject: [PATCH] Added chomp interpolation function. --- config/interpolate_funcs.go | 12 +++++++++ config/interpolate_funcs_test.go | 26 +++++++++++++++++++ .../docs/configuration/interpolation.html.md | 2 ++ 3 files changed, 40 insertions(+) diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index cc09e384c..f70fbdec6 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -58,6 +58,7 @@ func Funcs() map[string]ast.Function { "base64encode": interpolationFuncBase64Encode(), "base64sha256": interpolationFuncBase64Sha256(), "ceil": interpolationFuncCeil(), + "chomp": interpolationFuncChomp(), "cidrhost": interpolationFuncCidrHost(), "cidrnetmask": interpolationFuncCidrNetmask(), "cidrsubnet": interpolationFuncCidrSubnet(), @@ -459,6 +460,17 @@ func interpolationFuncCeil() ast.Function { } } +// interpolationFuncChomp removes trailing newlines from the given string +func interpolationFuncChomp() ast.Function { + return ast.Function{ + ArgTypes: []ast.Type{ast.TypeString}, + ReturnType: ast.TypeString, + Callback: func(args []interface{}) (interface{}, error) { + return strings.TrimRight(args[0].(string), "\n"), nil + }, + } +} + // interpolationFuncFloorreturns returns the greatest integer value less than or equal to the argument func interpolationFuncFloor() ast.Function { return ast.Function{ diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 04e85a6d5..6c430add1 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -370,6 +370,32 @@ func TestInterpolateFuncCeil(t *testing.T) { }) } +func TestInterpolateFuncChomp(t *testing.T) { + testFunction(t, testFunctionConfig{ + Cases: []testFunctionCase{ + { + `${chomp()}`, + nil, + true, + }, + + { + `${chomp("hello world")}`, + "hello world", + false, + }, + + { + `${chomp("goodbye\ncruel\nworld\n")}`, + `goodbye +cruel +world`, + false, + }, + }, + }) +} + func TestInterpolateFuncMap(t *testing.T) { testFunction(t, testFunctionConfig{ Cases: []testFunctionCase{ diff --git a/website/source/docs/configuration/interpolation.html.md b/website/source/docs/configuration/interpolation.html.md index 709261585..b7eb15032 100644 --- a/website/source/docs/configuration/interpolation.html.md +++ b/website/source/docs/configuration/interpolation.html.md @@ -156,6 +156,8 @@ The supported built-in functions are: * `ceil(float)` - Returns the least integer value greater than or equal to the argument. + * `chomp(string)` - Removes trailing newlines from the given string. + * `cidrhost(iprange, hostnum)` - Takes an IP address range in CIDR notation and creates an IP address with the given host number. For example, `cidrhost("10.0.0.0/8", 2)` returns `10.0.0.2`.