cli/command/image: use errors.Join

Use stdlib multi-errors instead of creating our own

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2025-02-03 19:20:34 +01:00
parent 2b9a4d5f4c
commit 7147e85f63
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

View File

@ -2,15 +2,14 @@ package image
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"strings"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion" "github.com/docker/cli/cli/command/completion"
"github.com/docker/docker/api/types/image" "github.com/docker/docker/api/types/image"
"github.com/docker/docker/errdefs" "github.com/docker/docker/errdefs"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -59,15 +58,16 @@ func runRemove(ctx context.Context, dockerCLI command.Cli, opts removeOptions, i
PruneChildren: !opts.noPrune, PruneChildren: !opts.noPrune,
} }
var errs []string // TODO(thaJeztah): this logic can likely be simplified: do we want to print "not found" errors at all when using "force"?
fatalErr := false fatalErr := false
var errs []error
for _, img := range images { for _, img := range images {
dels, err := apiClient.ImageRemove(ctx, img, options) dels, err := apiClient.ImageRemove(ctx, img, options)
if err != nil { if err != nil {
if !errdefs.IsNotFound(err) { if !errdefs.IsNotFound(err) {
fatalErr = true fatalErr = true
} }
errs = append(errs, err.Error()) errs = append(errs, err)
} else { } else {
for _, del := range dels { for _, del := range dels {
if del.Deleted != "" { if del.Deleted != "" {
@ -79,12 +79,11 @@ func runRemove(ctx context.Context, dockerCLI command.Cli, opts removeOptions, i
} }
} }
if len(errs) > 0 { if err := errors.Join(errs...); err != nil {
msg := strings.Join(errs, "\n")
if !opts.force || fatalErr { if !opts.force || fatalErr {
return errors.New(msg) return err
} }
_, _ = fmt.Fprintln(dockerCLI.Err(), msg) _, _ = fmt.Fprintln(dockerCLI.Err(), err)
} }
return nil return nil
} }