diff --git a/cli/command/context/list.go b/cli/command/context/list.go index 6943a99ea2..7798f131ae 100644 --- a/cli/command/context/list.go +++ b/cli/command/context/list.go @@ -50,10 +50,14 @@ func runList(dockerCli command.Cli, opts *listOptions) error { } var ( curContext = dockerCli.CurrentContext() + curFound bool contexts []*formatter.ClientContext ) for _, rawMeta := range contextMap { isCurrent := rawMeta.Name == curContext + if isCurrent { + curFound = true + } meta, err := command.GetDockerContext(rawMeta) if err != nil { // Add a stub-entry to the list, including the error-message @@ -79,6 +83,21 @@ func runList(dockerCli command.Cli, opts *listOptions) error { } contexts = append(contexts, &desc) } + if !curFound { + // The currently specified context wasn't found. We add a stub-entry + // to the list, including the error-message indicating that the context + // wasn't found. + var errMsg string + _, err := dockerCli.ContextStore().GetMetadata(curContext) + if err != nil { + errMsg = err.Error() + } + contexts = append(contexts, &formatter.ClientContext{ + Name: curContext, + Current: true, + Error: errMsg, + }) + } sort.Slice(contexts, func(i, j int) bool { return sortorder.NaturalLess(contexts[i].Name, contexts[j].Name) }) diff --git a/cli/command/context/list_test.go b/cli/command/context/list_test.go index ef6e6dfe0c..98ac2e480c 100644 --- a/cli/command/context/list_test.go +++ b/cli/command/context/list_test.go @@ -39,3 +39,11 @@ func TestListQuiet(t *testing.T) { assert.NilError(t, runList(cli, &listOptions{quiet: true})) golden.Assert(t, cli.OutBuffer().String(), "quiet-list.golden") } + +func TestListError(t *testing.T) { + cli := makeFakeCli(t) + cli.SetCurrentContext("nosuchcontext") + cli.OutBuffer().Reset() + assert.NilError(t, runList(cli, &listOptions{})) + golden.Assert(t, cli.OutBuffer().String(), "list-with-error.golden") +} diff --git a/cli/command/context/testdata/list-with-error.golden b/cli/command/context/testdata/list-with-error.golden new file mode 100644 index 0000000000..6d91b1b3ec --- /dev/null +++ b/cli/command/context/testdata/list-with-error.golden @@ -0,0 +1,3 @@ +NAME DESCRIPTION DOCKER ENDPOINT ERROR +default Current DOCKER_HOST based configuration unix:///var/run/docker.sock +nosuchcontext * context "nosuchcontext": context not found: … diff --git a/cli/command/context/testdata/list.golden b/cli/command/context/testdata/list.golden index aceb43ec60..d9b8121fcf 100644 --- a/cli/command/context/testdata/list.golden +++ b/cli/command/context/testdata/list.golden @@ -1,5 +1,5 @@ -NAME DESCRIPTION DOCKER ENDPOINT -current * description of current https://someswarmserver.example.com -default Current DOCKER_HOST based configuration unix:///var/run/docker.sock -other description of other https://someswarmserver.example.com -unset description of unset https://someswarmserver.example.com +NAME DESCRIPTION DOCKER ENDPOINT ERROR +current * description of current https://someswarmserver.example.com +default Current DOCKER_HOST based configuration unix:///var/run/docker.sock +other description of other https://someswarmserver.example.com +unset description of unset https://someswarmserver.example.com diff --git a/cli/command/formatter/context.go b/cli/command/formatter/context.go index c0a36d1ac9..71a53fd8ff 100644 --- a/cli/command/formatter/context.go +++ b/cli/command/formatter/context.go @@ -6,6 +6,8 @@ const ( dockerEndpointHeader = "DOCKER ENDPOINT" quietContextFormat = "{{.Name}}" + + maxErrLength = 45 ) // NewClientContextFormat returns a Format for rendering using a Context @@ -80,7 +82,7 @@ func (c *clientContextContext) DockerEndpoint() string { // Error returns the truncated error (if any) that occurred when loading the context. func (c *clientContextContext) Error() string { // TODO(thaJeztah) add "--no-trunc" option to context ls and set default to 30 cols to match "docker service ps" - return Ellipsis(c.c.Error, 45) + return Ellipsis(c.c.Error, maxErrLength) } // KubernetesEndpoint returns the kubernetes endpoint. diff --git a/cli/context/store/metadatastore.go b/cli/context/store/metadatastore.go index fa61c7aa48..ba3ea6c05a 100644 --- a/cli/context/store/metadatastore.go +++ b/cli/context/store/metadatastore.go @@ -59,7 +59,7 @@ func parseTypedOrMap(payload []byte, getter TypeGetter) (interface{}, error) { func (s *metadataStore) get(name string) (Metadata, error) { m, err := s.getByID(contextdirOf(name)) if err != nil { - return m, errors.Wrapf(err, "load context %q", name) + return m, errors.Wrapf(err, "context %q", name) } return m, nil } @@ -68,7 +68,7 @@ func (s *metadataStore) getByID(id contextdir) (Metadata, error) { bytes, err := os.ReadFile(filepath.Join(s.contextDir(id), metaFile)) if err != nil { if errors.Is(err, os.ErrNotExist) { - return Metadata{}, errdefs.NotFound(errors.Wrap(err, "context does not exist")) + return Metadata{}, errdefs.NotFound(errors.Wrap(err, "context not found")) } return Metadata{}, err }