Skip to content

Commit 981e75e

Browse files
committed
cli/command/network: use stdlib errors, remove errdefs uses
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 3382ee3 commit 981e75e

3 files changed

Lines changed: 25 additions & 15 deletions

File tree

cli/command/network/create.go

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

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"net"
@@ -13,7 +14,6 @@ import (
1314
"github.com/docker/cli/opts"
1415
"github.com/docker/docker/api/types/network"
1516
"github.com/docker/docker/client"
16-
"github.com/pkg/errors"
1717
"github.com/spf13/cobra"
1818
)
1919

@@ -143,7 +143,7 @@ func runCreate(ctx context.Context, apiClient client.NetworkAPIClient, output io
143143
//nolint:gocyclo
144144
func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
145145
if len(options.subnets) < len(options.ipRanges) || len(options.subnets) < len(options.gateways) {
146-
return nil, errors.Errorf("every ip-range or gateway must have a corresponding subnet")
146+
return nil, errors.New("every ip-range or gateway must have a corresponding subnet")
147147
}
148148
iData := map[string]*network.IPAMConfig{}
149149

@@ -159,7 +159,7 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
159159
return nil, err
160160
}
161161
if ok1 || ok2 {
162-
return nil, errors.Errorf("multiple overlapping subnet configuration is not supported")
162+
return nil, errors.New("multiple overlapping subnet configuration is not supported")
163163
}
164164
}
165165
iData[s] = &network.IPAMConfig{Subnet: s, AuxAddress: map[string]string{}}
@@ -180,14 +180,14 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
180180
continue
181181
}
182182
if iData[s].IPRange != "" {
183-
return nil, errors.Errorf("cannot configure multiple ranges (%s, %s) on the same subnet (%s)", r, iData[s].IPRange, s)
183+
return nil, fmt.Errorf("cannot configure multiple ranges (%s, %s) on the same subnet (%s)", r, iData[s].IPRange, s)
184184
}
185185
d := iData[s]
186186
d.IPRange = r
187187
match = true
188188
}
189189
if !match {
190-
return nil, errors.Errorf("no matching subnet for range %s", r)
190+
return nil, fmt.Errorf("no matching subnet for range %s", r)
191191
}
192192
}
193193

@@ -203,14 +203,14 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
203203
continue
204204
}
205205
if iData[s].Gateway != "" {
206-
return nil, errors.Errorf("cannot configure multiple gateways (%s, %s) for the same subnet (%s)", g, iData[s].Gateway, s)
206+
return nil, fmt.Errorf("cannot configure multiple gateways (%s, %s) for the same subnet (%s)", g, iData[s].Gateway, s)
207207
}
208208
d := iData[s]
209209
d.Gateway = g
210210
match = true
211211
}
212212
if !match {
213-
return nil, errors.Errorf("no matching subnet for gateway %s", g)
213+
return nil, fmt.Errorf("no matching subnet for gateway %s", g)
214214
}
215215
}
216216

@@ -229,7 +229,7 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
229229
match = true
230230
}
231231
if !match {
232-
return nil, errors.Errorf("no matching subnet for aux-address %s", aa)
232+
return nil, fmt.Errorf("no matching subnet for aux-address %s", aa)
233233
}
234234
}
235235

@@ -250,7 +250,7 @@ func subnetMatches(subnet, data string) (bool, error) {
250250

251251
_, s, err := net.ParseCIDR(subnet)
252252
if err != nil {
253-
return false, errors.Wrap(err, "invalid subnet")
253+
return false, fmt.Errorf("invalid subnet: %w", err)
254254
}
255255

256256
if strings.Contains(data, "/") {

cli/command/network/prune.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package network
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/docker/cli/internal/prompt"
1011
"github.com/docker/cli/opts"
11-
"github.com/docker/docker/errdefs"
12-
"github.com/pkg/errors"
1312
"github.com/spf13/cobra"
1413
)
1514

@@ -58,7 +57,7 @@ func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions)
5857
return "", err
5958
}
6059
if !r {
61-
return "", errdefs.Cancelled(errors.New("network prune has been cancelled"))
60+
return "", cancelledErr{errors.New("network prune has been cancelled")}
6261
}
6362
}
6463

@@ -77,6 +76,10 @@ func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions)
7776
return output, nil
7877
}
7978

79+
type cancelledErr struct{ error }
80+
81+
func (cancelledErr) Cancelled() {}
82+
8083
// RunPrune calls the Network Prune API
8184
// This returns the amount of space reclaimed and a detailed output string
8285
func RunPrune(ctx context.Context, dockerCli command.Cli, _ bool, filter opts.FilterOpt) (uint64, string, error) {

cli/command/network/remove_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ import (
88

99
"github.com/docker/cli/internal/test"
1010
"github.com/docker/docker/api/types/network"
11-
"github.com/docker/docker/errdefs"
1211
"gotest.tools/v3/assert"
1312
is "gotest.tools/v3/assert/cmp"
1413
)
1514

15+
type forBiddenErr struct{ error }
16+
17+
func (forBiddenErr) Forbidden() {}
18+
19+
type notFoundErr struct{ error }
20+
21+
func (notFoundErr) NotFound() {}
22+
1623
func TestNetworkRemoveForce(t *testing.T) {
1724
tests := []struct {
1825
doc string
@@ -68,9 +75,9 @@ func TestNetworkRemoveForce(t *testing.T) {
6875
networkRemoveFunc: func(ctx context.Context, networkID string) error {
6976
switch networkID {
7077
case "no-such-network":
71-
return errdefs.NotFound(errors.New("no such network: no-such-network"))
78+
return notFoundErr{errors.New("no such network: no-such-network")}
7279
case "in-use-network":
73-
return errdefs.Forbidden(errors.New("network is in use"))
80+
return forBiddenErr{errors.New("network is in use")}
7481
case "existing-network":
7582
return nil
7683
default:

0 commit comments

Comments
 (0)