Skip to content

Commit 0e4934d

Browse files
committed
cli/command/swarm: use stdlib errors
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent cd58331 commit 0e4934d

File tree

10 files changed

+20
-23
lines changed

10 files changed

+20
-23
lines changed

cli/command/swarm/ca.go

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

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"strings"
@@ -12,7 +13,6 @@ import (
1213
"github.com/docker/cli/internal/jsonstream"
1314
"github.com/moby/moby/api/types/swarm"
1415
"github.com/moby/moby/client"
15-
"github.com/pkg/errors"
1616
"github.com/spf13/cobra"
1717
"github.com/spf13/pflag"
1818
)
@@ -138,7 +138,7 @@ func attach(ctx context.Context, dockerCLI command.Cli, opts caOptions) error {
138138

139139
func displayTrustRoot(out io.Writer, info swarm.Swarm) error {
140140
if info.ClusterInfo.TLSInfo.TrustRoot == "" {
141-
return errors.New("No CA information available")
141+
return errors.New("no CA information available")
142142
}
143143
_, _ = fmt.Fprintln(out, strings.TrimSpace(info.ClusterInfo.TLSInfo.TrustRoot))
144144
return nil

cli/command/swarm/ca_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func swarmSpecWithFullCAConfig() *swarm.Spec {
5757
func TestDisplayTrustRootNoRoot(t *testing.T) {
5858
buffer := new(bytes.Buffer)
5959
err := displayTrustRoot(buffer, swarm.Swarm{})
60-
assert.Error(t, err, "No CA information available")
60+
assert.Error(t, err, "no CA information available")
6161
}
6262

6363
type invalidCATestCases struct {

cli/command/swarm/init.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/docker/cli/cli"
1010
"github.com/docker/cli/cli/command"
1111
"github.com/moby/moby/api/types/swarm"
12-
"github.com/pkg/errors"
1312
"github.com/spf13/cobra"
1413
"github.com/spf13/pflag"
1514
)
@@ -89,14 +88,14 @@ func runInit(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, o
8988
case swarm.NodeAvailabilityActive, swarm.NodeAvailabilityPause, swarm.NodeAvailabilityDrain:
9089
req.Availability = availability
9190
default:
92-
return errors.Errorf("invalid availability %q, only active, pause and drain are supported", opts.availability)
91+
return fmt.Errorf("invalid availability %q, only active, pause and drain are supported", opts.availability)
9392
}
9493
}
9594

9695
nodeID, err := apiClient.SwarmInit(ctx, req)
9796
if err != nil {
9897
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") {
99-
return errors.New(err.Error() + " - specify one with --advertise-addr")
98+
return fmt.Errorf("%w - specify one with --advertise-addr", err)
10099
}
101100
return err
102101
}
@@ -112,7 +111,7 @@ func runInit(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, o
112111
if req.AutoLockManagers {
113112
unlockKeyResp, err := apiClient.SwarmGetUnlockKey(ctx)
114113
if err != nil {
115-
return errors.Wrap(err, "could not fetch unlock key")
114+
return fmt.Errorf("could not fetch unlock key: %w", err)
116115
}
117116
printUnlockCommand(dockerCLI.Out(), unlockKeyResp.UnlockKey)
118117
}

cli/command/swarm/join.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/docker/cli/cli"
99
"github.com/docker/cli/cli/command"
1010
"github.com/moby/moby/api/types/swarm"
11-
"github.com/pkg/errors"
1211
"github.com/spf13/cobra"
1312
"github.com/spf13/pflag"
1413
)
@@ -69,7 +68,7 @@ func runJoin(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, o
6968
case swarm.NodeAvailabilityActive, swarm.NodeAvailabilityPause, swarm.NodeAvailabilityDrain:
7069
req.Availability = availability
7170
default:
72-
return errors.Errorf("invalid availability %q, only active, pause and drain are supported", opts.availability)
71+
return fmt.Errorf("invalid availability %q, only active, pause and drain are supported", opts.availability)
7372
}
7473
}
7574

cli/command/swarm/join_token.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package swarm
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

78
"github.com/docker/cli/cli"
89
"github.com/docker/cli/cli/command"
910
"github.com/moby/moby/client"
10-
"github.com/pkg/errors"
1111
"github.com/spf13/cobra"
1212
)
1313

cli/command/swarm/opts.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package swarm
33
import (
44
"encoding/csv"
55
"encoding/pem"
6+
"errors"
67
"fmt"
78
"os"
89
"strings"
910
"time"
1011

1112
"github.com/docker/cli/opts"
1213
"github.com/moby/moby/api/types/swarm"
13-
"github.com/pkg/errors"
1414
"github.com/spf13/pflag"
1515
)
1616

@@ -177,7 +177,7 @@ func parseExternalCA(caSpec string) (*swarm.ExternalCA, error) {
177177
for _, field := range fields {
178178
key, value, ok := strings.Cut(field, "=")
179179
if !ok {
180-
return nil, errors.Errorf("invalid field '%s' must be a key=value pair", field)
180+
return nil, fmt.Errorf("invalid field '%s' must be a key=value pair", field)
181181
}
182182

183183
// TODO(thaJeztah): these options should not be case-insensitive.
@@ -187,15 +187,15 @@ func parseExternalCA(caSpec string) (*swarm.ExternalCA, error) {
187187
if strings.ToLower(value) == string(swarm.ExternalCAProtocolCFSSL) {
188188
externalCA.Protocol = swarm.ExternalCAProtocolCFSSL
189189
} else {
190-
return nil, errors.Errorf("unrecognized external CA protocol %s", value)
190+
return nil, fmt.Errorf("unrecognized external CA protocol %s", value)
191191
}
192192
case "url":
193193
hasURL = true
194194
externalCA.URL = value
195195
case "cacert":
196196
cacontents, err := os.ReadFile(value)
197197
if err != nil {
198-
return nil, errors.Wrap(err, "unable to read CA cert for external CA")
198+
return nil, fmt.Errorf("unable to read CA cert for external CA: %w", err)
199199
}
200200
if pemBlock, _ := pem.Decode(cacontents); pemBlock == nil {
201201
return nil, errors.New("CA cert for external CA must be in PEM format")

cli/command/swarm/unlock.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package swarm
33
import (
44
"bufio"
55
"context"
6+
"errors"
67
"fmt"
78
"io"
89
"strings"
@@ -11,7 +12,6 @@ import (
1112
"github.com/docker/cli/cli/command"
1213
"github.com/docker/cli/cli/streams"
1314
"github.com/moby/moby/api/types/swarm"
14-
"github.com/pkg/errors"
1515
"github.com/spf13/cobra"
1616
"golang.org/x/term"
1717
)
@@ -47,11 +47,11 @@ func runUnlock(ctx context.Context, dockerCLI command.Cli) error {
4747

4848
switch info.Swarm.LocalNodeState {
4949
case swarm.LocalNodeStateInactive:
50-
return errors.New("Error: This node is not part of a swarm")
50+
return errors.New("error: this node is not part of a swarm")
5151
case swarm.LocalNodeStateLocked:
5252
break
5353
case swarm.LocalNodeStatePending, swarm.LocalNodeStateActive, swarm.LocalNodeStateError:
54-
return errors.New("Error: swarm is not locked")
54+
return errors.New("error: swarm is not locked")
5555
}
5656

5757
key, err := readKey(dockerCLI.In(), "Enter unlock key: ")

cli/command/swarm/unlock_key.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package swarm
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78

89
"github.com/docker/cli/cli"
910
"github.com/docker/cli/cli/command"
1011
"github.com/moby/moby/client"
11-
"github.com/pkg/errors"
1212
"github.com/spf13/cobra"
1313
)
1414

@@ -68,7 +68,7 @@ func runUnlockKey(ctx context.Context, dockerCLI command.Cli, opts unlockKeyOpti
6868

6969
unlockKeyResp, err := apiClient.SwarmGetUnlockKey(ctx)
7070
if err != nil {
71-
return errors.Wrap(err, "could not fetch unlock key")
71+
return fmt.Errorf("could not fetch unlock key: %w", err)
7272
}
7373

7474
if unlockKeyResp.UnlockKey == "" {

cli/command/swarm/unlock_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestSwarmUnlockErrors(t *testing.T) {
3535
},
3636
}, nil
3737
},
38-
expectedError: "This node is not part of a swarm",
38+
expectedError: "this node is not part of a swarm",
3939
},
4040
{
4141
name: "is-not-locked",
@@ -46,7 +46,7 @@ func TestSwarmUnlockErrors(t *testing.T) {
4646
},
4747
}, nil
4848
},
49-
expectedError: "Error: swarm is not locked",
49+
expectedError: "error: swarm is not locked",
5050
},
5151
{
5252
name: "unlockrequest-failed",

cli/command/swarm/update.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/docker/cli/cli"
88
"github.com/docker/cli/cli/command"
99
"github.com/moby/moby/client"
10-
"github.com/pkg/errors"
1110
"github.com/spf13/cobra"
1211
"github.com/spf13/pflag"
1312
)
@@ -65,7 +64,7 @@ func runUpdate(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet,
6564
if curAutoLock && !prevAutoLock {
6665
unlockKeyResp, err := apiClient.SwarmGetUnlockKey(ctx)
6766
if err != nil {
68-
return errors.Wrap(err, "could not fetch unlock key")
67+
return fmt.Errorf("could not fetch unlock key: %w", err)
6968
}
7069
printUnlockCommand(dockerCLI.Out(), unlockKeyResp.UnlockKey)
7170
}

0 commit comments

Comments
 (0)