Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 86b614a

Browse files
Anca IordachethaJeztah
authored andcommitted
app-214 Load Client info in getter function
Signed-off-by: Anca Iordache <anca.iordache@docker.com> Possible approach for client info - split ClientInfo() into ClientInfo() and loadClientInfo() - split ConfigFile() into ConfigFile() and loadConfigFile() - ConfigFile() and ClientInfo() call their corresponding loadXX function if it has not yet been loaded; this allows them to be used before Initialize() was called. - Initialize() *always* (re-)loads the configuration; this makes sure that the correct configuration is used when actually calling commands. Signed-off-by: Sebastiaan van Stijn <github@gone.nl> (cherry picked from commit 22a5dad847f53dd5d1bde9a61027d13c0cbce94d) Signed-off-by: Sebastiaan van Stijn <github@gone.nl> Upstream-commit: 25f04876d16b5ad2f120ca04ed77c71c6373637f Component: cli
1 parent a8a8c28 commit 86b614a

1 file changed

Lines changed: 41 additions & 15 deletions

File tree

  • components/cli/cli/command

components/cli/cli/command/cli.go

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/docker/cli/internal/containerizedengine"
2626
dopts "github.com/docker/cli/opts"
2727
clitypes "github.com/docker/cli/types"
28+
"github.com/docker/docker/api"
2829
"github.com/docker/docker/api/types"
2930
registrytypes "github.com/docker/docker/api/types/registry"
3031
"github.com/docker/docker/client"
@@ -76,7 +77,7 @@ type DockerCli struct {
7677
err io.Writer
7778
client client.APIClient
7879
serverInfo ServerInfo
79-
clientInfo ClientInfo
80+
clientInfo *ClientInfo
8081
contentTrust bool
8182
newContainerizeClient func(string) (clitypes.ContainerizedClient, error)
8283
contextStore store.Store
@@ -87,7 +88,7 @@ type DockerCli struct {
8788

8889
// DefaultVersion returns api.defaultVersion or DOCKER_API_VERSION if specified.
8990
func (cli *DockerCli) DefaultVersion() string {
90-
return cli.clientInfo.DefaultVersion
91+
return cli.ClientInfo().DefaultVersion
9192
}
9293

9394
// Client returns the APIClient
@@ -126,9 +127,16 @@ func ShowHelp(err io.Writer) func(*cobra.Command, []string) error {
126127

127128
// ConfigFile returns the ConfigFile
128129
func (cli *DockerCli) ConfigFile() *configfile.ConfigFile {
130+
if cli.configFile == nil {
131+
cli.loadConfigFile()
132+
}
129133
return cli.configFile
130134
}
131135

136+
func (cli *DockerCli) loadConfigFile() {
137+
cli.configFile = cliconfig.LoadDefaultConfigFile(cli.err)
138+
}
139+
132140
// ServerInfo returns the server version details for the host this client is
133141
// connected to
134142
func (cli *DockerCli) ServerInfo() ServerInfo {
@@ -137,7 +145,34 @@ func (cli *DockerCli) ServerInfo() ServerInfo {
137145

138146
// ClientInfo returns the client details for the cli
139147
func (cli *DockerCli) ClientInfo() ClientInfo {
140-
return cli.clientInfo
148+
if cli.clientInfo == nil {
149+
_ = cli.loadClientInfo()
150+
}
151+
return *cli.clientInfo
152+
}
153+
154+
func (cli *DockerCli) loadClientInfo() error {
155+
var experimentalValue string
156+
// Environment variable always overrides configuration
157+
if experimentalValue = os.Getenv("DOCKER_CLI_EXPERIMENTAL"); experimentalValue == "" {
158+
experimentalValue = cli.ConfigFile().Experimental
159+
}
160+
hasExperimental, err := isEnabled(experimentalValue)
161+
if err != nil {
162+
return errors.Wrap(err, "Experimental field")
163+
}
164+
165+
var v string
166+
if cli.client != nil {
167+
v = cli.client.ClientVersion()
168+
} else {
169+
v = api.DefaultVersion
170+
}
171+
cli.clientInfo = &ClientInfo{
172+
DefaultVersion: v,
173+
HasExperimental: hasExperimental,
174+
}
175+
return nil
141176
}
142177

143178
// ContentTrustEnabled returns whether content trust has been enabled by an
@@ -207,7 +242,7 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize
207242
debug.Enable()
208243
}
209244

210-
cli.configFile = cliconfig.LoadDefaultConfigFile(cli.err)
245+
cli.loadConfigFile()
211246

212247
baseContextStore := store.New(cliconfig.ContextStoreDir(), cli.contextStoreConfig)
213248
cli.contextStore = &ContextStoreWithDefault{
@@ -239,18 +274,9 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize
239274
return err
240275
}
241276
}
242-
var experimentalValue string
243-
// Environment variable always overrides configuration
244-
if experimentalValue = os.Getenv("DOCKER_CLI_EXPERIMENTAL"); experimentalValue == "" {
245-
experimentalValue = cli.configFile.Experimental
246-
}
247-
hasExperimental, err := isEnabled(experimentalValue)
277+
err = cli.loadClientInfo()
248278
if err != nil {
249-
return errors.Wrap(err, "Experimental field")
250-
}
251-
cli.clientInfo = ClientInfo{
252-
DefaultVersion: cli.client.ClientVersion(),
253-
HasExperimental: hasExperimental,
279+
return err
254280
}
255281
cli.initializeFromClient()
256282
return nil

0 commit comments

Comments
 (0)