diff --git a/.golangci.yml b/.golangci.yml index a566f849a..406764428 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,8 +5,10 @@ linters: disable-all: true enable: - dupl + - errorlint - gofmt - goimports + - gomodguard - gosimple - govet - ineffassign @@ -19,5 +21,12 @@ linters: linters-settings: gofmt: simplify: true + gomodguard: + blocked: + modules: + - github.com/pkg/errors: + recommendations: + - errors + - fmt dupl: threshold: 400 diff --git a/go.mod b/go.mod index dad0d8c79..26a40cf3c 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 766d1071d..b78325856 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/schema/error.go b/schema/error.go index baf875195..c667c23e2 100644 --- a/schema/error.go +++ b/schema/error.go @@ -17,6 +17,7 @@ package schema import ( "bufio" "encoding/json" + "errors" "io" ) @@ -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 diff --git a/schema/spec_test.go b/schema/spec_test.go index e8dde99f0..df5c9dc1d 100644 --- a/schema/spec_test.go +++ b/schema/spec_test.go @@ -16,6 +16,7 @@ package schema_test import ( "bytes" + "errors" "fmt" "io" "net/url" @@ -26,7 +27,6 @@ import ( "testing" "github.com/opencontainers/image-spec/schema" - "github.com/pkg/errors" "github.com/russross/blackfriday" ) @@ -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 } @@ -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) diff --git a/schema/validator.go b/schema/validator.go index 4b8cd0869..b7a0acaa8 100644 --- a/schema/validator.go +++ b/schema/validator.go @@ -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" ) @@ -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 { @@ -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() { @@ -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) { @@ -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 @@ -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 { @@ -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) @@ -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) } }