From d4600627b82a95a9bc205217c5cf48de29224062 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 9 Oct 2024 17:05:00 -0700 Subject: [PATCH] Switch from pkg/errors to %w part 1 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. Let's switch to %s-style error wrapping. Changes here are done by hand, touching code code which is not handled correctly by the go-wrap-to-percent-w tool. The reason being, errors.Wrap(err, "text") returns nil when err is nil, while fmt.Errorf("text: %w", err) returns non-nil even if err is nil. Signed-off-by: Kir Kolyshkin --- cli/command/context/options.go | 6 +++++- cli/command/system/version.go | 8 +++++--- cli/compose/interpolation/interpolation.go | 6 +++++- cli/context/store/store.go | 11 ++++++----- cli/registry/client/client.go | 10 ++++++++-- opts/gpus.go | 5 ++++- 6 files changed, 33 insertions(+), 13 deletions(-) diff --git a/cli/command/context/options.go b/cli/command/context/options.go index 7b39f7d768..d1cdb1e9c5 100644 --- a/cli/command/context/options.go +++ b/cli/command/context/options.go @@ -1,6 +1,7 @@ package context import ( + "fmt" "strconv" "strings" @@ -68,7 +69,10 @@ func parseBool(config map[string]string, name string) (bool, error) { return false, nil } res, err := strconv.ParseBool(strVal) - return res, errors.Wrap(err, name) + if err != nil { + return res, fmt.Errorf("%s: %w", name, err) + } + return res, nil } func validateConfig(config map[string]string, allowedKeys map[string]struct{}) error { diff --git a/cli/command/system/version.go b/cli/command/system/version.go index f25446b06d..1fe5537271 100644 --- a/cli/command/system/version.go +++ b/cli/command/system/version.go @@ -2,6 +2,7 @@ package system import ( "context" + "fmt" "runtime" "sort" "strconv" @@ -17,7 +18,6 @@ import ( "github.com/docker/cli/cli/version" "github.com/docker/cli/templates" "github.com/docker/docker/api/types" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/tonistiigi/go-rosetta" ) @@ -210,8 +210,10 @@ func newVersionTemplate(templateFormat string) (*template.Template, error) { } tmpl := templates.New("version").Funcs(template.FuncMap{"getDetailsOrder": getDetailsOrder}) tmpl, err := tmpl.Parse(templateFormat) - - return tmpl, errors.Wrap(err, "template parsing error") + if err != nil { + return nil, fmt.Errorf("template parsing error: %w", err) + } + return tmpl, nil } func getDetailsOrder(v types.ComponentVersion) []string { diff --git a/cli/compose/interpolation/interpolation.go b/cli/compose/interpolation/interpolation.go index 42aefc6620..11345a6a36 100644 --- a/cli/compose/interpolation/interpolation.go +++ b/cli/compose/interpolation/interpolation.go @@ -4,6 +4,7 @@ package interpolation import ( + "fmt" "os" "strings" @@ -67,7 +68,10 @@ func recursiveInterpolate(value any, path Path, opts Options) (any, error) { return newValue, nil } casted, err := caster(newValue) - return casted, newPathError(path, errors.Wrap(err, "failed to cast to expected type")) + if err != nil { + return nil, newPathError(path, fmt.Errorf("failed to cast to expected type: %w", err)) + } + return casted, nil case map[string]any: out := map[string]any{} diff --git a/cli/context/store/store.go b/cli/context/store/store.go index 066b5769d7..894c732be6 100644 --- a/cli/context/store/store.go +++ b/cli/context/store/store.go @@ -10,6 +10,7 @@ import ( "bytes" _ "crypto/sha256" // ensure ids can be computed "encoding/json" + "fmt" "io" "net/http" "path" @@ -344,13 +345,13 @@ func Import(name string, s Writer, reader io.Reader) error { func isValidFilePath(p string) error { if p != metaFile && !strings.HasPrefix(p, "tls/") { - return errors.New("unexpected context file") + return fmt.Errorf("%s: unexpected context file", p) } if path.Clean(p) != p { - return errors.New("unexpected path format") + return fmt.Errorf("%s: unexpected path format", p) } if strings.Contains(p, `\`) { - return errors.New(`unexpected '\' in path`) + return fmt.Errorf(`%s: unexpected '\' in path`, p) } return nil } @@ -374,7 +375,7 @@ func importTar(name string, s Writer, reader io.Reader) error { continue } if err := isValidFilePath(hdr.Name); err != nil { - return errors.Wrap(err, hdr.Name) + return err } if hdr.Name == metaFile { data, err := io.ReadAll(tr) @@ -426,7 +427,7 @@ func importZip(name string, s Writer, reader io.Reader) error { continue } if err := isValidFilePath(zf.Name); err != nil { - return errors.Wrap(err, zf.Name) + return err } if zf.Name == metaFile { f, err := zf.Open() diff --git a/cli/registry/client/client.go b/cli/registry/client/client.go index bbc7f4c584..9256c041fb 100644 --- a/cli/registry/client/client.go +++ b/cli/registry/client/client.go @@ -121,7 +121,10 @@ func (c *client) PutManifest(ctx context.Context, ref reference.Named, manifest } dgst, err := manifestService.Put(ctx, manifest, opts...) - return dgst, errors.Wrapf(err, "failed to put manifest %s", ref) + if err != nil { + return "", fmt.Errorf("failed to put manifest %s: %w", ref, err) + } + return dgst, nil } func (c *client) getRepositoryForReference(ctx context.Context, ref reference.Named, repoEndpoint repositoryEndpoint) (distribution.Repository, error) { @@ -157,7 +160,10 @@ func (c *client) getHTTPTransportForRepoEndpoint(ctx context.Context, repoEndpoi c.userAgent, repoEndpoint.actions, ) - return httpTransport, errors.Wrap(err, "failed to configure transport") + if err != nil { + return nil, fmt.Errorf("failed to configure transport: %w", err) + } + return httpTransport, nil } // GetManifest returns an ImageManifest for the reference diff --git a/opts/gpus.go b/opts/gpus.go index 93bf939786..d671c263b6 100644 --- a/opts/gpus.go +++ b/opts/gpus.go @@ -20,7 +20,10 @@ func parseCount(s string) (int, error) { return -1, nil } i, err := strconv.Atoi(s) - return i, errors.Wrap(err, "count must be an integer") + if err != nil { + return 0, fmt.Errorf("count must be an integer: %w", err) + } + return i, nil } // Set a new mount value