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 <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin 2024-10-09 17:05:00 -07:00
parent 305985c1b1
commit d4600627b8
6 changed files with 33 additions and 13 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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{}

View File

@ -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()

View File

@ -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

View File

@ -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