I'm using confd to populate a file through a template.
In this file, I want to have a list of elements shifted and inserted. This slice contains strings like
0=container-1
1=container-2
2=container-3
3=container-4
(in fact, it is a string that I split using split
confd function).
I want, on each container, be able to filter container name out and shift the list to have the ones after my container appear first.
As an example, on container-2
I would like to have as result
2=container-3
3=container-4
0=container-1
How to do that in a confd go template ? I think I know how to do that in go (but I'm not that good in that particular language), but I don't find how to do that using only a template ...
If you are unable to manipulate the slice/string outside the template, and if you are unable to add custom functions to the template either you will have do this inside the template. It's more verbose, but doable.
One approach is to have two loops nested inside a parent loop. The parent will look for the container you want to leave out, at which point it will spawn the two child loops with $i
holding the index of the one to be left out. The first child loop can then list containers whose index is greater than $i
and the second child loop will list containers whose index is less than $i
.
{{range $i, $c := $cons}}
{{/* find item to be skipped */}}
{{if (eq $c $.Skip)}}
{{range $j, $c := $cons}}
{{/* list items that come after the one to be skipped */}}
{{if (gt $j $i)}}
{{$c}}
{{end}}
{{end}}
{{range $j, $c := $cons}}
{{/* list items that come before the one to be skipped */}}
{{if (lt $j $i)}}
{{$c}}
{{end}}
{{end}}
{{end}}
{{end}}