From 6a734df1cca45d452e0dddeaee7b648c6418d30a Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 4 Nov 2022 14:01:42 +0100 Subject: [PATCH 1/4] cli/context/store: make sure we handle wrapped errors Signed-off-by: Sebastiaan van Stijn --- cli/context/store/metadatastore.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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") From acb934cc9de86b8c6c20088ccc061922cae9bb84 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 4 Nov 2022 12:34:14 +0100 Subject: [PATCH 2/4] cli/command: move default context description to context itself Signed-off-by: Sebastiaan van Stijn --- cli/command/context.go | 3 +++ cli/command/context/list.go | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) 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..d56ca8a7b1 100644 --- a/cli/command/context/list.go +++ b/cli/command/context/list.go @@ -59,9 +59,6 @@ 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, From 0cc3f688d6d77b7a5b84189151ad2dca0267a300 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 4 Nov 2022 14:02:35 +0100 Subject: [PATCH 3/4] cli/command: use more descriptive const in test On failures, the output wasn't clear which side was "expected" and which side was "actual". Signed-off-by: Sebastiaan van Stijn --- cli/command/context_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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)) From 592d90cafaa4097a63ac1ff7085d150deeb306f8 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 4 Nov 2022 14:07:20 +0100 Subject: [PATCH 4/4] cli/command/context: minor cleanup in runList() Signed-off-by: Sebastiaan van Stijn --- cli/command/context/list.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cli/command/context/list.go b/cli/command/context/list.go index d56ca8a7b1..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 @@ -61,7 +64,7 @@ func runList(dockerCli command.Cli, opts *listOptions) error { } desc := formatter.ClientContext{ Name: rawMeta.Name, - Current: rawMeta.Name == curContext, + Current: isCurrent, Description: meta.Description, DockerEndpoint: dockerEndpoint.Host, } @@ -74,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