Skip to content

Commit

Permalink
refactor(deps): replace unmaintained pkg/errors with fmt.Errorf (#…
Browse files Browse the repository at this point in the history
…747)

Signed-off-by: william.vanhevelingen <[email protected]>
  • Loading branch information
blkperl authored Oct 20, 2024
1 parent 5b72b34 commit ae8aa65
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 45 deletions.
6 changes: 3 additions & 3 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (

log "github.com/sirupsen/logrus"
"k8s.io/klog/v2"

"github.com/argoproj/pkg/errors"
)

// SetLogLevel parses and sets a logrus log level
func SetLogLevel(logLevel string) {
level, err := log.ParseLevel(logLevel)
errors.CheckError(err)
if err != nil {
log.Fatal(err)
}
log.SetLevel(level)
}

Expand Down
6 changes: 3 additions & 3 deletions exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package exec

import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
Expand All @@ -10,7 +11,6 @@ import (
"syscall"
"time"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/argoproj/pkg/rand"
Expand Down Expand Up @@ -183,7 +183,7 @@ type WaitPIDOpts struct {
// WaitPID waits for a non-child process id to exit
func WaitPID(pid int, opts ...WaitPIDOpts) error {
if runtime.GOOS != "linux" {
return errors.Errorf("Platform '%s' unsupported", runtime.GOOS)
return fmt.Errorf("platform '%s' unsupported", runtime.GOOS)
}
var timeout time.Duration
pollInterval := time.Second
Expand Down Expand Up @@ -211,7 +211,7 @@ func WaitPID(pid int, opts ...WaitPIDOpts) error {
if os.IsNotExist(err) {
return nil
}
return errors.WithStack(err)
return err
}
case <-timoutCh:
return ErrWaitPIDTimeout
Expand Down
6 changes: 2 additions & 4 deletions file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ package file

import (
"os"

"github.com/pkg/errors"
)

// IsDirectory returns whether or not the given file is a directory
func IsDirectory(path string) (bool, error) {
fileOrDir, err := os.Open(path)
if err != nil {
return false, errors.WithStack(err)
return false, err
}
defer func() { _ = fileOrDir.Close() }()
stat, err := fileOrDir.Stat()
if err != nil {
return false, errors.WithStack(err)
return false, err
}
return stat.IsDir(), nil
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
github.com/golang/protobuf v1.3.3
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/minio/minio-go/v7 v7.0.77
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
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/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down
2 changes: 0 additions & 2 deletions kube/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import (
"net/http"
"strings"

"github.com/pkg/errors"
apierr "k8s.io/apimachinery/pkg/api/errors"
)

// IsRequestEntityTooLargeErr determines if err is an error which indicates the size of the request
// was too large for the server to handle.
func IsRequestEntityTooLargeErr(err error) bool {
err = errors.Cause(err)
switch t := err.(type) {
case apierr.APIStatus:
if t.Status().Code == http.StatusRequestEntityTooLarge {
Expand Down
55 changes: 28 additions & 27 deletions s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
Expand All @@ -18,7 +20,6 @@ import (
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/minio/minio-go/v7/pkg/encrypt"
"github.com/minio/minio-go/v7/pkg/sse"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -173,7 +174,7 @@ func NewS3Client(ctx context.Context, opts S3ClientOpts) (S3Client, error) {

credentials, err := GetCredentials(opts)
if err != nil {
return nil, errors.WithStack(err)
return nil, err
}

var bucketLookupType minio.BucketLookupType
Expand All @@ -187,18 +188,18 @@ func NewS3Client(ctx context.Context, opts S3ClientOpts) (S3Client, error) {
minioOpts := &minio.Options{Creds: credentials, Secure: s3cli.Secure, Transport: opts.Transport, Region: s3cli.Region, BucketLookup: bucketLookupType}
minioClient, err = minio.New(s3cli.Endpoint, minioOpts)
if err != nil {
return nil, errors.WithStack(err)
return nil, err
}
if opts.Trace {
minioClient.TraceOn(log.StandardLogger().Out)
}

if opts.EncryptOpts.KmsKeyId != "" && opts.EncryptOpts.ServerSideCustomerKey != "" {
return nil, errors.New("EncryptOpts.KmsKeyId and EncryptOpts.SSECPassword cannot be set together")
return nil, fmt.Errorf("EncryptOpts.KmsKeyId and EncryptOpts.SSECPassword cannot be set together")
}

if opts.EncryptOpts.ServerSideCustomerKey != "" && !opts.Secure {
return nil, errors.New("Secure must be set if EncryptOpts.SSECPassword is set")
return nil, fmt.Errorf("Secure must be set if EncryptOpts.SSECPassword is set")
}

s3cli.ctx = ctx
Expand All @@ -215,32 +216,32 @@ func (s *s3client) PutFile(bucket, key, path string) error {
encOpts, err := s.EncryptOpts.buildServerSideEnc(bucket, key)

if err != nil {
return errors.WithStack(err)
return err
}

_, err = s.minioClient.FPutObject(s.ctx, bucket, key, path, minio.PutObjectOptions{ServerSideEncryption: encOpts})
if err != nil {
return errors.WithStack(err)
return err
}
return nil
}

func (s *s3client) BucketExists(bucketName string) (bool, error) {
log.WithField("bucket", bucketName).Info("Checking if bucket exists")
result, err := s.minioClient.BucketExists(s.ctx, bucketName)
return result, errors.WithStack(err)
return result, err
}

func (s *s3client) MakeBucket(bucketName string, opts minio.MakeBucketOptions) error {
log.WithFields(log.Fields{"bucket": bucketName, "region": opts.Region, "objectLocking": opts.ObjectLocking}).Info("Creating bucket")
err := s.minioClient.MakeBucket(s.ctx, bucketName, opts)

if err != nil {
return errors.WithStack(err)
return err
}

err = s.setBucketEnc(bucketName)
return errors.WithStack(err)
return err
}

type uploadTask struct {
Expand Down Expand Up @@ -294,12 +295,12 @@ func (s *s3client) GetFile(bucket, key, path string) error {

encOpts, err := s.EncryptOpts.buildServerSideEnc(bucket, key)
if err != nil {
return errors.WithStack(err)
return err
}

err = s.minioClient.FGetObject(s.ctx, bucket, key, path, minio.GetObjectOptions{ServerSideEncryption: encOpts})
if err != nil {
return errors.WithStack(err)
return err
}
return nil
}
Expand All @@ -310,16 +311,16 @@ func (s *s3client) OpenFile(bucket, key string) (io.ReadCloser, error) {

encOpts, err := s.EncryptOpts.buildServerSideEnc(bucket, key)
if err != nil {
return nil, errors.WithStack(err)
return nil, err
}
f, err := s.minioClient.GetObject(s.ctx, bucket, key, minio.GetObjectOptions{ServerSideEncryption: encOpts})
if err != nil {
return nil, errors.WithStack(err)
return nil, err
}
// the call above doesn't return an error in the case that the key doesn't exist, but by calling Stat() it will
_, err = f.Stat()
if err != nil {
return nil, errors.WithStack(err)
return nil, err
}
return f, nil
}
Expand All @@ -330,7 +331,7 @@ func (s *s3client) KeyExists(bucket, key string) (bool, error) {

encOpts, err := s.EncryptOpts.buildServerSideEnc(bucket, key)
if err != nil {
return false, errors.WithStack(err)
return false, err
}

_, err = s.minioClient.StatObject(s.ctx, bucket, key, minio.StatObjectOptions{ServerSideEncryption: encOpts})
Expand All @@ -341,7 +342,7 @@ func (s *s3client) KeyExists(bucket, key string) (bool, error) {
return false, nil
}

return false, errors.WithStack(err)
return false, err
}

func (s *s3client) Delete(bucket, key string) error {
Expand All @@ -363,12 +364,12 @@ func (s *s3client) GetDirectory(bucket, keyPrefix, path string) error {

encOpts, err := s.EncryptOpts.buildServerSideEnc(bucket, objKey)
if err != nil {
return errors.WithStack(err)
return err
}

err = s.minioClient.FGetObject(s.ctx, bucket, objKey, localPath, minio.GetObjectOptions{ServerSideEncryption: encOpts})
if err != nil {
return errors.WithStack(err)
return err
}
}
return nil
Expand All @@ -394,7 +395,7 @@ func (s *s3client) IsDirectory(bucket, keyPrefix string) (bool, error) {
objCh := s.minioClient.ListObjects(s.ctx, bucket, listOpts)
for obj := range objCh {
if obj.Err != nil {
return false, errors.WithStack(obj.Err)
return false, obj.Err
} else {
return true, nil
}
Expand Down Expand Up @@ -422,7 +423,7 @@ func (s *s3client) ListDirectory(bucket, keyPrefix string) ([]string, error) {
objCh := s.minioClient.ListObjects(s.ctx, bucket, listOpts)
for obj := range objCh {
if obj.Err != nil {
return nil, errors.WithStack(obj.Err)
return nil, obj.Err
}
if strings.HasSuffix(obj.Key, "/") {
// When a dir is created through AWS S3 console, a nameless obj will be created
Expand All @@ -440,8 +441,8 @@ func (s *s3client) ListDirectory(bucket, keyPrefix string) ([]string, error) {

// IsS3ErrCode returns if the supplied error is of a specific S3 error code
func IsS3ErrCode(err error, code string) bool {
err = errors.Cause(err)
if minioErr, ok := err.(minio.ErrorResponse); ok {
var minioErr minio.ErrorResponse
if errors.As(err, &minioErr) {
return minioErr.Code == code
}
return false
Expand Down Expand Up @@ -481,7 +482,7 @@ func (e *EncryptOpts) buildServerSideEnc(bucket, key string) (encrypt.ServerSide
encryptionCtx, err := parseKMSEncCntx(e.KmsEncryptionContext)

if err != nil {
return nil, errors.Wrap(err, "failed to parse KMS encryption context")
return nil, fmt.Errorf("failed to parse KMS encryption context: %w", err)
}

if encryptionCtx == nil {
Expand All @@ -498,7 +499,7 @@ func (e *EncryptOpts) buildServerSideEnc(bucket, key string) (encrypt.ServerSide
kms, err := encrypt.NewSSEKMS(e.KmsKeyId, encryptionCtx)

if err != nil {
return nil, errors.WithStack(err)
return nil, err
}

return kms, nil
Expand All @@ -515,10 +516,10 @@ func parseKMSEncCntx(kmsEncCntx string) (*string, error) {

jsonKMSEncryptionContext, err := json.Marshal(json.RawMessage(kmsEncCntx))
if err != nil {
return nil, errors.Wrap(err, "failed to marshal KMS encryption context")
return nil, fmt.Errorf("failed to marshal KMS encryption context: %w", err)
}

parsedKMSEncryptionContext := base64.StdEncoding.EncodeToString([]byte(jsonKMSEncryptionContext))
parsedKMSEncryptionContext := base64.StdEncoding.EncodeToString(jsonKMSEncryptionContext)

return &parsedKMSEncryptionContext, nil
}
5 changes: 2 additions & 3 deletions time/time.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package time

import (
"fmt"
"log"
"regexp"
"strconv"
"time"

"github.com/pkg/errors"
)

var durationRegex = regexp.MustCompile(`^(\d+)([smhd])$`)
Expand All @@ -15,7 +14,7 @@ var durationRegex = regexp.MustCompile(`^(\d+)([smhd])$`)
func ParseDuration(duration string) (*time.Duration, error) {
matches := durationRegex.FindStringSubmatch(duration)
if len(matches) != 3 {
return nil, errors.Errorf("Invalid since format '%s'. Expected format <duration><unit> (e.g. 3h)\n", duration)
return nil, fmt.Errorf("Invalid since format '%s'. Expected format <duration><unit> (e.g. 3h)\n", duration)
}
amount, err := strconv.ParseInt(matches[1], 10, 64)
if err != nil {
Expand Down

0 comments on commit ae8aa65

Please sign in to comment.