Skip to content

Commit

Permalink
WIP: fixing up things
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Nov 28, 2022
1 parent 79225a1 commit 8c49008
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
18 changes: 13 additions & 5 deletions cli/command/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 7 additions & 4 deletions cli/command/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
Expand Down
2 changes: 1 addition & 1 deletion e2e/global/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down

0 comments on commit 8c49008

Please sign in to comment.