From 22a5dad847f53dd5d1bde9a61027d13c0cbce94d Mon Sep 17 00:00:00 2001 From: Anca Iordache Date: Fri, 20 Sep 2019 17:13:26 +0200 Subject: [PATCH] app-214 Load Client info in getter function Signed-off-by: Anca Iordache 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 --- cli/command/cli.go | 56 +++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/cli/command/cli.go b/cli/command/cli.go index 186854f866..c2f601d866 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -25,6 +25,7 @@ import ( "github.com/docker/cli/internal/containerizedengine" dopts "github.com/docker/cli/opts" clitypes "github.com/docker/cli/types" + "github.com/docker/docker/api" "github.com/docker/docker/api/types" registrytypes "github.com/docker/docker/api/types/registry" "github.com/docker/docker/client" @@ -76,7 +77,7 @@ type DockerCli struct { err io.Writer client client.APIClient serverInfo ServerInfo - clientInfo ClientInfo + clientInfo *ClientInfo contentTrust bool newContainerizeClient func(string) (clitypes.ContainerizedClient, error) contextStore store.Store @@ -87,7 +88,7 @@ type DockerCli struct { // DefaultVersion returns api.defaultVersion or DOCKER_API_VERSION if specified. func (cli *DockerCli) DefaultVersion() string { - return cli.clientInfo.DefaultVersion + return cli.ClientInfo().DefaultVersion } // Client returns the APIClient @@ -126,9 +127,16 @@ func ShowHelp(err io.Writer) func(*cobra.Command, []string) error { // ConfigFile returns the ConfigFile func (cli *DockerCli) ConfigFile() *configfile.ConfigFile { + if cli.configFile == nil { + cli.loadConfigFile() + } return cli.configFile } +func (cli *DockerCli) loadConfigFile() { + cli.configFile = cliconfig.LoadDefaultConfigFile(cli.err) +} + // ServerInfo returns the server version details for the host this client is // connected to func (cli *DockerCli) ServerInfo() ServerInfo { @@ -137,7 +145,34 @@ func (cli *DockerCli) ServerInfo() ServerInfo { // ClientInfo returns the client details for the cli func (cli *DockerCli) ClientInfo() ClientInfo { - return cli.clientInfo + if cli.clientInfo == nil { + _ = cli.loadClientInfo() + } + return *cli.clientInfo +} + +func (cli *DockerCli) loadClientInfo() error { + var experimentalValue string + // Environment variable always overrides configuration + if experimentalValue = os.Getenv("DOCKER_CLI_EXPERIMENTAL"); experimentalValue == "" { + experimentalValue = cli.ConfigFile().Experimental + } + hasExperimental, err := isEnabled(experimentalValue) + if err != nil { + return errors.Wrap(err, "Experimental field") + } + + var v string + if cli.client != nil { + v = cli.client.ClientVersion() + } else { + v = api.DefaultVersion + } + cli.clientInfo = &ClientInfo{ + DefaultVersion: v, + HasExperimental: hasExperimental, + } + return nil } // ContentTrustEnabled returns whether content trust has been enabled by an @@ -207,7 +242,7 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize debug.Enable() } - cli.configFile = cliconfig.LoadDefaultConfigFile(cli.err) + cli.loadConfigFile() baseContextStore := store.New(cliconfig.ContextStoreDir(), cli.contextStoreConfig) cli.contextStore = &ContextStoreWithDefault{ @@ -239,18 +274,9 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize return err } } - var experimentalValue string - // Environment variable always overrides configuration - if experimentalValue = os.Getenv("DOCKER_CLI_EXPERIMENTAL"); experimentalValue == "" { - experimentalValue = cli.configFile.Experimental - } - hasExperimental, err := isEnabled(experimentalValue) + err = cli.loadClientInfo() if err != nil { - return errors.Wrap(err, "Experimental field") - } - cli.clientInfo = ClientInfo{ - DefaultVersion: cli.client.ClientVersion(), - HasExperimental: hasExperimental, + return err } cli.initializeFromClient() return nil