Skip to content

Commit 42e3491

Browse files
committed
Fix -endpoint being ignored
Fixes #1
1 parent 7aaca57 commit 42e3491

File tree

4 files changed

+70
-11
lines changed

4 files changed

+70
-11
lines changed

docker-gen.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,18 @@ func generateFromEvents(client *docker.Client, configs ConfigFile) {
184184
}
185185
}
186186

187-
func main() {
187+
func initFlags() {
188188
flag.BoolVar(&watch, "watch", false, "watch for container changes")
189189
flag.BoolVar(&onlyExposed, "only-exposed", false, "only include containers with exposed ports")
190190
flag.StringVar(&notifyCmd, "notify", "", "run command after template is regenerated")
191191
flag.StringVar(&configFile, "config", "", "config file with template directives")
192192
flag.IntVar(&interval, "interval", 0, "notify command interval (s)")
193193
flag.StringVar(&endpoint, "endpoint", "", "docker api endpoint")
194194
flag.Parse()
195+
}
196+
197+
func main() {
198+
initFlags()
195199

196200
if flag.NArg() < 1 && configFile == "" {
197201
usage()
@@ -217,12 +221,7 @@ func main() {
217221
Config: []Config{config}}
218222
}
219223

220-
if endpoint == "" && os.Getenv("DOCKER_HOST") != "" {
221-
endpoint = os.Getenv("DOCKER_HOST")
222-
} else {
223-
endpoint = "unix:///var/run/docker.sock"
224-
}
225-
224+
endpoint := getEndpoint()
226225
client, err := docker.NewClient(endpoint)
227226

228227
if err != nil {

docker_client.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,7 @@ func splitDockerImage(img string) (string, string, string) {
103103
}
104104

105105
func newConn() (*httputil.ClientConn, error) {
106-
dockerHost := endpoint
107-
if dockerHost == "" && os.Getenv("DOCKER_HOST") != "" {
108-
dockerHost = os.Getenv("DOCKER_HOST")
109-
}
106+
endpoint := getEndpoint()
110107

111108
proto, addr, err := parseHost(endpoint)
112109
if err != nil {

utils.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import "os"
4+
5+
func getEndpoint() string {
6+
defaultEndpoint := "unix:///var/run/docker.sock"
7+
if os.Getenv("DOCKER_HOST") != "" {
8+
defaultEndpoint = os.Getenv("DOCKER_HOST")
9+
}
10+
11+
if endpoint != "" {
12+
defaultEndpoint = endpoint
13+
}
14+
15+
return defaultEndpoint
16+
17+
}

utils_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"os"
6+
"testing"
7+
)
8+
9+
func TestDefaultEndpoint(t *testing.T) {
10+
endpoint := getEndpoint()
11+
if endpoint != "unix:///var/run/docker.sock" {
12+
t.Fatal("Expected unix:///var/run/docker.sock")
13+
}
14+
}
15+
16+
func TestDockerHostEndpoint(t *testing.T) {
17+
err := os.Setenv("DOCKER_HOST", "tcp://127.0.0.1:4243")
18+
if err != nil {
19+
t.Fatalf("Unable to set DOCKER_HOST: %s", err)
20+
}
21+
22+
endpoint := getEndpoint()
23+
if endpoint != "tcp://127.0.0.1:4243" {
24+
t.Fatal("Expected tcp://127.0.0.1:4243")
25+
}
26+
}
27+
28+
func TestDockerFlagEndpoint(t *testing.T) {
29+
30+
initFlags()
31+
err := os.Setenv("DOCKER_HOST", "tcp://127.0.0.1:4243")
32+
if err != nil {
33+
t.Fatalf("Unable to set DOCKER_HOST: %s", err)
34+
}
35+
36+
// flag value should override DOCKER_HOST and default value
37+
err = flag.Set("endpoint", "tcp://127.0.0.1:5555")
38+
if err != nil {
39+
t.Fatalf("Unable to set endpoint flag: %s", err)
40+
}
41+
42+
endpoint := getEndpoint()
43+
if endpoint != "tcp://127.0.0.1:5555" {
44+
t.Fatal("Expected tcp://127.0.0.1:5555")
45+
}
46+
}

0 commit comments

Comments
 (0)