cli/command/plugins: use errors.Join instead of custom cli.Errors

This command was using a custom "multi-error" implementation, but it
had some limitations, and the formatting wasn't great.

This patch replaces it with Go's errors.Join.

Before:

    docker plugin remove one two three
    Error response from daemon: plugin "one" not found, Error response from daemon: plugin "two" not found, Error response from daemon: plugin "three" not found

After:

    docker plugin remove one two three
    Error response from daemon: plugin "one" not found
    Error response from daemon: plugin "two" not found
    Error response from daemon: plugin "three" not found

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 71ebbb81ae)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2024-10-19 10:25:50 +02:00
parent 0008d79159
commit d3bf82db86
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 5 additions and 8 deletions

View File

@ -2,6 +2,7 @@ package plugin
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
@ -36,17 +37,13 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
} }
func runRemove(ctx context.Context, dockerCli command.Cli, opts *rmOptions) error { func runRemove(ctx context.Context, dockerCli command.Cli, opts *rmOptions) error {
var errs cli.Errors var errs error
for _, name := range opts.plugins { for _, name := range opts.plugins {
if err := dockerCli.Client().PluginRemove(ctx, name, types.PluginRemoveOptions{Force: opts.force}); err != nil { if err := dockerCli.Client().PluginRemove(ctx, name, types.PluginRemoveOptions{Force: opts.force}); err != nil {
errs = append(errs, err) errs = errors.Join(errs, err)
continue continue
} }
fmt.Fprintln(dockerCli.Out(), name) _, _ = fmt.Fprintln(dockerCli.Out(), name)
} }
// Do not simplify to `return errs` because even if errs == nil, it is not a nil-error interface value. return errs
if errs != nil {
return errs
}
return nil
} }