Skip to content

Commit 13188ef

Browse files
committed
Make -endpoint work consistently w/ -watch
See #18
1 parent c128476 commit 13188ef

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed

docker-gen.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,12 @@ func main() {
252252
Config: []Config{config}}
253253
}
254254

255-
endpoint := getEndpoint()
256-
client, err := docker.NewClient(endpoint)
255+
endpoint, err := getEndpoint()
256+
if err != nil {
257+
log.Fatalf("Bad endpoint: %s", err)
258+
}
257259

260+
client, err := docker.NewClient(endpoint)
258261
if err != nil {
259262
log.Fatalf("Unable to parse %s: %s", endpoint, err)
260263
}

docker_client.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ func splitDockerImage(img string) (string, string, string) {
104104
}
105105

106106
func newConn() (*httputil.ClientConn, error) {
107-
endpoint := getEndpoint()
107+
endpoint, err := getEndpoint()
108+
if err != nil {
109+
return nil, err
110+
}
108111

109112
proto, addr, err := parseHost(endpoint)
110113
if err != nil {

utils.go

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

3-
import "os"
3+
import (
4+
"errors"
5+
"os"
6+
)
47

5-
func getEndpoint() string {
8+
func getEndpoint() (string, error) {
69
defaultEndpoint := "unix:///var/run/docker.sock"
710
if os.Getenv("DOCKER_HOST") != "" {
811
defaultEndpoint = os.Getenv("DOCKER_HOST")
@@ -12,6 +15,21 @@ func getEndpoint() string {
1215
defaultEndpoint = endpoint
1316
}
1417

15-
return defaultEndpoint
18+
proto, host, err := parseHost(defaultEndpoint)
19+
if err != nil {
20+
return "", err
21+
}
22+
23+
if proto == "unix" {
24+
exist, err := exists(host)
25+
if err != nil {
26+
return "", err
27+
}
28+
29+
if !exist {
30+
return "", errors.New(host + " does not exists.")
31+
}
32+
}
1633

34+
return defaultEndpoint, nil
1735
}

utils_test.go

+35-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import (
77
)
88

99
func TestDefaultEndpoint(t *testing.T) {
10-
endpoint := getEndpoint()
10+
endpoint, err := getEndpoint()
11+
if err != nil {
12+
t.Fatal("%s", err)
13+
}
1114
if endpoint != "unix:///var/run/docker.sock" {
1215
t.Fatal("Expected unix:///var/run/docker.sock")
1316
}
@@ -19,7 +22,11 @@ func TestDockerHostEndpoint(t *testing.T) {
1922
t.Fatalf("Unable to set DOCKER_HOST: %s", err)
2023
}
2124

22-
endpoint := getEndpoint()
25+
endpoint, err := getEndpoint()
26+
if err != nil {
27+
t.Fatal("%s", err)
28+
}
29+
2330
if endpoint != "tcp://127.0.0.1:4243" {
2431
t.Fatal("Expected tcp://127.0.0.1:4243")
2532
}
@@ -39,8 +46,33 @@ func TestDockerFlagEndpoint(t *testing.T) {
3946
t.Fatalf("Unable to set endpoint flag: %s", err)
4047
}
4148

42-
endpoint := getEndpoint()
49+
endpoint, err := getEndpoint()
50+
if err != nil {
51+
t.Fatal("%s", err)
52+
}
4353
if endpoint != "tcp://127.0.0.1:5555" {
4454
t.Fatal("Expected tcp://127.0.0.1:5555")
4555
}
4656
}
57+
58+
func TestUnixNotExists(t *testing.T) {
59+
60+
endpoint = ""
61+
err := os.Setenv("DOCKER_HOST", "unix:///does/not/exist")
62+
if err != nil {
63+
t.Fatalf("Unable to set DOCKER_HOST: %s", err)
64+
}
65+
66+
_, err = getEndpoint()
67+
if err == nil {
68+
t.Fatal("endpoint should have failed")
69+
}
70+
}
71+
72+
func TestUnixBadFormat(t *testing.T) {
73+
endpoint = "unix:/var/run/docker.sock"
74+
_, err := getEndpoint()
75+
if err == nil {
76+
t.Fatal("endpoint should have failed")
77+
}
78+
}

0 commit comments

Comments
 (0)