Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deps: remove deprecated github.com/pkg/errors #1125

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ linters:
disable-all: true
enable:
- dupl
- errorlint
- gofmt
- goimports
- gomodguard
- gosimple
- govet
- ineffassign
Expand All @@ -19,5 +21,12 @@ linters:
linters-settings:
gofmt:
simplify: true
gomodguard:
blocked:
modules:
- github.com/pkg/errors:
recommendations:
- errors
- fmt
dupl:
threshold: 400
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.18

require (
github.com/opencontainers/go-digest v1.0.0
github.com/pkg/errors v0.9.1
github.com/russross/blackfriday v1.6.0
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415
github.com/xeipuuv/gojsonschema v1.2.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
Expand Down
4 changes: 3 additions & 1 deletion schema/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package schema
import (
"bufio"
"encoding/json"
"errors"
"io"
)

Expand All @@ -34,7 +35,8 @@ func (e *SyntaxError) Error() string { return e.msg }
// and converts it into a *schema.SyntaxError containing line/col information using the given reader.
// If the given error is not a *json.SyntaxError it is returned unchanged.
func WrapSyntaxError(r io.Reader, err error) error {
if serr, ok := err.(*json.SyntaxError); ok {
var serr *json.SyntaxError
if errors.As(err, &serr) {
buf := bufio.NewReader(r)
line := 0
col := 0
Expand Down
7 changes: 4 additions & 3 deletions schema/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package schema_test

import (
"bytes"
"errors"
"fmt"
"io"
"net/url"
Expand All @@ -26,7 +27,6 @@ import (
"testing"

"github.com/opencontainers/image-spec/schema"
"github.com/pkg/errors"
"github.com/russross/blackfriday"
)

Expand Down Expand Up @@ -93,7 +93,7 @@ func validate(t *testing.T, name string) {
}

for _, example := range examples {
if example.Err == errFormatInvalid && example.Mediatype == "" { // ignore
if errors.Is(example.Err, errFormatInvalid) && example.Mediatype == "" { // ignore
continue
}

Expand All @@ -111,7 +111,8 @@ func validate(t *testing.T, name string) {
}

var errs []error
if verr, ok := errors.Cause(err).(schema.ValidationError); ok {
var verr schema.ValidationError
if errors.As(err, &verr) {
errs = verr.Errs
} else {
printFields(t, "error", example.Mediatype, example.Title, err)
Expand Down
29 changes: 14 additions & 15 deletions schema/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ package schema
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"regexp"

digest "github.com/opencontainers/go-digest"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/xeipuuv/gojsonschema"
)

Expand Down Expand Up @@ -53,7 +53,7 @@ func (e ValidationError) Error() string {
func (v Validator) Validate(src io.Reader) error {
buf, err := io.ReadAll(src)
if err != nil {
return errors.Wrap(err, "unable to read the document file")
return fmt.Errorf("unable to read the document file: %w", err)
}

if f, ok := mapValidate[v]; ok {
Expand All @@ -71,9 +71,8 @@ func (v Validator) Validate(src io.Reader) error {

result, err := gojsonschema.Validate(sl, ml)
if err != nil {
return errors.Wrapf(
WrapSyntaxError(bytes.NewReader(buf), err),
"schema %s: unable to validate", v)
return fmt.Errorf("schema %s: unable to validate: %w", v,
WrapSyntaxError(bytes.NewReader(buf), err))
}

if result.Valid() {
Expand Down Expand Up @@ -101,12 +100,12 @@ func validateManifest(r io.Reader) error {

buf, err := io.ReadAll(r)
if err != nil {
return errors.Wrapf(err, "error reading the io stream")
return fmt.Errorf("error reading the io stream: %w", err)
}

err = json.Unmarshal(buf, &header)
if err != nil {
return errors.Wrap(err, "manifest format mismatch")
return fmt.Errorf("manifest format mismatch: %w", err)
}

if header.Config.MediaType != string(v1.MediaTypeImageConfig) {
Expand All @@ -131,16 +130,16 @@ func validateDescriptor(r io.Reader) error {

buf, err := io.ReadAll(r)
if err != nil {
return errors.Wrapf(err, "error reading the io stream")
return fmt.Errorf("error reading the io stream: %w", err)
}

err = json.Unmarshal(buf, &header)
if err != nil {
return errors.Wrap(err, "descriptor format mismatch")
return fmt.Errorf("descriptor format mismatch: %w", err)
}

err = header.Digest.Validate()
if err == digest.ErrDigestUnsupported {
if errors.Is(err, digest.ErrDigestUnsupported) {
// we ignore unsupported algorithms
fmt.Printf("warning: unsupported digest: %q: %v\n", header.Digest, err)
return nil
Expand All @@ -153,12 +152,12 @@ func validateIndex(r io.Reader) error {

buf, err := io.ReadAll(r)
if err != nil {
return errors.Wrapf(err, "error reading the io stream")
return fmt.Errorf("error reading the io stream: %w", err)
}

err = json.Unmarshal(buf, &header)
if err != nil {
return errors.Wrap(err, "index format mismatch")
return fmt.Errorf("index format mismatch: %w", err)
}

for _, manifest := range header.Manifests {
Expand All @@ -180,12 +179,12 @@ func validateConfig(r io.Reader) error {

buf, err := io.ReadAll(r)
if err != nil {
return errors.Wrapf(err, "error reading the io stream")
return fmt.Errorf("error reading the io stream: %w", err)
}

err = json.Unmarshal(buf, &header)
if err != nil {
return errors.Wrap(err, "config format mismatch")
return fmt.Errorf("config format mismatch: %w", err)
}

checkPlatform(header.OS, header.Architecture)
Expand All @@ -194,7 +193,7 @@ func validateConfig(r io.Reader) error {
envRegexp := regexp.MustCompile(`^[^=]+=.*$`)
for _, e := range header.Config.Env {
if !envRegexp.MatchString(e) {
return errors.Errorf("unexpected env: %q", e)
return fmt.Errorf("unexpected env: %q", e)
}
}

Expand Down