Skip to content

Commit be985bd

Browse files
committed
cli/command/stack/swarm: use errors.Join
Use stdlib multi-errors instead of creating our own Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent f1193ef commit be985bd

2 files changed

Lines changed: 7 additions & 11 deletions

File tree

cli/command/stack/remove_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func TestRemoveContinueAfterError(t *testing.T) {
161161
cmd.SetErr(io.Discard)
162162
cmd.SetArgs([]string{"foo", "bar"})
163163

164-
assert.Error(t, cmd.Execute(), "Failed to remove some resources from stack: foo")
164+
assert.Error(t, cmd.Execute(), "failed to remove some resources from stack: foo")
165165
assert.Check(t, is.DeepEqual(allServiceIDs, removedServices))
166166
assert.Check(t, is.DeepEqual(allNetworkIDs, cli.removedNetworks))
167167
assert.Check(t, is.DeepEqual(allSecretIDs, cli.removedSecrets))

cli/command/stack/swarm/remove.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@ package swarm
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"sort"
7-
"strings"
88

99
"github.com/docker/cli/cli/command"
1010
"github.com/docker/cli/cli/command/stack/options"
1111
"github.com/docker/docker/api/types/network"
1212
"github.com/docker/docker/api/types/swarm"
1313
"github.com/docker/docker/api/types/versions"
1414
"github.com/docker/docker/client"
15-
"github.com/pkg/errors"
1615
)
1716

1817
// RunRemove is the swarm implementation of docker stack remove
1918
func RunRemove(ctx context.Context, dockerCli command.Cli, opts options.Remove) error {
2019
apiClient := dockerCli.Client()
2120

22-
var errs []string
21+
var errs []error
2322
for _, namespace := range opts.Namespaces {
2423
services, err := getStackServices(ctx, apiClient, namespace)
2524
if err != nil {
@@ -52,28 +51,25 @@ func RunRemove(ctx context.Context, dockerCli command.Cli, opts options.Remove)
5251
continue
5352
}
5453

54+
// TODO(thaJeztah): change this "hasError" boolean to return a (multi-)error for each of these functions instead.
5555
hasError := removeServices(ctx, dockerCli, services)
5656
hasError = removeSecrets(ctx, dockerCli, secrets) || hasError
5757
hasError = removeConfigs(ctx, dockerCli, configs) || hasError
5858
hasError = removeNetworks(ctx, dockerCli, networks) || hasError
5959

6060
if hasError {
61-
errs = append(errs, "Failed to remove some resources from stack: "+namespace)
61+
errs = append(errs, errors.New("failed to remove some resources from stack: "+namespace))
6262
continue
6363
}
6464

6565
if !opts.Detach {
6666
err = waitOnTasks(ctx, apiClient, namespace)
6767
if err != nil {
68-
errs = append(errs, fmt.Sprintf("Failed to wait on tasks of stack: %s: %s", namespace, err))
68+
errs = append(errs, fmt.Errorf("failed to wait on tasks of stack: %s: %w", namespace, err))
6969
}
7070
}
7171
}
72-
73-
if len(errs) > 0 {
74-
return errors.New(strings.Join(errs, "\n"))
75-
}
76-
return nil
72+
return errors.Join(errs...)
7773
}
7874

7975
func sortServiceByName(services []swarm.Service) func(i, j int) bool {

0 commit comments

Comments
 (0)