diff --git a/CHANGELOG b/CHANGELOG index ddf70f6a9..76901d056 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,9 @@ -### v0.16.0-megalithic.1 +### [v0.16.0-megalithic.2] - 2020-06-24 + +* Document arithmetic funcs from [#824](https://github.com/kelseyhightower/confd/pull/824) +* Add loop and loop1to to template_funcs from [#363](https://github.com/kelseyhightower/confd/pull/363) + +### [v0.16.0-megalithic.1] - 2020-06-23 Build with Go 1.13 to add support for Go Template variable replacement. diff --git a/docs/templates.md b/docs/templates.md index c74f09ddc..cd8c7d635 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -364,6 +364,64 @@ Alias for the [strconv.Atoi](https://golang.org/pkg/strconv/#Atoi) function. {{seq 1 (atoi (getv "/count"))}} ``` +### add + +Add two integers. + +``` +{{$five := add 1 4}} +``` + +### sub + +Subtract two integers. + +``` +{{$four := sub 10 6}} +``` + +### mul + +Multiplicate two integers. + +``` +{{$six := mul 3 2}} +``` + +### div + +Divide two integers. + +``` +{{$two := sub 6 3}} +``` + +### mod + +Calculate modulus of two integers. + +``` +{{$three := mod 8 5}} +``` + +### loop + +Takes two integers and generates the number of integers given by the first integer starting with the second integer. + +``` +# Generates [5,6,7] +{{loop 3 5}} +``` + +### loop1to + +Generates integers from 1 to the given integer. + +``` +# Generates [1,2,3,4,5] +{{loop1to 5}} +``` + ## Example Usage ```Bash diff --git a/resource/template/template_funcs.go b/resource/template/template_funcs.go index 4dad18e65..73bd38b26 100644 --- a/resource/template/template_funcs.go +++ b/resource/template/template_funcs.go @@ -17,6 +17,22 @@ import ( "github.com/kelseyhightower/memkv" ) +// loop take the number of elements to generate as well as the starting element. So loop(3, 5) will generate [5,6,7] +// It's helpful as it allow you to write templates like: +// {{range $index, $val := loop1to 2}} +// cpu-map {{ $val }} {{ $index }} +// {{end}} +// => +// cpu-map 1 0 +// cpu-map 2 1 +func loop(n, s int) (arr []int) { + arr = make([]int, n) + for i := 0; i < n; i++ { + arr[i] = i + s + } + return +} + func newFuncMap() map[string]interface{} { m := make(map[string]interface{}) m["base"] = path.Base @@ -49,6 +65,8 @@ func newFuncMap() map[string]interface{} { m["div"] = func(a, b int) int { return a / b } m["mod"] = func(a, b int) int { return a % b } m["mul"] = func(a, b int) int { return a * b } + m["loop"] = loop + m["loop1to"] = func(n int) []int { return loop(n, 1) } m["seq"] = Seq m["atoi"] = strconv.Atoi return m diff --git a/version.go b/version.go index 9398fe67f..20837895b 100644 --- a/version.go +++ b/version.go @@ -1,6 +1,6 @@ package main -const Version = "0.16.0-megalithic.1" +const Version = "0.16.0-megalithic.2" // We want to replace this variable at build time with "-ldflags -X main.GitSHA=xxx", where const is not supported. var GitSHA = ""