Skip to content

Commit aa97619

Browse files
committed
cli/command: don't wrap client options
We may still change this, but in the client module, the signature of the client.Opt changed to now include a non-exported type, which means that we can't construct a custom option that is implemented using client options: #18 16.94 # github.com/docker/cli/cli/context/docker #18 16.94 cli/context/docker/load.go:105:29: cannot use withHTTPClient(tlsConfig) (value of type func(*client.Client) error) as client.Opt value in argument to append #18 16.94 cli/context/docker/load.go:152:6: cannot use c (variable of type *client.Client) as *client.clientConfig value in argument to client.WithHTTPClient(&http.Client{…}) We can consider exporting the `client.clientConfig` type (but keep its fields non-exported), but for this use, we don't strictly need it, so let's change the implementation to not having to depend on that. Signed-off-by: Sebastiaan van Stijn <[email protected]> (cherry picked from commit e7d14d9) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 2de1ea9 commit aa97619

File tree

2 files changed

+55
-50
lines changed

2 files changed

+55
-50
lines changed

cli/command/cli.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,14 @@ func newAPIClientFromEndpoint(ep docker.Endpoint, configFile *configfile.ConfigF
324324
if len(configFile.HTTPHeaders) > 0 {
325325
opts = append(opts, client.WithHTTPHeaders(configFile.HTTPHeaders))
326326
}
327-
opts = append(opts, withCustomHeadersFromEnv(), client.WithUserAgent(UserAgent()))
327+
withCustomHeaders, err := withCustomHeadersFromEnv()
328+
if err != nil {
329+
return nil, err
330+
}
331+
if withCustomHeaders != nil {
332+
opts = append(opts, withCustomHeaders)
333+
}
334+
opts = append(opts, client.WithUserAgent(UserAgent()))
328335
return client.NewClientWithOpts(opts...)
329336
}
330337

cli/command/cli_options.go

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -180,61 +180,59 @@ const envOverrideHTTPHeaders = "DOCKER_CUSTOM_HEADERS"
180180
// override headers with the same name).
181181
//
182182
// TODO(thaJeztah): this is a client Option, and should be moved to the client. It is non-exported for that reason.
183-
func withCustomHeadersFromEnv() client.Opt {
184-
return func(apiClient *client.Client) error {
185-
value := os.Getenv(envOverrideHTTPHeaders)
186-
if value == "" {
187-
return nil
188-
}
189-
csvReader := csv.NewReader(strings.NewReader(value))
190-
fields, err := csvReader.Read()
191-
if err != nil {
192-
return invalidParameter(errors.Errorf(
193-
"failed to parse custom headers from %s environment variable: value must be formatted as comma-separated key=value pairs",
194-
envOverrideHTTPHeaders,
195-
))
196-
}
197-
if len(fields) == 0 {
198-
return nil
199-
}
200-
201-
env := map[string]string{}
202-
for _, kv := range fields {
203-
k, v, hasValue := strings.Cut(kv, "=")
204-
205-
// Only strip whitespace in keys; preserve whitespace in values.
206-
k = strings.TrimSpace(k)
183+
func withCustomHeadersFromEnv() (client.Opt, error) {
184+
value := os.Getenv(envOverrideHTTPHeaders)
185+
if value == "" {
186+
return nil, nil
187+
}
188+
csvReader := csv.NewReader(strings.NewReader(value))
189+
fields, err := csvReader.Read()
190+
if err != nil {
191+
return nil, invalidParameter(errors.Errorf(
192+
"failed to parse custom headers from %s environment variable: value must be formatted as comma-separated key=value pairs",
193+
envOverrideHTTPHeaders,
194+
))
195+
}
196+
if len(fields) == 0 {
197+
return nil, nil
198+
}
207199

208-
if k == "" {
209-
return invalidParameter(errors.Errorf(
210-
`failed to set custom headers from %s environment variable: value contains a key=value pair with an empty key: '%s'`,
211-
envOverrideHTTPHeaders, kv,
212-
))
213-
}
200+
env := map[string]string{}
201+
for _, kv := range fields {
202+
k, v, hasValue := strings.Cut(kv, "=")
214203

215-
// We don't currently allow empty key=value pairs, and produce an error.
216-
// This is something we could allow in future (e.g. to read value
217-
// from an environment variable with the same name). In the meantime,
218-
// produce an error to prevent users from depending on this.
219-
if !hasValue {
220-
return invalidParameter(errors.Errorf(
221-
`failed to set custom headers from %s environment variable: missing "=" in key=value pair: '%s'`,
222-
envOverrideHTTPHeaders, kv,
223-
))
224-
}
204+
// Only strip whitespace in keys; preserve whitespace in values.
205+
k = strings.TrimSpace(k)
225206

226-
env[http.CanonicalHeaderKey(k)] = v
207+
if k == "" {
208+
return nil, invalidParameter(errors.Errorf(
209+
`failed to set custom headers from %s environment variable: value contains a key=value pair with an empty key: '%s'`,
210+
envOverrideHTTPHeaders, kv,
211+
))
227212
}
228213

229-
if len(env) == 0 {
230-
// We should probably not hit this case, as we don't skip values
231-
// (only return errors), but we don't want to discard existing
232-
// headers with an empty set.
233-
return nil
214+
// We don't currently allow empty key=value pairs, and produce an error.
215+
// This is something we could allow in future (e.g. to read value
216+
// from an environment variable with the same name). In the meantime,
217+
// produce an error to prevent users from depending on this.
218+
if !hasValue {
219+
return nil, invalidParameter(errors.Errorf(
220+
`failed to set custom headers from %s environment variable: missing "=" in key=value pair: '%s'`,
221+
envOverrideHTTPHeaders, kv,
222+
))
234223
}
235224

236-
// TODO(thaJeztah): add a client.WithExtraHTTPHeaders() function to allow these headers to be _added_ to existing ones, instead of _replacing_
237-
// see https://github.com/docker/cli/pull/5098#issuecomment-2147403871 (when updating, also update the WARNING in the function and env-var GoDoc)
238-
return client.WithHTTPHeaders(env)(apiClient)
225+
env[http.CanonicalHeaderKey(k)] = v
226+
}
227+
228+
if len(env) == 0 {
229+
// We should probably not hit this case, as we don't skip values
230+
// (only return errors), but we don't want to discard existing
231+
// headers with an empty set.
232+
return nil, nil
239233
}
234+
235+
// TODO(thaJeztah): add a client.WithExtraHTTPHeaders() function to allow these headers to be _added_ to existing ones, instead of _replacing_
236+
// see https://github.com/docker/cli/pull/5098#issuecomment-2147403871 (when updating, also update the WARNING in the function and env-var GoDoc)
237+
return client.WithHTTPHeaders(env), nil
240238
}

0 commit comments

Comments
 (0)