From ed4b0a67befc7d70c2f6cb2b8c65111ad6c8e57b Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 4 Nov 2022 14:39:07 +0100 Subject: [PATCH] cli/command/context: context ls: add ERROR column, and don't fail early This updates `docker context ls` to: - not abort listing contexts when failing one (or more) contexts - instead, adding an ERROR column to inform the user there was an issue loading the context. Signed-off-by: Sebastiaan van Stijn --- cli/command/context/list.go | 13 +++++++++++-- cli/command/formatter/context.go | 12 ++++++++++-- e2e/context/testdata/context-ls-notls.golden | 6 +++--- e2e/context/testdata/context-ls-tls.golden | 6 +++--- e2e/context/testdata/context-ls.golden | 6 +++--- 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/cli/command/context/list.go b/cli/command/context/list.go index 5077157352..6943a99ea2 100644 --- a/cli/command/context/list.go +++ b/cli/command/context/list.go @@ -56,17 +56,26 @@ func runList(dockerCli command.Cli, opts *listOptions) error { isCurrent := rawMeta.Name == curContext meta, err := command.GetDockerContext(rawMeta) if err != nil { - return err + // Add a stub-entry to the list, including the error-message + // indicating that the context couldn't be loaded. + contexts = append(contexts, &formatter.ClientContext{ + Name: rawMeta.Name, + Current: isCurrent, + Error: err.Error(), + }) + continue } + var errMsg string dockerEndpoint, err := docker.EndpointFromContext(rawMeta) if err != nil { - return err + errMsg = err.Error() } desc := formatter.ClientContext{ Name: rawMeta.Name, Current: isCurrent, Description: meta.Description, DockerEndpoint: dockerEndpoint.Host, + Error: errMsg, } contexts = append(contexts, &desc) } diff --git a/cli/command/formatter/context.go b/cli/command/formatter/context.go index 19780e7588..c0a36d1ac9 100644 --- a/cli/command/formatter/context.go +++ b/cli/command/formatter/context.go @@ -1,8 +1,8 @@ package formatter const ( - // ClientContextTableFormat is the default client context format - ClientContextTableFormat = "table {{.Name}}{{if .Current}} *{{end}}\t{{.Description}}\t{{.DockerEndpoint}}" + // ClientContextTableFormat is the default client context format. + ClientContextTableFormat = "table {{.Name}}{{if .Current}} *{{end}}\t{{.Description}}\t{{.DockerEndpoint}}\t{{.Error}}" dockerEndpointHeader = "DOCKER ENDPOINT" quietContextFormat = "{{.Name}}" @@ -25,6 +25,7 @@ type ClientContext struct { Description string DockerEndpoint string Current bool + Error string } // ClientContextWrite writes formatted contexts using the Context @@ -51,6 +52,7 @@ func newClientContextContext() *clientContextContext { "Name": NameHeader, "Description": DescriptionHeader, "DockerEndpoint": dockerEndpointHeader, + "Error": ErrorHeader, } return &ctx } @@ -75,6 +77,12 @@ func (c *clientContextContext) DockerEndpoint() string { return c.c.DockerEndpoint } +// 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) +} + // KubernetesEndpoint returns the kubernetes endpoint. // // Deprecated: support for kubernetes endpoints in contexts has been removed, and this formatting option will always be empty. diff --git a/e2e/context/testdata/context-ls-notls.golden b/e2e/context/testdata/context-ls-notls.golden index 2d33df13a4..c36c8e7bb6 100644 --- a/e2e/context/testdata/context-ls-notls.golden +++ b/e2e/context/testdata/context-ls-notls.golden @@ -1,3 +1,3 @@ -NAME DESCRIPTION DOCKER ENDPOINT -default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock -remote my remote cluster ssh://someserver +NAME DESCRIPTION DOCKER ENDPOINT ERROR +default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock +remote my remote cluster ssh://someserver diff --git a/e2e/context/testdata/context-ls-tls.golden b/e2e/context/testdata/context-ls-tls.golden index 13aed65a88..6d9756e7e5 100644 --- a/e2e/context/testdata/context-ls-tls.golden +++ b/e2e/context/testdata/context-ls-tls.golden @@ -1,3 +1,3 @@ -NAME DESCRIPTION DOCKER ENDPOINT -default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock -test unix:///var/run/docker.sock +NAME DESCRIPTION DOCKER ENDPOINT ERROR +default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock +test unix:///var/run/docker.sock diff --git a/e2e/context/testdata/context-ls.golden b/e2e/context/testdata/context-ls.golden index 2d33df13a4..c36c8e7bb6 100644 --- a/e2e/context/testdata/context-ls.golden +++ b/e2e/context/testdata/context-ls.golden @@ -1,3 +1,3 @@ -NAME DESCRIPTION DOCKER ENDPOINT -default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock -remote my remote cluster ssh://someserver +NAME DESCRIPTION DOCKER ENDPOINT ERROR +default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock +remote my remote cluster ssh://someserver