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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-11-04 14:39:07 +01:00
parent 14f97cc10a
commit ed4b0a67be
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
5 changed files with 30 additions and 13 deletions

View File

@ -56,17 +56,26 @@ func runList(dockerCli command.Cli, opts *listOptions) error {
isCurrent := rawMeta.Name == curContext isCurrent := rawMeta.Name == curContext
meta, err := command.GetDockerContext(rawMeta) meta, err := command.GetDockerContext(rawMeta)
if err != nil { 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) dockerEndpoint, err := docker.EndpointFromContext(rawMeta)
if err != nil { if err != nil {
return err errMsg = err.Error()
} }
desc := formatter.ClientContext{ desc := formatter.ClientContext{
Name: rawMeta.Name, Name: rawMeta.Name,
Current: isCurrent, Current: isCurrent,
Description: meta.Description, Description: meta.Description,
DockerEndpoint: dockerEndpoint.Host, DockerEndpoint: dockerEndpoint.Host,
Error: errMsg,
} }
contexts = append(contexts, &desc) contexts = append(contexts, &desc)
} }

View File

@ -1,8 +1,8 @@
package formatter package formatter
const ( const (
// ClientContextTableFormat is the default client context format // ClientContextTableFormat is the default client context format.
ClientContextTableFormat = "table {{.Name}}{{if .Current}} *{{end}}\t{{.Description}}\t{{.DockerEndpoint}}" ClientContextTableFormat = "table {{.Name}}{{if .Current}} *{{end}}\t{{.Description}}\t{{.DockerEndpoint}}\t{{.Error}}"
dockerEndpointHeader = "DOCKER ENDPOINT" dockerEndpointHeader = "DOCKER ENDPOINT"
quietContextFormat = "{{.Name}}" quietContextFormat = "{{.Name}}"
@ -25,6 +25,7 @@ type ClientContext struct {
Description string Description string
DockerEndpoint string DockerEndpoint string
Current bool Current bool
Error string
} }
// ClientContextWrite writes formatted contexts using the Context // ClientContextWrite writes formatted contexts using the Context
@ -51,6 +52,7 @@ func newClientContextContext() *clientContextContext {
"Name": NameHeader, "Name": NameHeader,
"Description": DescriptionHeader, "Description": DescriptionHeader,
"DockerEndpoint": dockerEndpointHeader, "DockerEndpoint": dockerEndpointHeader,
"Error": ErrorHeader,
} }
return &ctx return &ctx
} }
@ -75,6 +77,12 @@ func (c *clientContextContext) DockerEndpoint() string {
return c.c.DockerEndpoint 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. // KubernetesEndpoint returns the kubernetes endpoint.
// //
// Deprecated: support for kubernetes endpoints in contexts has been removed, and this formatting option will always be empty. // Deprecated: support for kubernetes endpoints in contexts has been removed, and this formatting option will always be empty.

View File

@ -1,3 +1,3 @@
NAME DESCRIPTION DOCKER ENDPOINT NAME DESCRIPTION DOCKER ENDPOINT ERROR
default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock
remote my remote cluster ssh://someserver remote my remote cluster ssh://someserver

View File

@ -1,3 +1,3 @@
NAME DESCRIPTION DOCKER ENDPOINT NAME DESCRIPTION DOCKER ENDPOINT ERROR
default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock
test unix:///var/run/docker.sock test unix:///var/run/docker.sock

View File

@ -1,3 +1,3 @@
NAME DESCRIPTION DOCKER ENDPOINT NAME DESCRIPTION DOCKER ENDPOINT ERROR
default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock
remote my remote cluster ssh://someserver remote my remote cluster ssh://someserver