diff --git a/cli/command/cli.go b/cli/command/cli.go index 91c4ebd0ef12..5d7572a6cb14 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -105,7 +105,7 @@ func (cli *DockerCli) CurrentVersion() string { // Client returns the APIClient func (cli *DockerCli) Client() client.APIClient { if err := cli.initialize(); err != nil { - _, _ = fmt.Fprintf(cli.Err(), "Failed to initialize: %s\n", err) + _, _ = fmt.Fprintf(cli.Err(), "failed to initialize: %s\n", err) os.Exit(1) } return cli.client @@ -152,7 +152,7 @@ func (cli *DockerCli) ConfigFile() *configfile.ConfigFile { // ServerInfo returns the server version details for the host this client is // connected to func (cli *DockerCli) ServerInfo() ServerInfo { - // TODO(thaJeztah) make ServerInfo() lazily load the info (ping only when needed) + _ = cli.initialize() return cli.serverInfo } @@ -429,16 +429,24 @@ func (cli *DockerCli) DockerEndpoint() docker.Endpoint { if err := cli.initialize(); err != nil { // Note that we're not terminating here, as this function may be used // in cases where we're able to continue. - _, _ = fmt.Fprintf(cli.Err(), "Failed to initialize: %s\n", cli.initErr) + _, _ = fmt.Fprintf(cli.Err(), "%s\n", cli.initErr) } return cli.dockerEndpoint } +func (cli *DockerCli) getDockerEndPoint() (ep docker.Endpoint, err error) { + cn := cli.CurrentContext() + if cn == DefaultContextName { + return resolveDefaultDockerEndpoint(cli.options) + } + return resolveDockerEndpoint(cli.contextStore, cn) +} + func (cli *DockerCli) initialize() error { cli.init.Do(func() { - cli.dockerEndpoint, cli.initErr = resolveDockerEndpoint(cli.contextStore, resolveContextName(cli.options, cli.configFile)) + cli.dockerEndpoint, cli.initErr = cli.getDockerEndPoint() if cli.initErr != nil { - cli.initErr = errors.Wrap(cli.initErr, "unable to resolve docker endpoint") + cli.initErr = errors.Wrap(cli.initErr, "failed to initialize") return } if cli.client == nil { diff --git a/cli/command/cli_test.go b/cli/command/cli_test.go index ea82d3a41205..4b29605ae0a9 100644 --- a/cli/command/cli_test.go +++ b/cli/command/cli_test.go @@ -118,7 +118,7 @@ func (c *fakeClient) NegotiateAPIVersionPing(types.Ping) { } func TestInitializeFromClient(t *testing.T) { - defaultVersion := "v1.55" + const defaultVersion = "v1.55" testcases := []struct { doc string @@ -160,10 +160,13 @@ func TestInitializeFromClient(t *testing.T) { } cli := &DockerCli{ - client: apiclient, - err: io.Discard, + client: apiclient, + currentContext: DefaultContextName, + err: io.Discard, + options: flags.NewClientOptions(), } - cli.initializeFromClient() + err := cli.initialize() + assert.NilError(t, err) assert.DeepEqual(t, cli.ServerInfo(), testcase.expectedServer) assert.Equal(t, apiclient.negotiated, testcase.negotiated) }) diff --git a/e2e/global/cli_test.go b/e2e/global/cli_test.go index 863079b1c32c..96880b38a57d 100644 --- a/e2e/global/cli_test.go +++ b/e2e/global/cli_test.go @@ -21,7 +21,7 @@ func TestTLSVerify(t *testing.T) { // Regardless of whether we specify true or false we need to // test to make sure tls is turned on if --tlsverify is specified at all result := icmd.RunCmd(icmd.Command("docker", "--tlsverify=false", "ps")) - result.Assert(t, icmd.Expected{ExitCode: 1, Err: "unable to resolve docker endpoint:"}) + result.Assert(t, icmd.Expected{ExitCode: 1, Err: "failed to initialize:"}) result = icmd.RunCmd(icmd.Command("docker", "--tlsverify=true", "ps")) result.Assert(t, icmd.Expected{ExitCode: 1, Err: "ca.pem"})