diff --git a/pkg/detectors/launchdarkly/launchdarkly.go b/pkg/detectors/launchdarkly/launchdarkly.go index eada888f..debded7d 100644 --- a/pkg/detectors/launchdarkly/launchdarkly.go +++ b/pkg/detectors/launchdarkly/launchdarkly.go @@ -2,6 +2,7 @@ package launchdarkly import ( "context" + "errors" "fmt" "net/http" "regexp" @@ -88,7 +89,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result _, err := ldclient.MakeCustomClient(resMatch, defaultSDKConfig, defaultSDKTimeout) if err == nil { s1.Verified = true - } else if err == ldclient.ErrInitializationFailed || err.Error() == invalidSDKKeyError { + } else if errors.Is(err, ldclient.ErrInitializationFailed) || err.Error() == invalidSDKKeyError { // If initialization fails, the key is not valid, so do nothing. } else { // If the error isn't nil or known, then this is likely a timeout error: ldclient.ErrInitializationTimeout diff --git a/pkg/output/legacy_json.go b/pkg/output/legacy_json.go index e6423d04..799d7149 100644 --- a/pkg/output/legacy_json.go +++ b/pkg/output/legacy_json.go @@ -2,6 +2,7 @@ package output import ( "encoding/json" + "errors" "fmt" "log" "net/url" @@ -172,7 +173,7 @@ func GenerateDiff(commit *object.Commit, fileName string) string { // First grab the first parent of the commit. If there are none, we are at the first commit and should diff against // an empty file. parent, err := commit.Parent(0) - if err != object.ErrParentNotFound && err != nil { + if !errors.Is(err, object.ErrParentNotFound) && err != nil { logger.Error(err, "could not find parent", "commit", commit.Hash.String()) } @@ -180,7 +181,7 @@ func GenerateDiff(commit *object.Commit, fileName string) string { var parentFile *object.File if parent != nil { parentFile, err = parent.File(fileName) - if err != nil && err != object.ErrFileNotFound { + if err != nil && !errors.Is(err, object.ErrFileNotFound) { logger.Error(err, "could not get previous version of file") return diff } diff --git a/pkg/sources/git/git.go b/pkg/sources/git/git.go index 0c1e2b40..0e4f5e60 100644 --- a/pkg/sources/git/git.go +++ b/pkg/sources/git/git.go @@ -3,6 +3,7 @@ package git import ( "bufio" "bytes" + "errors" "fmt" "net/url" "os" @@ -13,9 +14,7 @@ import ( "sync/atomic" "time" - "github.com/go-errors/errors" "github.com/go-git/go-git/v5" - gogit "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" "github.com/google/go-github/v42/github" @@ -116,7 +115,7 @@ func (s *Source) Init(aCtx context.Context, name string, jobId sources.JobID, so var conn sourcespb.Git if err := anypb.UnmarshalTo(connection, &conn, proto.UnmarshalOptions{}); err != nil { - return errors.WrapPrefix(err, "error unmarshalling connection", 0) + return fmt.Errorf("error unmarshalling connection: %w", err) } if uri := conn.GetUri(); uri != "" { @@ -131,7 +130,7 @@ func (s *Source) Init(aCtx context.Context, name string, jobId sources.JobID, so if err != nil { return fmt.Errorf("error creating filter: %w", err) } - opts := []ScanOption{ScanOptionFilter(filter), ScanOptionLogOptions(new(gogit.LogOptions))} + opts := []ScanOption{ScanOptionFilter(filter), ScanOptionLogOptions(new(git.LogOptions))} if depth := conn.GetMaxDepth(); depth != 0 { opts = append(opts, ScanOptionMaxDepth(depth)) @@ -388,7 +387,7 @@ func executeClone(ctx context.Context, params cloneParams) (*git.Repository, err // Execute command and wait for the stdout / stderr. output, err := cloneCmd.CombinedOutput() if err != nil { - err = errors.WrapPrefix(err, "error running 'git clone'", 0) + err = fmt.Errorf("error executing git clone: %w", err) } logger.V(3).Info("git subcommand finished", "output", string(output)) @@ -742,14 +741,14 @@ func normalizeConfig(scanOptions *ScanOptions, repo *git.Repository) (err error) if !plumbing.IsHash(scanOptions.BaseHash) { base, err := TryAdditionalBaseRefs(repo, scanOptions.BaseHash) if err != nil { - return errors.WrapPrefix(err, "unable to resolve base ref", 0) + return fmt.Errorf("unable to resolve base ref: %w", err) } scanOptions.BaseHash = base.String() baseCommit, _ = repo.CommitObject(plumbing.NewHash(scanOptions.BaseHash)) } else { baseCommit, err = repo.CommitObject(baseHash) if err != nil { - return errors.WrapPrefix(err, "unable to resolve base ref", 0) + return fmt.Errorf("unable to resolve base ref: %w", err) } } } @@ -760,14 +759,14 @@ func normalizeConfig(scanOptions *ScanOptions, repo *git.Repository) (err error) if !plumbing.IsHash(scanOptions.HeadHash) { head, err := TryAdditionalBaseRefs(repo, scanOptions.HeadHash) if err != nil { - return errors.WrapPrefix(err, "unable to resolve head ref", 0) + return fmt.Errorf("unable to resolve head ref: %w", err) } scanOptions.HeadHash = head.String() headCommit, _ = repo.CommitObject(plumbing.NewHash(scanOptions.HeadHash)) } else { headCommit, err = repo.CommitObject(headHash) if err != nil { - return errors.WrapPrefix(err, "unable to resolve head ref", 0) + return fmt.Errorf("unable to resolve head ref: %w", err) } } } @@ -776,7 +775,7 @@ func normalizeConfig(scanOptions *ScanOptions, repo *git.Repository) (err error) if headCommit != nil && baseCommit != nil { mergeBase, err := headCommit.MergeBase(baseCommit) if err != nil || len(mergeBase) < 1 { - return errors.WrapPrefix(err, "could not find common base between the given references", 0) + return fmt.Errorf("unable to resolve merge base: %w", err) } scanOptions.BaseHash = mergeBase[0].Hash.String() } @@ -791,7 +790,7 @@ func stripPassword(u string) (string, error) { repoURL, err := url.Parse(u) if err != nil { - return "", errors.WrapPrefix(err, "repo remote cannot be sanitized as URI", 0) + return "", fmt.Errorf("repo remote is not a URI: %w", err) } repoURL.User = nil @@ -808,7 +807,7 @@ func TryAdditionalBaseRefs(repo *git.Repository, base string) (*plumbing.Hash, e } for _, prefix := range revisionPrefixes { outHash, err := repo.ResolveRevision(plumbing.Revision(prefix + base)) - if err == plumbing.ErrReferenceNotFound { + if errors.Is(err, plumbing.ErrReferenceNotFound) { continue } if err != nil {