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
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
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)
}