Browse code

Move client-opts to a separate file

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2019/01/21 22:52:46
Showing 2 changed files
... ...
@@ -47,16 +47,13 @@ import (
47 47
 	"net"
48 48
 	"net/http"
49 49
 	"net/url"
50
-	"os"
51 50
 	"path"
52
-	"path/filepath"
53 51
 	"strings"
54 52
 
55 53
 	"github.com/docker/docker/api"
56 54
 	"github.com/docker/docker/api/types"
57 55
 	"github.com/docker/docker/api/types/versions"
58 56
 	"github.com/docker/go-connections/sockets"
59
-	"github.com/docker/go-connections/tlsconfig"
60 57
 	"github.com/pkg/errors"
61 58
 )
62 59
 
... ...
@@ -111,137 +108,6 @@ func NewEnvClient() (*Client, error) {
111 111
 	return NewClientWithOpts(FromEnv)
112 112
 }
113 113
 
114
-// FromEnv configures the client with values from environment variables.
115
-//
116
-// Supported environment variables:
117
-// DOCKER_HOST to set the url to the docker server.
118
-// DOCKER_API_VERSION to set the version of the API to reach, leave empty for latest.
119
-// DOCKER_CERT_PATH to load the TLS certificates from.
120
-// DOCKER_TLS_VERIFY to enable or disable TLS verification, off by default.
121
-func FromEnv(c *Client) error {
122
-	if dockerCertPath := os.Getenv("DOCKER_CERT_PATH"); dockerCertPath != "" {
123
-		options := tlsconfig.Options{
124
-			CAFile:             filepath.Join(dockerCertPath, "ca.pem"),
125
-			CertFile:           filepath.Join(dockerCertPath, "cert.pem"),
126
-			KeyFile:            filepath.Join(dockerCertPath, "key.pem"),
127
-			InsecureSkipVerify: os.Getenv("DOCKER_TLS_VERIFY") == "",
128
-		}
129
-		tlsc, err := tlsconfig.Client(options)
130
-		if err != nil {
131
-			return err
132
-		}
133
-
134
-		c.client = &http.Client{
135
-			Transport:     &http.Transport{TLSClientConfig: tlsc},
136
-			CheckRedirect: CheckRedirect,
137
-		}
138
-	}
139
-
140
-	if host := os.Getenv("DOCKER_HOST"); host != "" {
141
-		if err := WithHost(host)(c); err != nil {
142
-			return err
143
-		}
144
-	}
145
-
146
-	if version := os.Getenv("DOCKER_API_VERSION"); version != "" {
147
-		c.version = version
148
-		c.manualOverride = true
149
-	}
150
-	return nil
151
-}
152
-
153
-// WithTLSClientConfig applies a tls config to the client transport.
154
-func WithTLSClientConfig(cacertPath, certPath, keyPath string) func(*Client) error {
155
-	return func(c *Client) error {
156
-		opts := tlsconfig.Options{
157
-			CAFile:             cacertPath,
158
-			CertFile:           certPath,
159
-			KeyFile:            keyPath,
160
-			ExclusiveRootPools: true,
161
-		}
162
-		config, err := tlsconfig.Client(opts)
163
-		if err != nil {
164
-			return errors.Wrap(err, "failed to create tls config")
165
-		}
166
-		if transport, ok := c.client.Transport.(*http.Transport); ok {
167
-			transport.TLSClientConfig = config
168
-			return nil
169
-		}
170
-		return errors.Errorf("cannot apply tls config to transport: %T", c.client.Transport)
171
-	}
172
-}
173
-
174
-// WithDialer applies the dialer.DialContext to the client transport. This can be
175
-// used to set the Timeout and KeepAlive settings of the client.
176
-// Deprecated: use WithDialContext
177
-func WithDialer(dialer *net.Dialer) func(*Client) error {
178
-	return WithDialContext(dialer.DialContext)
179
-}
180
-
181
-// WithDialContext applies the dialer to the client transport. This can be
182
-// used to set the Timeout and KeepAlive settings of the client.
183
-func WithDialContext(dialContext func(ctx context.Context, network, addr string) (net.Conn, error)) func(*Client) error {
184
-	return func(c *Client) error {
185
-		if transport, ok := c.client.Transport.(*http.Transport); ok {
186
-			transport.DialContext = dialContext
187
-			return nil
188
-		}
189
-		return errors.Errorf("cannot apply dialer to transport: %T", c.client.Transport)
190
-	}
191
-}
192
-
193
-// WithVersion overrides the client version with the specified one
194
-func WithVersion(version string) func(*Client) error {
195
-	return func(c *Client) error {
196
-		c.version = version
197
-		return nil
198
-	}
199
-}
200
-
201
-// WithHost overrides the client host with the specified one.
202
-func WithHost(host string) func(*Client) error {
203
-	return func(c *Client) error {
204
-		hostURL, err := ParseHostURL(host)
205
-		if err != nil {
206
-			return err
207
-		}
208
-		c.host = host
209
-		c.proto = hostURL.Scheme
210
-		c.addr = hostURL.Host
211
-		c.basePath = hostURL.Path
212
-		if transport, ok := c.client.Transport.(*http.Transport); ok {
213
-			return sockets.ConfigureTransport(transport, c.proto, c.addr)
214
-		}
215
-		return errors.Errorf("cannot apply host to transport: %T", c.client.Transport)
216
-	}
217
-}
218
-
219
-// WithHTTPClient overrides the client http client with the specified one
220
-func WithHTTPClient(client *http.Client) func(*Client) error {
221
-	return func(c *Client) error {
222
-		if client != nil {
223
-			c.client = client
224
-		}
225
-		return nil
226
-	}
227
-}
228
-
229
-// WithHTTPHeaders overrides the client default http headers
230
-func WithHTTPHeaders(headers map[string]string) func(*Client) error {
231
-	return func(c *Client) error {
232
-		c.customHTTPHeaders = headers
233
-		return nil
234
-	}
235
-}
236
-
237
-// WithScheme overrides the client scheme with the specified one
238
-func WithScheme(scheme string) func(*Client) error {
239
-	return func(c *Client) error {
240
-		c.scheme = scheme
241
-		return nil
242
-	}
243
-}
244
-
245 114
 // NewClientWithOpts initializes a new API client with default values. It takes functors
246 115
 // to modify values when creating it, like `NewClientWithOpts(WithVersion(…))`
247 116
 // It also initializes the custom http headers to add to each request.
248 117
new file mode 100644
... ...
@@ -0,0 +1,144 @@
0
+package client
1
+
2
+import (
3
+	"context"
4
+	"net"
5
+	"net/http"
6
+	"os"
7
+	"path/filepath"
8
+
9
+	"github.com/docker/go-connections/sockets"
10
+	"github.com/docker/go-connections/tlsconfig"
11
+	"github.com/pkg/errors"
12
+)
13
+
14
+// FromEnv configures the client with values from environment variables.
15
+//
16
+// Supported environment variables:
17
+// DOCKER_HOST to set the url to the docker server.
18
+// DOCKER_API_VERSION to set the version of the API to reach, leave empty for latest.
19
+// DOCKER_CERT_PATH to load the TLS certificates from.
20
+// DOCKER_TLS_VERIFY to enable or disable TLS verification, off by default.
21
+func FromEnv(c *Client) error {
22
+	if dockerCertPath := os.Getenv("DOCKER_CERT_PATH"); dockerCertPath != "" {
23
+		options := tlsconfig.Options{
24
+			CAFile:             filepath.Join(dockerCertPath, "ca.pem"),
25
+			CertFile:           filepath.Join(dockerCertPath, "cert.pem"),
26
+			KeyFile:            filepath.Join(dockerCertPath, "key.pem"),
27
+			InsecureSkipVerify: os.Getenv("DOCKER_TLS_VERIFY") == "",
28
+		}
29
+		tlsc, err := tlsconfig.Client(options)
30
+		if err != nil {
31
+			return err
32
+		}
33
+
34
+		c.client = &http.Client{
35
+			Transport:     &http.Transport{TLSClientConfig: tlsc},
36
+			CheckRedirect: CheckRedirect,
37
+		}
38
+	}
39
+
40
+	if host := os.Getenv("DOCKER_HOST"); host != "" {
41
+		if err := WithHost(host)(c); err != nil {
42
+			return err
43
+		}
44
+	}
45
+
46
+	if version := os.Getenv("DOCKER_API_VERSION"); version != "" {
47
+		c.version = version
48
+		c.manualOverride = true
49
+	}
50
+	return nil
51
+}
52
+
53
+// WithDialer applies the dialer.DialContext to the client transport. This can be
54
+// used to set the Timeout and KeepAlive settings of the client.
55
+// Deprecated: use WithDialContext
56
+func WithDialer(dialer *net.Dialer) func(*Client) error {
57
+	return WithDialContext(dialer.DialContext)
58
+}
59
+
60
+// WithDialContext applies the dialer to the client transport. This can be
61
+// used to set the Timeout and KeepAlive settings of the client.
62
+func WithDialContext(dialContext func(ctx context.Context, network, addr string) (net.Conn, error)) func(*Client) error {
63
+	return func(c *Client) error {
64
+		if transport, ok := c.client.Transport.(*http.Transport); ok {
65
+			transport.DialContext = dialContext
66
+			return nil
67
+		}
68
+		return errors.Errorf("cannot apply dialer to transport: %T", c.client.Transport)
69
+	}
70
+}
71
+
72
+// WithHost overrides the client host with the specified one.
73
+func WithHost(host string) func(*Client) error {
74
+	return func(c *Client) error {
75
+		hostURL, err := ParseHostURL(host)
76
+		if err != nil {
77
+			return err
78
+		}
79
+		c.host = host
80
+		c.proto = hostURL.Scheme
81
+		c.addr = hostURL.Host
82
+		c.basePath = hostURL.Path
83
+		if transport, ok := c.client.Transport.(*http.Transport); ok {
84
+			return sockets.ConfigureTransport(transport, c.proto, c.addr)
85
+		}
86
+		return errors.Errorf("cannot apply host to transport: %T", c.client.Transport)
87
+	}
88
+}
89
+
90
+// WithHTTPClient overrides the client http client with the specified one
91
+func WithHTTPClient(client *http.Client) func(*Client) error {
92
+	return func(c *Client) error {
93
+		if client != nil {
94
+			c.client = client
95
+		}
96
+		return nil
97
+	}
98
+}
99
+
100
+// WithHTTPHeaders overrides the client default http headers
101
+func WithHTTPHeaders(headers map[string]string) func(*Client) error {
102
+	return func(c *Client) error {
103
+		c.customHTTPHeaders = headers
104
+		return nil
105
+	}
106
+}
107
+
108
+// WithScheme overrides the client scheme with the specified one
109
+func WithScheme(scheme string) func(*Client) error {
110
+	return func(c *Client) error {
111
+		c.scheme = scheme
112
+		return nil
113
+	}
114
+}
115
+
116
+// WithTLSClientConfig applies a tls config to the client transport.
117
+func WithTLSClientConfig(cacertPath, certPath, keyPath string) func(*Client) error {
118
+	return func(c *Client) error {
119
+		opts := tlsconfig.Options{
120
+			CAFile:             cacertPath,
121
+			CertFile:           certPath,
122
+			KeyFile:            keyPath,
123
+			ExclusiveRootPools: true,
124
+		}
125
+		config, err := tlsconfig.Client(opts)
126
+		if err != nil {
127
+			return errors.Wrap(err, "failed to create tls config")
128
+		}
129
+		if transport, ok := c.client.Transport.(*http.Transport); ok {
130
+			transport.TLSClientConfig = config
131
+			return nil
132
+		}
133
+		return errors.Errorf("cannot apply tls config to transport: %T", c.client.Transport)
134
+	}
135
+}
136
+
137
+// WithVersion overrides the client version with the specified one
138
+func WithVersion(version string) func(*Client) error {
139
+	return func(c *Client) error {
140
+		c.version = version
141
+		return nil
142
+	}
143
+}