Skip to content

Commit f53298d

Browse files
committed
Add a groupByMulti template function
Supports the use case of having multiple values stored in an env var that are separated by a comma or other char. For example, w/ nginx-proxy, VIRTUAL_HOST=foo.example.com controls the virtual host that will be setup. If an container supported multiple virtual host, it could not be done w/ VIRTUAL_HOST=foo.example.com,bar.example.com.
1 parent d8781dc commit f53298d

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

template.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ import (
1111
"text/template"
1212
)
1313

14+
func groupByMulti(entries []*RuntimeContainer, key, sep string) map[string][]*RuntimeContainer {
15+
groups := make(map[string][]*RuntimeContainer)
16+
for _, v := range entries {
17+
value := deepGet(*v, key)
18+
if value != nil {
19+
items := strings.Split(value.(string), sep)
20+
for _, item := range items {
21+
groups[item] = append(groups[item], v)
22+
}
23+
24+
}
25+
}
26+
return groups
27+
}
28+
1429
func groupBy(entries []*RuntimeContainer, key string) map[string][]*RuntimeContainer {
1530
groups := make(map[string][]*RuntimeContainer)
1631
for _, v := range entries {
@@ -32,9 +47,10 @@ func contains(item map[string]string, key string) bool {
3247
func generateFile(config Config, containers []*RuntimeContainer) bool {
3348
templatePath := config.Template
3449
tmpl, err := template.New(filepath.Base(templatePath)).Funcs(template.FuncMap{
35-
"contains": contains,
36-
"groupBy": groupBy,
37-
"split": strings.Split,
50+
"contains": contains,
51+
"groupBy": groupBy,
52+
"groupByMulti": groupByMulti,
53+
"split": strings.Split,
3854
}).ParseFiles(templatePath)
3955
if err != nil {
4056
log.Fatalf("unable to parse template: %s", err)

template_test.go

+46-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package main
22

3-
import (
4-
"testing"
5-
)
3+
import "testing"
64

75
func TestContains(t *testing.T) {
86
env := map[string]string{
@@ -56,3 +54,48 @@ func TestGroupByExitingKey(t *testing.T) {
5654
t.Fail()
5755
}
5856
}
57+
58+
func TestGroupByMulti(t *testing.T) {
59+
containers := []*RuntimeContainer{
60+
&RuntimeContainer{
61+
Env: map[string]string{
62+
"VIRTUAL_HOST": "demo1.localhost",
63+
},
64+
ID: "1",
65+
},
66+
&RuntimeContainer{
67+
Env: map[string]string{
68+
"VIRTUAL_HOST": "demo1.localhost,demo3.localhost",
69+
},
70+
ID: "2",
71+
},
72+
&RuntimeContainer{
73+
Env: map[string]string{
74+
"VIRTUAL_HOST": "demo2.localhost",
75+
},
76+
ID: "3",
77+
},
78+
}
79+
80+
groups := groupByMulti(containers, "Env.VIRTUAL_HOST", ",")
81+
if len(groups) != 3 {
82+
t.Fatalf("expected 3 got %d", len(groups))
83+
}
84+
85+
if len(groups["demo1.localhost"]) != 2 {
86+
t.Fatalf("expected 2 got %s", len(groups["demo1.localhost"]))
87+
}
88+
89+
if len(groups["demo2.localhost"]) != 1 {
90+
t.Fatalf("expected 1 got %s", len(groups["demo2.localhost"]))
91+
}
92+
if groups["demo2.localhost"][0].ID != "3" {
93+
t.Fatalf("expected 2 got %s", groups["demo2.localhost"][0].ID)
94+
}
95+
if len(groups["demo3.localhost"]) != 1 {
96+
t.Fatalf("expect 1 got %d", len(groups["demo3.localhost"]))
97+
}
98+
if groups["demo3.localhost"][0].ID != "2" {
99+
t.Fatalf("expected 2 got %s", groups["demo3.localhost"][0].ID)
100+
}
101+
}

0 commit comments

Comments
 (0)