2

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 ...

stefanobaghino
  • 11,253
  • 4
  • 35
  • 63
Riduidel
  • 22,052
  • 14
  • 85
  • 185
  • 1
    Not sure if this is what you're looking for https://play.golang.org/p/EHB-8BZ83o – mkopriva Nov 14 '17 at 15:09
  • What do you exactly mean by 'split go template function'? Templates are one mechanism, `strings.Spit` is a diferent one.. – Wojciech Kaczmarek Nov 14 '17 at 16:37
  • @WojciechKaczmarek There is a [`split`](https://github.com/kelseyhightower/confd/blob/master/docs/templates.md#split) function available in confd go templates, which is what I use to split my array – Riduidel Nov 15 '17 at 10:21
  • @mkopriva no because you use pure go where I'm looking for a solution using go template. In fact, the better I can think about is creating a duplicated array and iterating it from the position of the element to remove – Riduidel Nov 15 '17 at 15:15
  • @Riduidel I think that if you use the term 'go template' people are assuming templates from standard Go library (`text/template` or `html/template`). Let's go back to the basics. Do you mean https://github.com/kelseyhightower/confd ? – Wojciech Kaczmarek Nov 15 '17 at 15:18
  • Btw the SO tag you used also suggests the standard library templates I mentioned. – Wojciech Kaczmarek Nov 15 '17 at 15:20
  • @WojciechKaczmarek yes, but, as far as I understand, confd uses an extended version of standard go templates. Am I right ? – Riduidel Nov 15 '17 at 15:26
  • 1
    @Riduidel Maybe, I don't know. For sure, the `split` function is not in standard lib of template functions https://golang.org/pkg/text/template/#hdr-Functions . I'd suggest you add more explicit reference to that function so that the casual reader knows quickly it's not only about standard templates. Then you'd get the faster & more accurate answer from the actual user of the `confd` package. Can you cite a documentation of that `split` also? – Wojciech Kaczmarek Nov 15 '17 at 15:34
  • 1
    Thanks. Now we know from the docs it's the same template language and also that `split` is a wrapper for `strings.Split`. Then you can adapt the proposal @mkopriva gave you. You just need to capture a result of `split` to a var and then use `append` from @mkopriva's example on it. Looks like it'll be shorter and more elegant than reiterating. – Wojciech Kaczmarek Nov 15 '17 at 15:49

1 Answers1

1

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}}

https://play.golang.org/p/lGdExfHAvy

mkopriva
  • 35,176
  • 4
  • 57
  • 71