Skip to content

Commit 80c24bf

Browse files
committed
feat(cli): add --http-header option
Signed-off-by: Zhaofeng Miao <522856232@qq.com>
1 parent 84b86e2 commit 80c24bf

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

cli/command/cli.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize
229229
}
230230

231231
if cli.client == nil {
232-
cli.client, err = newAPIClientFromEndpoint(cli.dockerEndpoint, cli.configFile)
232+
cli.client, err = newAPIClientFromEndpoint(opts.Common, cli.dockerEndpoint, cli.configFile)
233233
if err != nil {
234234
return err
235235
}
@@ -255,18 +255,26 @@ func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.
255255
if err != nil {
256256
return nil, errors.Wrap(err, "unable to resolve docker endpoint")
257257
}
258-
return newAPIClientFromEndpoint(endpoint, configFile)
258+
return newAPIClientFromEndpoint(opts, endpoint, configFile)
259259
}
260260

261-
func newAPIClientFromEndpoint(ep docker.Endpoint, configFile *configfile.ConfigFile) (client.APIClient, error) {
261+
func newAPIClientFromEndpoint(opts *cliflags.CommonOptions, ep docker.Endpoint, configFile *configfile.ConfigFile) (client.APIClient, error) {
262262
clientOpts, err := ep.ClientOpts()
263263
if err != nil {
264264
return nil, err
265265
}
266-
customHeaders := make(map[string]string, len(configFile.HTTPHeaders))
266+
customHeaders := make(map[string]string, len(configFile.HTTPHeaders)+len(opts.HttpHeaders))
267267
for k, v := range configFile.HTTPHeaders {
268268
customHeaders[k] = v
269269
}
270+
271+
for i := 0; i < len(opts.HttpHeaders); i++ {
272+
split := strings.Split(opts.HttpHeaders[i], ":")
273+
k := strings.TrimSpace(split[0])
274+
v := strings.TrimSpace(split[1])
275+
customHeaders[k] = v
276+
}
277+
270278
customHeaders["User-Agent"] = UserAgent()
271279
clientOpts = append(clientOpts, client.WithHTTPHeaders(customHeaders))
272280
return client.NewClientWithOpts(clientOpts...)

cli/flags/common.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ var (
4545

4646
// CommonOptions are options common to both the client and the daemon.
4747
type CommonOptions struct {
48-
Debug bool
49-
Hosts []string
50-
LogLevel string
51-
TLS bool
52-
TLSVerify bool
53-
TLSOptions *tlsconfig.Options
54-
Context string
48+
Debug bool
49+
Hosts []string
50+
LogLevel string
51+
TLS bool
52+
TLSVerify bool
53+
TLSOptions *tlsconfig.Options
54+
Context string
55+
HttpHeaders []string
5556
}
5657

5758
// NewCommonOptions returns a new CommonOptions
@@ -87,6 +88,8 @@ func (commonOpts *CommonOptions) InstallFlags(flags *pflag.FlagSet) {
8788
flags.VarP(hostOpt, "host", "H", "Daemon socket(s) to connect to")
8889
flags.StringVarP(&commonOpts.Context, "context", "c", "",
8990
`Name of the context to use to connect to the daemon (overrides `+client.EnvOverrideHost+` env var and default context set with "docker context use")`)
91+
httpHeaderOpt := opts.NewNamedListOptsRef("http-headers", &commonOpts.HttpHeaders, nil)
92+
flags.VarP(httpHeaderOpt, "http-header", "", "Custom HTTP headers")
9093
}
9194

9295
// SetDefaultOptions sets default values for options after flag parsing is

0 commit comments

Comments
 (0)