diff --git a/cli/command/context.go b/cli/command/context.go index f6367c4a48..29a2be860f 100644 --- a/cli/command/context.go +++ b/cli/command/context.go @@ -58,5 +58,8 @@ func GetDockerContext(storeMetadata store.Metadata) (DockerContext, error) { if !ok { return DockerContext{}, errors.New("context metadata is not a valid DockerContext") } + if storeMetadata.Name == DefaultContextName { + res.Description = "Current DOCKER_HOST based configuration" + } return res, nil } diff --git a/cli/command/context/list.go b/cli/command/context/list.go index 9833581eed..5077157352 100644 --- a/cli/command/context/list.go +++ b/cli/command/context/list.go @@ -44,13 +44,16 @@ func runList(dockerCli command.Cli, opts *listOptions) error { if opts.format == "" { opts.format = formatter.TableFormatKey } - curContext := dockerCli.CurrentContext() contextMap, err := dockerCli.ContextStore().List() if err != nil { return err } - var contexts []*formatter.ClientContext + var ( + curContext = dockerCli.CurrentContext() + contexts []*formatter.ClientContext + ) for _, rawMeta := range contextMap { + isCurrent := rawMeta.Name == curContext meta, err := command.GetDockerContext(rawMeta) if err != nil { return err @@ -59,12 +62,9 @@ func runList(dockerCli command.Cli, opts *listOptions) error { if err != nil { return err } - if rawMeta.Name == command.DefaultContextName { - meta.Description = "Current DOCKER_HOST based configuration" - } desc := formatter.ClientContext{ Name: rawMeta.Name, - Current: rawMeta.Name == curContext, + Current: isCurrent, Description: meta.Description, DockerEndpoint: dockerEndpoint.Host, } @@ -77,7 +77,7 @@ func runList(dockerCli command.Cli, opts *listOptions) error { return err } if os.Getenv(client.EnvOverrideHost) != "" { - fmt.Fprintf(dockerCli.Err(), "Warning: %[1]s environment variable overrides the active context. "+ + _, _ = fmt.Fprintf(dockerCli.Err(), "Warning: %[1]s environment variable overrides the active context. "+ "To use a context, either set the global --context flag, or unset %[1]s environment variable.\n", client.EnvOverrideHost) } return nil diff --git a/cli/command/context_test.go b/cli/command/context_test.go index 0f5d9a88d2..7ef2157942 100644 --- a/cli/command/context_test.go +++ b/cli/command/context_test.go @@ -16,7 +16,8 @@ func TestDockerContextMetadataKeepAdditionalFields(t *testing.T) { } jsonBytes, err := json.Marshal(c) assert.NilError(t, err) - assert.Equal(t, `{"Description":"test","foo":"bar"}`, string(jsonBytes)) + const expected = `{"Description":"test","foo":"bar"}` + assert.Equal(t, string(jsonBytes), expected) var c2 DockerContext assert.NilError(t, json.Unmarshal(jsonBytes, &c2)) diff --git a/cli/context/store/metadatastore.go b/cli/context/store/metadatastore.go index 18c1b4c4ca..fa61c7aa48 100644 --- a/cli/context/store/metadatastore.go +++ b/cli/context/store/metadatastore.go @@ -101,7 +101,7 @@ func (s *metadataStore) remove(name string) error { func (s *metadataStore) list() ([]Metadata, error) { ctxDirs, err := listRecursivelyMetadataDirs(s.root) if err != nil { - if os.IsNotExist(err) { + if errors.Is(err, os.ErrNotExist) { return nil, nil } return nil, err @@ -110,7 +110,7 @@ func (s *metadataStore) list() ([]Metadata, error) { for _, dir := range ctxDirs { c, err := s.getByID(contextdir(dir)) if err != nil { - if os.IsNotExist(err) { + if errors.Is(err, os.ErrNotExist) { continue } return nil, errors.Wrap(err, "failed to read metadata")