cli/command/network: use stdlib errors, remove errdefs uses

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2025-05-16 19:48:17 +02:00
parent 3382ee3e99
commit 981e75e0f4
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
3 changed files with 25 additions and 15 deletions

View File

@ -2,6 +2,7 @@ package network
import (
"context"
"errors"
"fmt"
"io"
"net"
@ -13,7 +14,6 @@ import (
"github.com/docker/cli/opts"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
@ -143,7 +143,7 @@ func runCreate(ctx context.Context, apiClient client.NetworkAPIClient, output io
//nolint:gocyclo
func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
if len(options.subnets) < len(options.ipRanges) || len(options.subnets) < len(options.gateways) {
return nil, errors.Errorf("every ip-range or gateway must have a corresponding subnet")
return nil, errors.New("every ip-range or gateway must have a corresponding subnet")
}
iData := map[string]*network.IPAMConfig{}
@ -159,7 +159,7 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
return nil, err
}
if ok1 || ok2 {
return nil, errors.Errorf("multiple overlapping subnet configuration is not supported")
return nil, errors.New("multiple overlapping subnet configuration is not supported")
}
}
iData[s] = &network.IPAMConfig{Subnet: s, AuxAddress: map[string]string{}}
@ -180,14 +180,14 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
continue
}
if iData[s].IPRange != "" {
return nil, errors.Errorf("cannot configure multiple ranges (%s, %s) on the same subnet (%s)", r, iData[s].IPRange, s)
return nil, fmt.Errorf("cannot configure multiple ranges (%s, %s) on the same subnet (%s)", r, iData[s].IPRange, s)
}
d := iData[s]
d.IPRange = r
match = true
}
if !match {
return nil, errors.Errorf("no matching subnet for range %s", r)
return nil, fmt.Errorf("no matching subnet for range %s", r)
}
}
@ -203,14 +203,14 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
continue
}
if iData[s].Gateway != "" {
return nil, errors.Errorf("cannot configure multiple gateways (%s, %s) for the same subnet (%s)", g, iData[s].Gateway, s)
return nil, fmt.Errorf("cannot configure multiple gateways (%s, %s) for the same subnet (%s)", g, iData[s].Gateway, s)
}
d := iData[s]
d.Gateway = g
match = true
}
if !match {
return nil, errors.Errorf("no matching subnet for gateway %s", g)
return nil, fmt.Errorf("no matching subnet for gateway %s", g)
}
}
@ -229,7 +229,7 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
match = true
}
if !match {
return nil, errors.Errorf("no matching subnet for aux-address %s", aa)
return nil, fmt.Errorf("no matching subnet for aux-address %s", aa)
}
}
@ -250,7 +250,7 @@ func subnetMatches(subnet, data string) (bool, error) {
_, s, err := net.ParseCIDR(subnet)
if err != nil {
return false, errors.Wrap(err, "invalid subnet")
return false, fmt.Errorf("invalid subnet: %w", err)
}
if strings.Contains(data, "/") {

View File

@ -2,14 +2,13 @@ package network
import (
"context"
"errors"
"fmt"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/internal/prompt"
"github.com/docker/cli/opts"
"github.com/docker/docker/errdefs"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
@ -58,7 +57,7 @@ func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions)
return "", err
}
if !r {
return "", errdefs.Cancelled(errors.New("network prune has been cancelled"))
return "", cancelledErr{errors.New("network prune has been cancelled")}
}
}
@ -77,6 +76,10 @@ func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions)
return output, nil
}
type cancelledErr struct{ error }
func (cancelledErr) Cancelled() {}
// RunPrune calls the Network Prune API
// This returns the amount of space reclaimed and a detailed output string
func RunPrune(ctx context.Context, dockerCli command.Cli, _ bool, filter opts.FilterOpt) (uint64, string, error) {

View File

@ -8,11 +8,18 @@ import (
"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/errdefs"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
type forBiddenErr struct{ error }
func (forBiddenErr) Forbidden() {}
type notFoundErr struct{ error }
func (notFoundErr) NotFound() {}
func TestNetworkRemoveForce(t *testing.T) {
tests := []struct {
doc string
@ -68,9 +75,9 @@ func TestNetworkRemoveForce(t *testing.T) {
networkRemoveFunc: func(ctx context.Context, networkID string) error {
switch networkID {
case "no-such-network":
return errdefs.NotFound(errors.New("no such network: no-such-network"))
return notFoundErr{errors.New("no such network: no-such-network")}
case "in-use-network":
return errdefs.Forbidden(errors.New("network is in use"))
return forBiddenErr{errors.New("network is in use")}
case "existing-network":
return nil
default: