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>
This commit is contained in:
parent
f1193effc0
commit
be985bd28e
@ -161,7 +161,7 @@ func TestRemoveContinueAfterError(t *testing.T) {
|
|||||||
cmd.SetErr(io.Discard)
|
cmd.SetErr(io.Discard)
|
||||||
cmd.SetArgs([]string{"foo", "bar"})
|
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(allServiceIDs, removedServices))
|
||||||
assert.Check(t, is.DeepEqual(allNetworkIDs, cli.removedNetworks))
|
assert.Check(t, is.DeepEqual(allNetworkIDs, cli.removedNetworks))
|
||||||
assert.Check(t, is.DeepEqual(allSecretIDs, cli.removedSecrets))
|
assert.Check(t, is.DeepEqual(allSecretIDs, cli.removedSecrets))
|
||||||
|
@ -2,9 +2,9 @@ package swarm
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
"github.com/docker/cli/cli/command/stack/options"
|
"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/swarm"
|
||||||
"github.com/docker/docker/api/types/versions"
|
"github.com/docker/docker/api/types/versions"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// RunRemove is the swarm implementation of docker stack remove
|
// RunRemove is the swarm implementation of docker stack remove
|
||||||
func RunRemove(ctx context.Context, dockerCli command.Cli, opts options.Remove) error {
|
func RunRemove(ctx context.Context, dockerCli command.Cli, opts options.Remove) error {
|
||||||
apiClient := dockerCli.Client()
|
apiClient := dockerCli.Client()
|
||||||
|
|
||||||
var errs []string
|
var errs []error
|
||||||
for _, namespace := range opts.Namespaces {
|
for _, namespace := range opts.Namespaces {
|
||||||
services, err := getStackServices(ctx, apiClient, namespace)
|
services, err := getStackServices(ctx, apiClient, namespace)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -52,28 +51,25 @@ func RunRemove(ctx context.Context, dockerCli command.Cli, opts options.Remove)
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(thaJeztah): change this "hasError" boolean to return a (multi-)error for each of these functions instead.
|
||||||
hasError := removeServices(ctx, dockerCli, services)
|
hasError := removeServices(ctx, dockerCli, services)
|
||||||
hasError = removeSecrets(ctx, dockerCli, secrets) || hasError
|
hasError = removeSecrets(ctx, dockerCli, secrets) || hasError
|
||||||
hasError = removeConfigs(ctx, dockerCli, configs) || hasError
|
hasError = removeConfigs(ctx, dockerCli, configs) || hasError
|
||||||
hasError = removeNetworks(ctx, dockerCli, networks) || hasError
|
hasError = removeNetworks(ctx, dockerCli, networks) || hasError
|
||||||
|
|
||||||
if 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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if !opts.Detach {
|
if !opts.Detach {
|
||||||
err = waitOnTasks(ctx, apiClient, namespace)
|
err = waitOnTasks(ctx, apiClient, namespace)
|
||||||
if err != nil {
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return errors.Join(errs...)
|
||||||
if len(errs) > 0 {
|
|
||||||
return errors.New(strings.Join(errs, "\n"))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func sortServiceByName(services []swarm.Service) func(i, j int) bool {
|
func sortServiceByName(services []swarm.Service) func(i, j int) bool {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user