Skip to content

Commit 0674a00

Browse files
authored
Merge pull request #6079 from thaJeztah/less_errdefs
remove uses of github.com/docker/docker/errdefs
2 parents eee0cf9 + 1058b22 commit 0674a00

File tree

28 files changed

+202
-96
lines changed

28 files changed

+202
-96
lines changed

cli/command/builder/prune.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import (
1212
"github.com/docker/cli/internal/prompt"
1313
"github.com/docker/cli/opts"
1414
"github.com/docker/docker/api/types/build"
15-
"github.com/docker/docker/errdefs"
16-
units "github.com/docker/go-units"
15+
"github.com/docker/go-units"
1716
"github.com/spf13/cobra"
1817
)
1918

@@ -75,7 +74,7 @@ func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions)
7574
return 0, "", err
7675
}
7776
if !r {
78-
return 0, "", errdefs.Cancelled(errors.New("builder prune has been cancelled"))
77+
return 0, "", cancelledErr{errors.New("builder prune has been cancelled")}
7978
}
8079
}
8180

@@ -101,6 +100,10 @@ func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions)
101100
return report.SpaceReclaimed, output, nil
102101
}
103102

103+
type cancelledErr struct{ error }
104+
105+
func (cancelledErr) Cancelled() {}
106+
104107
// CachePrune executes a prune command for build cache
105108
func CachePrune(ctx context.Context, dockerCli command.Cli, all bool, filter opts.FilterOpt) (uint64, string, error) {
106109
return runPrune(ctx, dockerCli, pruneOptions{force: true, all: all, filter: filter})

cli/command/cli_options.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/docker/cli/cli/streams"
1313
"github.com/docker/docker/client"
14-
"github.com/docker/docker/errdefs"
1514
"github.com/moby/term"
1615
"github.com/pkg/errors"
1716
)
@@ -178,7 +177,7 @@ func withCustomHeadersFromEnv() client.Opt {
178177
csvReader := csv.NewReader(strings.NewReader(value))
179178
fields, err := csvReader.Read()
180179
if err != nil {
181-
return errdefs.InvalidParameter(errors.Errorf(
180+
return invalidParameter(errors.Errorf(
182181
"failed to parse custom headers from %s environment variable: value must be formatted as comma-separated key=value pairs",
183182
envOverrideHTTPHeaders,
184183
))
@@ -195,7 +194,7 @@ func withCustomHeadersFromEnv() client.Opt {
195194
k = strings.TrimSpace(k)
196195

197196
if k == "" {
198-
return errdefs.InvalidParameter(errors.Errorf(
197+
return invalidParameter(errors.Errorf(
199198
`failed to set custom headers from %s environment variable: value contains a key=value pair with an empty key: '%s'`,
200199
envOverrideHTTPHeaders, kv,
201200
))
@@ -206,7 +205,7 @@ func withCustomHeadersFromEnv() client.Opt {
206205
// from an environment variable with the same name). In the meantime,
207206
// produce an error to prevent users from depending on this.
208207
if !hasValue {
209-
return errdefs.InvalidParameter(errors.Errorf(
208+
return invalidParameter(errors.Errorf(
210209
`failed to set custom headers from %s environment variable: missing "=" in key=value pair: '%s'`,
211210
envOverrideHTTPHeaders, kv,
212211
))

cli/command/container/create.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/docker/docker/api/types/mount"
3030
"github.com/docker/docker/api/types/versions"
3131
"github.com/docker/docker/client"
32-
"github.com/docker/docker/errdefs"
3332
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3433
"github.com/pkg/errors"
3534
"github.com/spf13/cobra"
@@ -317,7 +316,7 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
317316
if options.platform != "" && versions.GreaterThanOrEqualTo(dockerCli.Client().ClientVersion(), "1.41") {
318317
p, err := platforms.Parse(options.platform)
319318
if err != nil {
320-
return "", errors.Wrap(errdefs.InvalidParameter(err), "error parsing specified platform")
319+
return "", errors.Wrap(invalidParameter(err), "error parsing specified platform")
321320
}
322321
platform = &p
323322
}

cli/command/container/errors.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package container
2+
3+
import cerrdefs "github.com/containerd/errdefs"
4+
5+
func invalidParameter(err error) error {
6+
if err == nil || cerrdefs.IsInvalidArgument(err) {
7+
return err
8+
}
9+
return invalidParameterErr{err}
10+
}
11+
12+
type invalidParameterErr struct{ error }
13+
14+
func (invalidParameterErr) InvalidParameter() {}
15+
func (e invalidParameterErr) Unwrap() error {
16+
return e.error
17+
}
18+
19+
func notFound(err error) error {
20+
if err == nil || cerrdefs.IsNotFound(err) {
21+
return err
22+
}
23+
return notFoundErr{err}
24+
}
25+
26+
type notFoundErr struct{ error }
27+
28+
func (notFoundErr) NotFound() {}
29+
func (e notFoundErr) Unwrap() error {
30+
return e.error
31+
}

cli/command/container/opts.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
mounttypes "github.com/docker/docker/api/types/mount"
2020
networktypes "github.com/docker/docker/api/types/network"
2121
"github.com/docker/docker/api/types/strslice"
22-
"github.com/docker/docker/errdefs"
2322
"github.com/docker/go-connections/nat"
2423
"github.com/pkg/errors"
2524
"github.com/spf13/pflag"
@@ -781,7 +780,7 @@ func parseNetworkOpts(copts *containerOptions) (map[string]*networktypes.Endpoin
781780
return nil, err
782781
}
783782
if _, ok := endpoints[n.Target]; ok {
784-
return nil, errdefs.InvalidParameter(errors.Errorf("network %q is specified multiple times", n.Target))
783+
return nil, invalidParameter(errors.Errorf("network %q is specified multiple times", n.Target))
785784
}
786785

787786
// For backward compatibility: if no custom options are provided for the network,
@@ -795,30 +794,30 @@ func parseNetworkOpts(copts *containerOptions) (map[string]*networktypes.Endpoin
795794
endpoints[n.Target] = ep
796795
}
797796
if hasUserDefined && hasNonUserDefined {
798-
return nil, errdefs.InvalidParameter(errors.New("conflicting options: cannot attach both user-defined and non-user-defined network-modes"))
797+
return nil, invalidParameter(errors.New("conflicting options: cannot attach both user-defined and non-user-defined network-modes"))
799798
}
800799
return endpoints, nil
801800
}
802801

803802
func applyContainerOptions(n *opts.NetworkAttachmentOpts, copts *containerOptions) error { //nolint:gocyclo
804803
// TODO should we error if _any_ advanced option is used? (i.e. forbid to combine advanced notation with the "old" flags (`--network-alias`, `--link`, `--ip`, `--ip6`)?
805804
if len(n.Aliases) > 0 && copts.aliases.Len() > 0 {
806-
return errdefs.InvalidParameter(errors.New("conflicting options: cannot specify both --network-alias and per-network alias"))
805+
return invalidParameter(errors.New("conflicting options: cannot specify both --network-alias and per-network alias"))
807806
}
808807
if len(n.Links) > 0 && copts.links.Len() > 0 {
809-
return errdefs.InvalidParameter(errors.New("conflicting options: cannot specify both --link and per-network links"))
808+
return invalidParameter(errors.New("conflicting options: cannot specify both --link and per-network links"))
810809
}
811810
if n.IPv4Address != "" && copts.ipv4Address != "" {
812-
return errdefs.InvalidParameter(errors.New("conflicting options: cannot specify both --ip and per-network IPv4 address"))
811+
return invalidParameter(errors.New("conflicting options: cannot specify both --ip and per-network IPv4 address"))
813812
}
814813
if n.IPv6Address != "" && copts.ipv6Address != "" {
815-
return errdefs.InvalidParameter(errors.New("conflicting options: cannot specify both --ip6 and per-network IPv6 address"))
814+
return invalidParameter(errors.New("conflicting options: cannot specify both --ip6 and per-network IPv6 address"))
816815
}
817816
if n.MacAddress != "" && copts.macAddress != "" {
818-
return errdefs.InvalidParameter(errors.New("conflicting options: cannot specify both --mac-address and per-network MAC address"))
817+
return invalidParameter(errors.New("conflicting options: cannot specify both --mac-address and per-network MAC address"))
819818
}
820819
if len(n.LinkLocalIPs) > 0 && copts.linkLocalIPs.Len() > 0 {
821-
return errdefs.InvalidParameter(errors.New("conflicting options: cannot specify both --link-local-ip and per-network link-local IP addresses"))
820+
return invalidParameter(errors.New("conflicting options: cannot specify both --link-local-ip and per-network link-local IP addresses"))
822821
}
823822
if copts.aliases.Len() > 0 {
824823
n.Aliases = make([]string, copts.aliases.Len())

cli/command/container/prune.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/docker/cli/cli/command/completion"
1010
"github.com/docker/cli/internal/prompt"
1111
"github.com/docker/cli/opts"
12-
"github.com/docker/docker/errdefs"
1312
units "github.com/docker/go-units"
1413
"github.com/pkg/errors"
1514
"github.com/spf13/cobra"
@@ -62,7 +61,7 @@ func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions)
6261
return 0, "", err
6362
}
6463
if !r {
65-
return 0, "", errdefs.Cancelled(errors.New("container prune has been cancelled"))
64+
return 0, "", cancelledErr{errors.New("container prune has been cancelled")}
6665
}
6766
}
6867

@@ -82,6 +81,10 @@ func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions)
8281
return spaceReclaimed, output, nil
8382
}
8483

84+
type cancelledErr struct{ error }
85+
86+
func (cancelledErr) Cancelled() {}
87+
8588
// RunPrune calls the Container Prune API
8689
// This returns the amount of space reclaimed and a detailed output string
8790
func RunPrune(ctx context.Context, dockerCli command.Cli, _ bool, filter opts.FilterOpt) (uint64, string, error) {

cli/command/container/restart_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/docker/cli/internal/test"
1212
"github.com/docker/docker/api/types/container"
13-
"github.com/docker/docker/errdefs"
1413
"gotest.tools/v3/assert"
1514
is "gotest.tools/v3/assert/cmp"
1615
)
@@ -66,7 +65,7 @@ func TestRestart(t *testing.T) {
6665
containerRestartFunc: func(ctx context.Context, containerID string, options container.StopOptions) error {
6766
assert.Check(t, is.DeepEqual(options, tc.expectedOpts))
6867
if containerID == "nosuchcontainer" {
69-
return errdefs.NotFound(errors.New("Error: no such container: " + containerID))
68+
return notFound(errors.New("Error: no such container: " + containerID))
7069
}
7170

7271
// TODO(thaJeztah): consider using parallelOperation for restart, similar to "stop" and "remove"

cli/command/container/rm_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/docker/cli/internal/test"
1212
"github.com/docker/docker/api/types/container"
13-
"github.com/docker/docker/errdefs"
1413
"gotest.tools/v3/assert"
1514
)
1615

@@ -36,7 +35,7 @@ func TestRemoveForce(t *testing.T) {
3635
mutex.Unlock()
3736

3837
if container == "nosuchcontainer" {
39-
return errdefs.NotFound(errors.New("Error: no such container: " + container))
38+
return notFound(errors.New("Error: no such container: " + container))
4039
}
4140
return nil
4241
},

cli/command/container/stop_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/docker/cli/internal/test"
1212
"github.com/docker/docker/api/types/container"
13-
"github.com/docker/docker/errdefs"
1413
"gotest.tools/v3/assert"
1514
is "gotest.tools/v3/assert/cmp"
1615
)
@@ -66,7 +65,7 @@ func TestStop(t *testing.T) {
6665
containerStopFunc: func(ctx context.Context, containerID string, options container.StopOptions) error {
6766
assert.Check(t, is.DeepEqual(options, tc.expectedOpts))
6867
if containerID == "nosuchcontainer" {
69-
return errdefs.NotFound(errors.New("Error: no such container: " + containerID))
68+
return notFound(errors.New("Error: no such container: " + containerID))
7069
}
7170

7271
// containerStopFunc is called in parallel for each container

cli/command/context/create.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package context
55

66
import (
77
"bytes"
8+
"errors"
89
"fmt"
910

1011
cerrdefs "github.com/containerd/errdefs"
@@ -14,7 +15,6 @@ import (
1415
"github.com/docker/cli/cli/command/formatter/tabwriter"
1516
"github.com/docker/cli/cli/context/docker"
1617
"github.com/docker/cli/cli/context/store"
17-
"github.com/pkg/errors"
1818
"github.com/spf13/cobra"
1919
)
2020

@@ -91,7 +91,7 @@ func createNewContext(contextStore store.ReaderWriter, o *CreateOptions) error {
9191
}
9292
dockerEP, dockerTLS, err := getDockerEndpointMetadataAndTLS(contextStore, o.Docker)
9393
if err != nil {
94-
return errors.Wrap(err, "unable to create docker endpoint config")
94+
return fmt.Errorf("unable to create docker endpoint config: %w", err)
9595
}
9696
contextMetadata := store.Metadata{
9797
Endpoints: map[string]any{
@@ -124,9 +124,9 @@ func checkContextNameForCreation(s store.Reader, name string) error {
124124
}
125125
if _, err := s.GetMetadata(name); !cerrdefs.IsNotFound(err) {
126126
if err != nil {
127-
return errors.Wrap(err, "error while getting existing contexts")
127+
return fmt.Errorf("error while getting existing contexts: %w", err)
128128
}
129-
return errors.Errorf("context %q already exists", name)
129+
return fmt.Errorf("context %q already exists", name)
130130
}
131131
return nil
132132
}

0 commit comments

Comments
 (0)