Skip to content

Commit

Permalink
Switch from github.com/pkg/errors to %w-style wrap
Browse files Browse the repository at this point in the history
The github.com/pkg/errors is mostly obsoleted since Go 1.13 introduced
%w-style error wrapping. It is also not maintained and is now archived
by the owner.

Done by using https://github.com/AkihiroSuda/go-wrap-to-percent-w tool.

Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed Oct 9, 2024
1 parent 305985c commit 9b2a50e
Show file tree
Hide file tree
Showing 214 changed files with 852 additions and 717 deletions.
6 changes: 3 additions & 3 deletions cli-plugins/manager/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package manager

import (
"github.com/pkg/errors"
"fmt"
)

// pluginError is set as Plugin.Err by NewPlugin if the plugin
Expand Down Expand Up @@ -44,11 +44,11 @@ func wrapAsPluginError(err error, msg string) error {
if err == nil {
return nil
}
return &pluginError{cause: errors.Wrap(err, msg)}
return &pluginError{cause: fmt.Errorf(msg+": %w", err)}
}

// NewPluginError creates a new pluginError, analogous to
// errors.Errorf.
func NewPluginError(msg string, args ...any) error {
return &pluginError{cause: errors.Errorf(msg, args...)}
return &pluginError{cause: fmt.Errorf(msg, args...)}
}
9 changes: 5 additions & 4 deletions cli-plugins/manager/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package manager
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -44,14 +45,14 @@ func newPlugin(c Candidate, cmds []*cobra.Command) (Plugin, error) {
// which would fail here, so there are all real errors.
fullname := filepath.Base(path)
if fullname == "." {
return Plugin{}, errors.Errorf("unable to determine basename of plugin candidate %q", path)
return Plugin{}, fmt.Errorf("unable to determine basename of plugin candidate %q", path)
}
var err error
if fullname, err = trimExeSuffix(fullname); err != nil {
return Plugin{}, errors.Wrapf(err, "plugin candidate %q", path)
return Plugin{}, fmt.Errorf("plugin candidate %q: %w", path, err)
}
if !strings.HasPrefix(fullname, NamePrefix) {
return Plugin{}, errors.Errorf("plugin candidate %q: does not have %q prefix", path, NamePrefix)
return Plugin{}, fmt.Errorf("plugin candidate %q: does not have %q prefix", path, NamePrefix)
}

p := Plugin{
Expand Down
7 changes: 3 additions & 4 deletions cli-plugins/manager/suffix_windows.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package manager

import (
"fmt"
"path/filepath"
"strings"

"github.com/pkg/errors"
)

// This is made slightly more complex due to needing to be case insensitive.
func trimExeSuffix(s string) (string, error) {
ext := filepath.Ext(s)
if ext == "" {
return "", errors.Errorf("path %q lacks required file extension", s)
return "", fmt.Errorf("path %q lacks required file extension", s)
}

exe := ".exe"
if !strings.EqualFold(ext, exe) {
return "", errors.Errorf("path %q lacks required %q suffix", s, exe)
return "", fmt.Errorf("path %q lacks required %q suffix", s, exe)
}
return strings.TrimSuffix(s, ext), nil
}
Expand Down
4 changes: 2 additions & 2 deletions cli/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/fvbommel/sortorder"
"github.com/moby/term"
"github.com/morikuni/aec"
"github.com/pkg/errors"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -214,7 +214,7 @@ var helpCommand = &cobra.Command{
RunE: func(c *cobra.Command, args []string) error {
cmd, args, e := c.Root().Find(args)
if cmd == nil || e != nil || len(args) > 0 {
return errors.Errorf("unknown help topic: %v", strings.Join(args, " "))
return fmt.Errorf("unknown help topic: %v", strings.Join(args, " "))
}
helpFunc := cmd.HelpFunc()
helpFunc(cmd, args)
Expand Down
5 changes: 3 additions & 2 deletions cli/command/checkpoint/create_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package checkpoint

import (
"fmt"
"io"
"strings"
"testing"

"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types/checkpoint"
"github.com/pkg/errors"

"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
Expand All @@ -29,7 +30,7 @@ func TestCheckpointCreateErrors(t *testing.T) {
{
args: []string{"foo", "bar"},
checkpointCreateFunc: func(container string, options checkpoint.CreateOptions) error {
return errors.Errorf("error creating checkpoint for container foo")
return fmt.Errorf("error creating checkpoint for container foo")
},
expectedError: "error creating checkpoint for container foo",
},
Expand Down
5 changes: 3 additions & 2 deletions cli/command/checkpoint/list_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package checkpoint

import (
"fmt"
"io"
"testing"

"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types/checkpoint"
"github.com/pkg/errors"

"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/golden"
Expand All @@ -29,7 +30,7 @@ func TestCheckpointListErrors(t *testing.T) {
{
args: []string{"foo"},
checkpointListFunc: func(container string, options checkpoint.ListOptions) ([]checkpoint.Summary, error) {
return []checkpoint.Summary{}, errors.Errorf("error getting checkpoints for container foo")
return []checkpoint.Summary{}, fmt.Errorf("error getting checkpoints for container foo")
},
expectedError: "error getting checkpoints for container foo",
},
Expand Down
5 changes: 3 additions & 2 deletions cli/command/checkpoint/remove_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package checkpoint

import (
"fmt"
"io"
"testing"

"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types/checkpoint"
"github.com/pkg/errors"

"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
Expand All @@ -28,7 +29,7 @@ func TestCheckpointRemoveErrors(t *testing.T) {
{
args: []string{"foo", "bar"},
checkpointDeleteFunc: func(container string, options checkpoint.DeleteOptions) error {
return errors.Errorf("error deleting checkpoint")
return fmt.Errorf("error deleting checkpoint")
},
expectedError: "error deleting checkpoint",
},
Expand Down
9 changes: 5 additions & 4 deletions cli/command/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package command

import (
"context"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -33,7 +34,7 @@ import (
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
"github.com/docker/go-connections/tlsconfig"
"github.com/pkg/errors"

"github.com/spf13/cobra"
notaryclient "github.com/theupdateframework/notary/client"
)
Expand Down Expand Up @@ -177,7 +178,7 @@ func (cli *DockerCli) BuildKitEnabled() (bool, error) {
if v := os.Getenv("DOCKER_BUILDKIT"); v != "" {
enabled, err := strconv.ParseBool(v)
if err != nil {
return false, errors.Wrap(err, "DOCKER_BUILDKIT environment variable expects boolean value")
return false, fmt.Errorf("DOCKER_BUILDKIT environment variable expects boolean value: %w", err)
}
return enabled, nil
}
Expand Down Expand Up @@ -311,7 +312,7 @@ func NewAPIClientFromFlags(opts *cliflags.ClientOptions, configFile *configfile.
}
endpoint, err := resolveDockerEndpoint(contextStore, resolveContextName(opts, configFile))
if err != nil {
return nil, errors.Wrap(err, "unable to resolve docker endpoint")
return nil, fmt.Errorf("unable to resolve docker endpoint: %w", err)
}
return newAPIClientFromEndpoint(endpoint, configFile)
}
Expand Down Expand Up @@ -492,7 +493,7 @@ func (cli *DockerCli) initialize() error {
cli.init.Do(func() {
cli.dockerEndpoint, cli.initErr = cli.getDockerEndPoint()
if cli.initErr != nil {
cli.initErr = errors.Wrap(cli.initErr, "unable to resolve docker endpoint")
cli.initErr = fmt.Errorf("unable to resolve docker endpoint: %w", cli.initErr)
return
}
if cli.client == nil {
Expand Down
8 changes: 4 additions & 4 deletions cli/command/cli_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
import (
"context"
"encoding/csv"
"fmt"
"io"
"net/http"
"os"
Expand All @@ -13,7 +14,6 @@ import (
"github.com/docker/docker/client"
"github.com/docker/docker/errdefs"
"github.com/moby/term"
"github.com/pkg/errors"
)

// CLIOption is a functional argument to apply options to a [DockerCli]. These
Expand Down Expand Up @@ -177,7 +177,7 @@ func withCustomHeadersFromEnv() client.Opt {
csvReader := csv.NewReader(strings.NewReader(value))
fields, err := csvReader.Read()
if err != nil {
return errdefs.InvalidParameter(errors.Errorf("failed to parse custom headers from %s environment variable: value must be formatted as comma-separated key=value pairs", envOverrideHTTPHeaders))
return errdefs.InvalidParameter(fmt.Errorf("failed to parse custom headers from %s environment variable: value must be formatted as comma-separated key=value pairs", envOverrideHTTPHeaders))
}
if len(fields) == 0 {
return nil
Expand All @@ -191,15 +191,15 @@ func withCustomHeadersFromEnv() client.Opt {
k = strings.TrimSpace(k)

if k == "" {
return errdefs.InvalidParameter(errors.Errorf(`failed to set custom headers from %s environment variable: value contains a key=value pair with an empty key: '%s'`, envOverrideHTTPHeaders, kv))
return errdefs.InvalidParameter(fmt.Errorf(`failed to set custom headers from %s environment variable: value contains a key=value pair with an empty key: '%s'`, envOverrideHTTPHeaders, kv))
}

// We don't currently allow empty key=value pairs, and produce an error.
// This is something we could allow in future (e.g. to read value
// from an environment variable with the same name). In the meantime,
// produce an error to prevent users from depending on this.
if !hasValue {
return errdefs.InvalidParameter(errors.Errorf(`failed to set custom headers from %s environment variable: missing "=" in key=value pair: '%s'`, envOverrideHTTPHeaders, kv))
return errdefs.InvalidParameter(fmt.Errorf(`failed to set custom headers from %s environment variable: missing "=" in key=value pair: '%s'`, envOverrideHTTPHeaders, kv))
}

env[http.CanonicalHeaderKey(k)] = v
Expand Down
3 changes: 2 additions & 1 deletion cli/command/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net"
Expand All @@ -22,7 +23,7 @@ import (
"github.com/docker/docker/api"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/pkg/errors"

"gotest.tools/v3/assert"
"gotest.tools/v3/fs"
)
Expand Down
4 changes: 2 additions & 2 deletions cli/command/config/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/docker/cli/opts"
"github.com/docker/docker/api/types/swarm"
"github.com/moby/sys/sequential"
"github.com/pkg/errors"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -63,7 +63,7 @@ func RunConfigCreate(ctx context.Context, dockerCli command.Cli, options CreateO

configData, err := io.ReadAll(in)
if err != nil {
return errors.Errorf("Error reading content from %q: %v", options.File, err)
return fmt.Errorf("Error reading content from %q: %v", options.File, err)
}

spec := swarm.ConfigSpec{
Expand Down
13 changes: 7 additions & 6 deletions cli/command/config/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"context"
"fmt"
"io"
"os"
"path/filepath"
Expand All @@ -12,7 +13,7 @@ import (
"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/pkg/errors"

"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/golden"
Expand All @@ -37,7 +38,7 @@ func TestConfigCreateErrors(t *testing.T) {
{
args: []string{"name", filepath.Join("testdata", configDataFile)},
configCreateFunc: func(_ context.Context, configSpec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
return types.ConfigCreateResponse{}, errors.Errorf("error creating config")
return types.ConfigCreateResponse{}, fmt.Errorf("error creating config")
},
expectedError: "error creating config",
},
Expand All @@ -64,7 +65,7 @@ func TestConfigCreateWithName(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{
configCreateFunc: func(_ context.Context, spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
if spec.Name != name {
return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
return types.ConfigCreateResponse{}, fmt.Errorf("expected name %q, got %q", name, spec.Name)
}

actual = spec.Data
Expand Down Expand Up @@ -103,7 +104,7 @@ func TestConfigCreateWithLabels(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{
configCreateFunc: func(_ context.Context, spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
if !reflect.DeepEqual(spec, expected) {
return types.ConfigCreateResponse{}, errors.Errorf("expected %+v, got %+v", expected, spec)
return types.ConfigCreateResponse{}, fmt.Errorf("expected %+v, got %+v", expected, spec)
}

return types.ConfigCreateResponse{
Expand All @@ -129,11 +130,11 @@ func TestConfigCreateWithTemplatingDriver(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{
configCreateFunc: func(_ context.Context, spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
if spec.Name != name {
return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
return types.ConfigCreateResponse{}, fmt.Errorf("expected name %q, got %q", name, spec.Name)
}

if spec.Templating.Name != expectedDriver.Name {
return types.ConfigCreateResponse{}, errors.Errorf("expected driver %v, got %v", expectedDriver, spec.Labels)
return types.ConfigCreateResponse{}, fmt.Errorf("expected driver %v, got %v", expectedDriver, spec.Labels)
}

return types.ConfigCreateResponse{
Expand Down
8 changes: 4 additions & 4 deletions cli/command/config/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/docker/cli/internal/test"
"github.com/docker/cli/internal/test/builders"
"github.com/docker/docker/api/types/swarm"
"github.com/pkg/errors"

"gotest.tools/v3/assert"
"gotest.tools/v3/golden"
)
Expand All @@ -28,7 +28,7 @@ func TestConfigInspectErrors(t *testing.T) {
{
args: []string{"foo"},
configInspectFunc: func(_ context.Context, configID string) (swarm.Config, []byte, error) {
return swarm.Config{}, nil, errors.Errorf("error while inspecting the config")
return swarm.Config{}, nil, fmt.Errorf("error while inspecting the config")
},
expectedError: "error while inspecting the config",
},
Expand All @@ -45,7 +45,7 @@ func TestConfigInspectErrors(t *testing.T) {
if configID == "foo" {
return *builders.Config(builders.ConfigName("foo")), nil, nil
}
return swarm.Config{}, nil, errors.Errorf("error while inspecting the config")
return swarm.Config{}, nil, fmt.Errorf("error while inspecting the config")
},
expectedError: "error while inspecting the config",
},
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestConfigInspectWithoutFormat(t *testing.T) {
args: []string{"foo"},
configInspectFunc: func(_ context.Context, name string) (swarm.Config, []byte, error) {
if name != "foo" {
return swarm.Config{}, nil, errors.Errorf("Invalid name, expected %s, got %s", "foo", name)
return swarm.Config{}, nil, fmt.Errorf("Invalid name, expected %s, got %s", "foo", name)
}
return *builders.Config(builders.ConfigID("ID-foo"), builders.ConfigName("foo")), nil, nil
},
Expand Down
Loading

0 comments on commit 9b2a50e

Please sign in to comment.