From 70738f83a55eee4c92f9e67b01eaa98440a21f99 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 4 Nov 2022 13:40:46 +0100 Subject: [PATCH] cli/command: DockerCli: CurrentContext(): resolve dynamically Store a reference to the options in the client, so that they can be used for later use. Now that resolveContextName() is a very lightweight function we can also dynamically get its value and use it for cli.CurrentContext(). Signed-off-by: Sebastiaan van Stijn --- cli/command/cli.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cli/command/cli.go b/cli/command/cli.go index f27d43a5f738..d28b2f67e03a 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -68,6 +68,7 @@ type Cli interface { // Instances of the client can be returned from NewDockerCli. type DockerCli struct { configFile *configfile.ConfigFile + options *cliflags.ClientOptions in *streams.In out *streams.Out err io.Writer @@ -75,7 +76,6 @@ type DockerCli struct { serverInfo ServerInfo contentTrust bool contextStore store.Store - currentContext string dockerEndpoint docker.Endpoint contextStoreConfig store.Config initTimeout time.Duration @@ -216,16 +216,16 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize } cli.loadConfigFile() + cli.options = opts baseContextStore := store.New(config.ContextStoreDir(), cli.contextStoreConfig) cli.contextStore = &ContextStoreWithDefault{ Store: baseContextStore, Resolver: func() (*DefaultContext, error) { - return ResolveDefaultContext(opts, cli.contextStoreConfig) + return ResolveDefaultContext(cli.options, cli.contextStoreConfig) }, } - cli.currentContext = resolveContextName(opts, cli.configFile) - cli.dockerEndpoint, err = resolveDockerEndpoint(cli.contextStore, cli.currentContext) + cli.dockerEndpoint, err = resolveDockerEndpoint(cli.contextStore, resolveContextName(opts, cli.configFile)) if err != nil { return errors.Wrap(err, "unable to resolve docker endpoint") } @@ -390,7 +390,7 @@ func (cli *DockerCli) ContextStore() store.Store { // CurrentContext does not validate if the given context exists or if it's // valid; errors may occur when trying to use it. func (cli *DockerCli) CurrentContext() string { - return cli.currentContext + return resolveContextName(cli.options, cli.configFile) } // CurrentContext returns the current context name, based on flags,