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 ( import (
"context" "context"
"errors"
"fmt" "fmt"
"io" "io"
"net" "net"
@ -13,7 +14,6 @@ import (
"github.com/docker/cli/opts" "github.com/docker/cli/opts"
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -143,7 +143,7 @@ func runCreate(ctx context.Context, apiClient client.NetworkAPIClient, output io
//nolint:gocyclo //nolint:gocyclo
func createIPAMConfig(options ipamOptions) (*network.IPAM, error) { func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
if len(options.subnets) < len(options.ipRanges) || len(options.subnets) < len(options.gateways) { 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{} iData := map[string]*network.IPAMConfig{}
@ -159,7 +159,7 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
return nil, err return nil, err
} }
if ok1 || ok2 { 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{}} iData[s] = &network.IPAMConfig{Subnet: s, AuxAddress: map[string]string{}}
@ -180,14 +180,14 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
continue continue
} }
if iData[s].IPRange != "" { 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 := iData[s]
d.IPRange = r d.IPRange = r
match = true match = true
} }
if !match { 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 continue
} }
if iData[s].Gateway != "" { 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 := iData[s]
d.Gateway = g d.Gateway = g
match = true match = true
} }
if !match { 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 match = true
} }
if !match { 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) _, s, err := net.ParseCIDR(subnet)
if err != nil { if err != nil {
return false, errors.Wrap(err, "invalid subnet") return false, fmt.Errorf("invalid subnet: %w", err)
} }
if strings.Contains(data, "/") { if strings.Contains(data, "/") {

View File

@ -2,14 +2,13 @@ package network
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/cli/internal/prompt" "github.com/docker/cli/internal/prompt"
"github.com/docker/cli/opts" "github.com/docker/cli/opts"
"github.com/docker/docker/errdefs"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -58,7 +57,7 @@ func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions)
return "", err return "", err
} }
if !r { 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 return output, nil
} }
type cancelledErr struct{ error }
func (cancelledErr) Cancelled() {}
// RunPrune calls the Network Prune API // RunPrune calls the Network Prune API
// This returns the amount of space reclaimed and a detailed output string // 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) { 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/cli/internal/test"
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
"github.com/docker/docker/errdefs"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp" 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) { func TestNetworkRemoveForce(t *testing.T) {
tests := []struct { tests := []struct {
doc string doc string
@ -68,9 +75,9 @@ func TestNetworkRemoveForce(t *testing.T) {
networkRemoveFunc: func(ctx context.Context, networkID string) error { networkRemoveFunc: func(ctx context.Context, networkID string) error {
switch networkID { switch networkID {
case "no-such-network": 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": case "in-use-network":
return errdefs.Forbidden(errors.New("network is in use")) return forBiddenErr{errors.New("network is in use")}
case "existing-network": case "existing-network":
return nil return nil
default: default: