diff --git a/cli/command/stack/remove_test.go b/cli/command/stack/remove_test.go index ed5c9e4f8b..1a6923b747 100644 --- a/cli/command/stack/remove_test.go +++ b/cli/command/stack/remove_test.go @@ -161,7 +161,7 @@ func TestRemoveContinueAfterError(t *testing.T) { cmd.SetErr(io.Discard) cmd.SetArgs([]string{"foo", "bar"}) - assert.Error(t, cmd.Execute(), "Failed to remove some resources from stack: foo") + assert.Error(t, cmd.Execute(), "failed to remove some resources from stack: foo") assert.Check(t, is.DeepEqual(allServiceIDs, removedServices)) assert.Check(t, is.DeepEqual(allNetworkIDs, cli.removedNetworks)) assert.Check(t, is.DeepEqual(allSecretIDs, cli.removedSecrets)) diff --git a/cli/command/stack/swarm/remove.go b/cli/command/stack/swarm/remove.go index 8c16a7fc04..cd426d5111 100644 --- a/cli/command/stack/swarm/remove.go +++ b/cli/command/stack/swarm/remove.go @@ -2,9 +2,9 @@ package swarm import ( "context" + "errors" "fmt" "sort" - "strings" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/stack/options" @@ -12,14 +12,13 @@ import ( "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/versions" "github.com/docker/docker/client" - "github.com/pkg/errors" ) // RunRemove is the swarm implementation of docker stack remove func RunRemove(ctx context.Context, dockerCli command.Cli, opts options.Remove) error { apiClient := dockerCli.Client() - var errs []string + var errs []error for _, namespace := range opts.Namespaces { services, err := getStackServices(ctx, apiClient, namespace) if err != nil { @@ -52,28 +51,25 @@ func RunRemove(ctx context.Context, dockerCli command.Cli, opts options.Remove) continue } + // TODO(thaJeztah): change this "hasError" boolean to return a (multi-)error for each of these functions instead. hasError := removeServices(ctx, dockerCli, services) hasError = removeSecrets(ctx, dockerCli, secrets) || hasError hasError = removeConfigs(ctx, dockerCli, configs) || hasError hasError = removeNetworks(ctx, dockerCli, networks) || hasError if hasError { - errs = append(errs, "Failed to remove some resources from stack: "+namespace) + errs = append(errs, errors.New("failed to remove some resources from stack: "+namespace)) continue } if !opts.Detach { err = waitOnTasks(ctx, apiClient, namespace) if err != nil { - errs = append(errs, fmt.Sprintf("Failed to wait on tasks of stack: %s: %s", namespace, err)) + errs = append(errs, fmt.Errorf("failed to wait on tasks of stack: %s: %w", namespace, err)) } } } - - if len(errs) > 0 { - return errors.New(strings.Join(errs, "\n")) - } - return nil + return errors.Join(errs...) } func sortServiceByName(services []swarm.Service) func(i, j int) bool {