Browse code

client: define "Opt" type

Minor improvement, but makes defining a list of options
a bit cleaner, and more descriptive;

Before:

opts := make([]func(*client.Client) error, 0)

After:

opts := make([]client.Opt, 0)

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

Sebastiaan van Stijn authored on 2019/04/10 08:11:53
Showing 3 changed files
... ...
@@ -107,7 +107,7 @@ func CheckRedirect(req *http.Request, via []*http.Request) error {
107 107
 // It won't send any version information if the version number is empty. It is
108 108
 // highly recommended that you set a version or your client may break if the
109 109
 // server is upgraded.
110
-func NewClientWithOpts(ops ...func(*Client) error) (*Client, error) {
110
+func NewClientWithOpts(ops ...Opt) (*Client, error) {
111 111
 	client, err := defaultHTTPClient(DefaultDockerHost)
112 112
 	if err != nil {
113 113
 		return nil, err
... ...
@@ -12,6 +12,9 @@ import (
12 12
 	"github.com/pkg/errors"
13 13
 )
14 14
 
15
+// Opt is a configuration option to initialize a client
16
+type Opt func(*Client) error
17
+
15 18
 // FromEnv configures the client with values from environment variables.
16 19
 //
17 20
 // Supported environment variables:
... ...
@@ -55,13 +58,13 @@ func FromEnv(c *Client) error {
55 55
 // WithDialer applies the dialer.DialContext to the client transport. This can be
56 56
 // used to set the Timeout and KeepAlive settings of the client.
57 57
 // Deprecated: use WithDialContext
58
-func WithDialer(dialer *net.Dialer) func(*Client) error {
58
+func WithDialer(dialer *net.Dialer) Opt {
59 59
 	return WithDialContext(dialer.DialContext)
60 60
 }
61 61
 
62 62
 // WithDialContext applies the dialer to the client transport. This can be
63 63
 // used to set the Timeout and KeepAlive settings of the client.
64
-func WithDialContext(dialContext func(ctx context.Context, network, addr string) (net.Conn, error)) func(*Client) error {
64
+func WithDialContext(dialContext func(ctx context.Context, network, addr string) (net.Conn, error)) Opt {
65 65
 	return func(c *Client) error {
66 66
 		if transport, ok := c.client.Transport.(*http.Transport); ok {
67 67
 			transport.DialContext = dialContext
... ...
@@ -72,7 +75,7 @@ func WithDialContext(dialContext func(ctx context.Context, network, addr string)
72 72
 }
73 73
 
74 74
 // WithHost overrides the client host with the specified one.
75
-func WithHost(host string) func(*Client) error {
75
+func WithHost(host string) Opt {
76 76
 	return func(c *Client) error {
77 77
 		hostURL, err := ParseHostURL(host)
78 78
 		if err != nil {
... ...
@@ -90,7 +93,7 @@ func WithHost(host string) func(*Client) error {
90 90
 }
91 91
 
92 92
 // WithHTTPClient overrides the client http client with the specified one
93
-func WithHTTPClient(client *http.Client) func(*Client) error {
93
+func WithHTTPClient(client *http.Client) Opt {
94 94
 	return func(c *Client) error {
95 95
 		if client != nil {
96 96
 			c.client = client
... ...
@@ -100,7 +103,7 @@ func WithHTTPClient(client *http.Client) func(*Client) error {
100 100
 }
101 101
 
102 102
 // WithHTTPHeaders overrides the client default http headers
103
-func WithHTTPHeaders(headers map[string]string) func(*Client) error {
103
+func WithHTTPHeaders(headers map[string]string) Opt {
104 104
 	return func(c *Client) error {
105 105
 		c.customHTTPHeaders = headers
106 106
 		return nil
... ...
@@ -108,7 +111,7 @@ func WithHTTPHeaders(headers map[string]string) func(*Client) error {
108 108
 }
109 109
 
110 110
 // WithScheme overrides the client scheme with the specified one
111
-func WithScheme(scheme string) func(*Client) error {
111
+func WithScheme(scheme string) Opt {
112 112
 	return func(c *Client) error {
113 113
 		c.scheme = scheme
114 114
 		return nil
... ...
@@ -116,7 +119,7 @@ func WithScheme(scheme string) func(*Client) error {
116 116
 }
117 117
 
118 118
 // WithTLSClientConfig applies a tls config to the client transport.
119
-func WithTLSClientConfig(cacertPath, certPath, keyPath string) func(*Client) error {
119
+func WithTLSClientConfig(cacertPath, certPath, keyPath string) Opt {
120 120
 	return func(c *Client) error {
121 121
 		opts := tlsconfig.Options{
122 122
 			CAFile:             cacertPath,
... ...
@@ -137,7 +140,7 @@ func WithTLSClientConfig(cacertPath, certPath, keyPath string) func(*Client) err
137 137
 }
138 138
 
139 139
 // WithVersion overrides the client version with the specified one
140
-func WithVersion(version string) func(*Client) error {
140
+func WithVersion(version string) Opt {
141 141
 	return func(c *Client) error {
142 142
 		c.version = version
143 143
 		c.manualOverride = true
... ...
@@ -25,11 +25,11 @@ import (
25 25
 )
26 26
 
27 27
 // NewAPIClient returns a docker API client configured from environment variables
28
-func NewAPIClient(t assert.TestingT, ops ...func(*client.Client) error) client.APIClient {
28
+func NewAPIClient(t assert.TestingT, ops ...client.Opt) client.APIClient {
29 29
 	if ht, ok := t.(test.HelperT); ok {
30 30
 		ht.Helper()
31 31
 	}
32
-	ops = append([]func(*client.Client) error{client.FromEnv}, ops...)
32
+	ops = append([]client.Opt{client.FromEnv}, ops...)
33 33
 	clt, err := client.NewClientWithOpts(ops...)
34 34
 	assert.NilError(t, err)
35 35
 	return clt