diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index f1f97b04e..3ae127221 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -89,6 +89,7 @@ func Funcs() map[string]ast.Function { "merge": interpolationFuncMerge(), "min": interpolationFuncMin(), "pathexpand": interpolationFuncPathExpand(), + "pow": interpolationFuncPow(), "uuid": interpolationFuncUUID(), "replace": interpolationFuncReplace(), "sha1": interpolationFuncSha1(), @@ -394,6 +395,17 @@ func interpolationFuncConcat() ast.Function { } } +// interpolationFuncPow returns base x exponential of y. +func interpolationFuncPow() ast.Function { + return ast.Function{ + ArgTypes: []ast.Type{ast.TypeFloat, ast.TypeFloat}, + ReturnType: ast.TypeFloat, + Callback: func(args []interface{}) (interface{}, error) { + return math.Pow(args[0].(float64), args[1].(float64)), nil + }, + } +} + // interpolationFuncFile implements the "file" function that allows // loading contents from a file. func interpolationFuncFile() ast.Function { diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 6ed65a6f4..865936ab7 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -310,6 +310,54 @@ func TestInterpolateFuncMin(t *testing.T) { }) } +func TestInterpolateFuncPow(t *testing.T) { + testFunction(t, testFunctionConfig{ + Cases: []testFunctionCase{ + { + `${pow(1, 0)}`, + "1", + false, + }, + { + `${pow(1, 1)}`, + "1", + false, + }, + + { + `${pow(2, 0)}`, + "1", + false, + }, + { + `${pow(2, 1)}`, + "2", + false, + }, + { + `${pow(3, 2)}`, + "9", + false, + }, + { + `${pow(-3, 2)}`, + "9", + false, + }, + { + `${pow(2, -2)}`, + "0.25", + false, + }, + { + `${pow(0, 2)}`, + "0", + false, + }, + }, + }) +} + func TestInterpolateFuncFloor(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 296b35be2..bd5c18b31 100644 --- a/website/source/docs/configuration/interpolation.html.md +++ b/website/source/docs/configuration/interpolation.html.md @@ -312,6 +312,12 @@ The supported built-in functions are: * `pathexpand(string)` - Returns a filepath string with `~` expanded to the home directory. Note: This will create a plan diff between two different hosts, unless the filepaths are the same. + * `pow(x, y)` - Returns the base `x` of exponential `y`. + + Example: + * `${pow(3,2)}` = 9 + * `${pow(4,0)}` = 1 + * `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