cli/command/swarm: use stdlib errors

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-09-08 20:22:42 +02:00
parent cd583313ee
commit 0e4934d36c
10 changed files with 20 additions and 23 deletions

View File

@ -2,6 +2,7 @@ package swarm
import (
"context"
"errors"
"fmt"
"io"
"strings"
@ -12,7 +13,6 @@ import (
"github.com/docker/cli/internal/jsonstream"
"github.com/moby/moby/api/types/swarm"
"github.com/moby/moby/client"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
@ -138,7 +138,7 @@ func attach(ctx context.Context, dockerCLI command.Cli, opts caOptions) error {
func displayTrustRoot(out io.Writer, info swarm.Swarm) error {
if info.ClusterInfo.TLSInfo.TrustRoot == "" {
return errors.New("No CA information available")
return errors.New("no CA information available")
}
_, _ = fmt.Fprintln(out, strings.TrimSpace(info.ClusterInfo.TLSInfo.TrustRoot))
return nil

View File

@ -57,7 +57,7 @@ func swarmSpecWithFullCAConfig() *swarm.Spec {
func TestDisplayTrustRootNoRoot(t *testing.T) {
buffer := new(bytes.Buffer)
err := displayTrustRoot(buffer, swarm.Swarm{})
assert.Error(t, err, "No CA information available")
assert.Error(t, err, "no CA information available")
}
type invalidCATestCases struct {

View File

@ -9,7 +9,6 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/moby/moby/api/types/swarm"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
@ -89,14 +88,14 @@ func runInit(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, o
case swarm.NodeAvailabilityActive, swarm.NodeAvailabilityPause, swarm.NodeAvailabilityDrain:
req.Availability = availability
default:
return errors.Errorf("invalid availability %q, only active, pause and drain are supported", opts.availability)
return fmt.Errorf("invalid availability %q, only active, pause and drain are supported", opts.availability)
}
}
nodeID, err := apiClient.SwarmInit(ctx, req)
if err != nil {
if strings.Contains(err.Error(), "could not choose an IP address to advertise") || strings.Contains(err.Error(), "could not find the system's IP address") {
return errors.New(err.Error() + " - specify one with --advertise-addr")
return fmt.Errorf("%w - specify one with --advertise-addr", err)
}
return err
}
@ -112,7 +111,7 @@ func runInit(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, o
if req.AutoLockManagers {
unlockKeyResp, err := apiClient.SwarmGetUnlockKey(ctx)
if err != nil {
return errors.Wrap(err, "could not fetch unlock key")
return fmt.Errorf("could not fetch unlock key: %w", err)
}
printUnlockCommand(dockerCLI.Out(), unlockKeyResp.UnlockKey)
}

View File

@ -8,7 +8,6 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/moby/moby/api/types/swarm"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
@ -69,7 +68,7 @@ func runJoin(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, o
case swarm.NodeAvailabilityActive, swarm.NodeAvailabilityPause, swarm.NodeAvailabilityDrain:
req.Availability = availability
default:
return errors.Errorf("invalid availability %q, only active, pause and drain are supported", opts.availability)
return fmt.Errorf("invalid availability %q, only active, pause and drain are supported", opts.availability)
}
}

View File

@ -2,12 +2,12 @@ package swarm
import (
"context"
"errors"
"fmt"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/moby/moby/client"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

View File

@ -3,6 +3,7 @@ package swarm
import (
"encoding/csv"
"encoding/pem"
"errors"
"fmt"
"os"
"strings"
@ -10,7 +11,6 @@ import (
"github.com/docker/cli/opts"
"github.com/moby/moby/api/types/swarm"
"github.com/pkg/errors"
"github.com/spf13/pflag"
)
@ -177,7 +177,7 @@ func parseExternalCA(caSpec string) (*swarm.ExternalCA, error) {
for _, field := range fields {
key, value, ok := strings.Cut(field, "=")
if !ok {
return nil, errors.Errorf("invalid field '%s' must be a key=value pair", field)
return nil, fmt.Errorf("invalid field '%s' must be a key=value pair", field)
}
// TODO(thaJeztah): these options should not be case-insensitive.
@ -187,7 +187,7 @@ func parseExternalCA(caSpec string) (*swarm.ExternalCA, error) {
if strings.ToLower(value) == string(swarm.ExternalCAProtocolCFSSL) {
externalCA.Protocol = swarm.ExternalCAProtocolCFSSL
} else {
return nil, errors.Errorf("unrecognized external CA protocol %s", value)
return nil, fmt.Errorf("unrecognized external CA protocol %s", value)
}
case "url":
hasURL = true
@ -195,7 +195,7 @@ func parseExternalCA(caSpec string) (*swarm.ExternalCA, error) {
case "cacert":
cacontents, err := os.ReadFile(value)
if err != nil {
return nil, errors.Wrap(err, "unable to read CA cert for external CA")
return nil, fmt.Errorf("unable to read CA cert for external CA: %w", err)
}
if pemBlock, _ := pem.Decode(cacontents); pemBlock == nil {
return nil, errors.New("CA cert for external CA must be in PEM format")

View File

@ -3,6 +3,7 @@ package swarm
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"strings"
@ -11,7 +12,6 @@ import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/streams"
"github.com/moby/moby/api/types/swarm"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"golang.org/x/term"
)
@ -47,11 +47,11 @@ func runUnlock(ctx context.Context, dockerCLI command.Cli) error {
switch info.Swarm.LocalNodeState {
case swarm.LocalNodeStateInactive:
return errors.New("Error: This node is not part of a swarm")
return errors.New("error: this node is not part of a swarm")
case swarm.LocalNodeStateLocked:
break
case swarm.LocalNodeStatePending, swarm.LocalNodeStateActive, swarm.LocalNodeStateError:
return errors.New("Error: swarm is not locked")
return errors.New("error: swarm is not locked")
}
key, err := readKey(dockerCLI.In(), "Enter unlock key: ")

View File

@ -2,13 +2,13 @@ package swarm
import (
"context"
"errors"
"fmt"
"io"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/moby/moby/client"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
@ -68,7 +68,7 @@ func runUnlockKey(ctx context.Context, dockerCLI command.Cli, opts unlockKeyOpti
unlockKeyResp, err := apiClient.SwarmGetUnlockKey(ctx)
if err != nil {
return errors.Wrap(err, "could not fetch unlock key")
return fmt.Errorf("could not fetch unlock key: %w", err)
}
if unlockKeyResp.UnlockKey == "" {

View File

@ -35,7 +35,7 @@ func TestSwarmUnlockErrors(t *testing.T) {
},
}, nil
},
expectedError: "This node is not part of a swarm",
expectedError: "this node is not part of a swarm",
},
{
name: "is-not-locked",
@ -46,7 +46,7 @@ func TestSwarmUnlockErrors(t *testing.T) {
},
}, nil
},
expectedError: "Error: swarm is not locked",
expectedError: "error: swarm is not locked",
},
{
name: "unlockrequest-failed",

View File

@ -7,7 +7,6 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/moby/moby/client"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
@ -65,7 +64,7 @@ func runUpdate(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet,
if curAutoLock && !prevAutoLock {
unlockKeyResp, err := apiClient.SwarmGetUnlockKey(ctx)
if err != nil {
return errors.Wrap(err, "could not fetch unlock key")
return fmt.Errorf("could not fetch unlock key: %w", err)
}
printUnlockCommand(dockerCLI.Out(), unlockKeyResp.UnlockKey)
}