Skip to content

Commit 242875d

Browse files
committed
Merge branch 'md5-fix-env-split'
2 parents 9978084 + c14bed8 commit 242875d

File tree

5 files changed

+41
-14
lines changed

5 files changed

+41
-14
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.SILENT :
22
.PHONY : docker-gen clean fmt
33

4-
TAG:=`git describe --abbrev=0 --tags`
4+
TAG:=`git describe --tags`
55
LDFLAGS:=-X main.buildVersion $(TAG)
66

77
all: docker-gen
@@ -25,7 +25,7 @@ dist: dist-clean
2525

2626

2727
release: dist
28-
glock sync github.com/jwilder/docker-gen
28+
glock sync -n < GLOCKFILE
2929
tar -cvzf docker-gen-linux-amd64-$(TAG).tar.gz -C dist/linux/amd64 docker-gen
3030
tar -cvzf docker-gen-linux-i386-$(TAG).tar.gz -C dist/linux/i386 docker-gen
3131
tar -cvzf docker-gen-linux-armel-$(TAG).tar.gz -C dist/linux/armel docker-gen

docker-gen.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,7 @@ type ConfigFile struct {
9898
type Context []*RuntimeContainer
9999

100100
func (c *Context) Env() map[string]string {
101-
102-
env := make(map[string]string)
103-
for _, i := range os.Environ() {
104-
parts := strings.Split(i, "=")
105-
env[parts[0]] = parts[1]
106-
}
107-
return env
101+
return splitKeyValueSlice(os.Environ())
108102
}
109103

110104
func (c *ConfigFile) filterWatches() ConfigFile {

docker_client.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,7 @@ func getContainers(client *docker.Client) ([]*RuntimeContainer, error) {
147147
}
148148
}
149149

150-
for _, entry := range container.Config.Env {
151-
parts := strings.Split(entry, "=")
152-
runtimeContainer.Env[parts[0]] = parts[1]
153-
}
154-
150+
runtimeContainer.Env = splitKeyValueSlice(container.Config.Env)
155151
containers = append(containers, runtimeContainer)
156152
}
157153
return containers, nil

utils.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"os"
5+
"strings"
56
)
67

78
func getEndpoint() (string, error) {
@@ -21,3 +22,19 @@ func getEndpoint() (string, error) {
2122

2223
return defaultEndpoint, nil
2324
}
25+
26+
// splitKeyValueSlice takes a string slice where values are of the form
27+
// KEY, KEY=, KEY=VALUE or KEY=NESTED_KEY=VALUE2, and returns a map[string]string where items
28+
// are split at their first `=`.
29+
func splitKeyValueSlice(in []string) map[string]string {
30+
env := make(map[string]string)
31+
for _, entry := range in {
32+
parts := strings.SplitN(entry, "=", 2)
33+
if len(parts) != 2 {
34+
parts = append(parts, "")
35+
}
36+
env[parts[0]] = parts[1]
37+
}
38+
return env
39+
40+
}

utils_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,23 @@ func TestUnixBadFormat(t *testing.T) {
6262
t.Fatal("endpoint should have failed")
6363
}
6464
}
65+
66+
func TestSplitKeyValueSlice(t *testing.T) {
67+
tests := []struct {
68+
input []string
69+
expected string
70+
}{
71+
{[]string{"K"}, ""},
72+
{[]string{"K="}, ""},
73+
{[]string{"K=V3"}, "V3"},
74+
{[]string{"K=V4=V5"}, "V4=V5"},
75+
}
76+
77+
for _, i := range tests {
78+
v := splitKeyValueSlice(i.input)
79+
if v["K"] != i.expected {
80+
t.Fatalf("expected K='%s'. got '%s'", i.expected, v["K"])
81+
}
82+
83+
}
84+
}

0 commit comments

Comments
 (0)