From ae4f6aa662dd26a879f82d507fff13f816748e92 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Mon, 18 Nov 2024 19:07:22 +0200 Subject: [PATCH] refactor: replace fmt.Errorf with errors.New for consistency (#5294) The PR replaces usages of `fmt.Errorf` with `errors.New` for creating errors. Enables `perfsprint` linter to prevent future regressions. --- .golangci.yaml | 8 ++++++++ cmd/build.go | 3 ++- cmd/check.go | 3 ++- cmd/healthcheck.go | 4 ++-- internal/client/client.go | 5 +++-- internal/client/git.go | 2 +- internal/client/gitlab.go | 3 ++- internal/git/config.go | 8 ++++---- internal/pipe/archive/archive.go | 2 +- internal/pipe/blob/blob.go | 4 ++-- internal/pipe/blob/upload.go | 6 ++++-- internal/pipe/brew/brew.go | 2 +- internal/pipe/docker/docker.go | 3 ++- internal/pipe/krew/krew.go | 6 +++--- internal/pipe/linkedin/client.go | 6 +++--- internal/pipe/nix/nix.go | 2 +- internal/pipe/project/project.go | 4 ++-- internal/pipe/sbom/sbom.go | 3 ++- internal/pipe/scoop/scoop.go | 3 ++- internal/pipe/smtp/smtp.go | 7 ++++--- internal/pipe/snapcraft/snapcraft.go | 4 ++-- internal/pipe/snapshot/snapshot.go | 3 ++- internal/pipe/winget/winget.go | 3 ++- 23 files changed, 57 insertions(+), 37 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 1f5d88990..1e3321f6d 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -20,6 +20,7 @@ linters: - gocritic - nolintlint - noctx + - perfsprint linters-settings: gocritic: disabled-checks: @@ -43,6 +44,12 @@ linters-settings: deny: - pkg: "github.com/pkg/errors" desc: "use stdlib instead" + perfsprint: + int-conversion: false + err-error: false + errorf: true + sprintf1: false + strconcat: false revive: enable-all-rules: false rules: @@ -80,3 +87,4 @@ issues: - path: _test\.go linters: - noctx + - perfsprint diff --git a/cmd/build.go b/cmd/build.go index fbc172a9b..d6eb0eb21 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -1,6 +1,7 @@ package cmd import ( + "errors" "fmt" "path/filepath" "runtime" @@ -229,7 +230,7 @@ func (w withOutputPipe) String() string { func (w withOutputPipe) Run(ctx *context.Context) error { bins := ctx.Artifacts.Filter(artifact.ByType(artifact.Binary)).List() if len(bins) == 0 { - return fmt.Errorf("no binary found") + return errors.New("no binary found") } path := bins[0].Path out := w.output diff --git a/cmd/check.go b/cmd/check.go index 50d84be3c..fd190e4ee 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -1,6 +1,7 @@ package cmd import ( + "errors" "fmt" "io" "os" @@ -62,7 +63,7 @@ func newCheckCmd() *checkCmd { if ctx.Deprecated { errs = append(errs, wrapErrorWithCode( - fmt.Errorf("configuration is valid, but uses deprecated properties"), + errors.New("configuration is valid, but uses deprecated properties"), 2, path, )) diff --git a/cmd/healthcheck.go b/cmd/healthcheck.go index b91c3de81..5cf6f1de3 100644 --- a/cmd/healthcheck.go +++ b/cmd/healthcheck.go @@ -1,7 +1,7 @@ package cmd import ( - "fmt" + "errors" "io" "os/exec" "sync" @@ -71,7 +71,7 @@ func newHealthcheckCmd() *healthcheckCmd { return nil } - return fmt.Errorf("one or more needed tools are not present") + return errors.New("one or more needed tools are not present") }); err != nil { return err } diff --git a/internal/client/client.go b/internal/client/client.go index 5c6c65493..358a91972 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -2,6 +2,7 @@ package client import ( + "errors" "fmt" "os" @@ -20,11 +21,11 @@ const ( ) // ErrNotImplemented is returned when a client does not implement certain feature. -var ErrNotImplemented = fmt.Errorf("not implemented") +var ErrNotImplemented = errors.New("not implemented") // ErrReleaseDisabled happens when a configuration tries to use the default // url_template even though the release is disabled. -var ErrReleaseDisabled = fmt.Errorf("release is disabled, cannot use default url_template") +var ErrReleaseDisabled = errors.New("release is disabled, cannot use default url_template") // Info of the repository. type Info struct { diff --git a/internal/client/git.go b/internal/client/git.go index 3e43b9eee..5389c3807 100644 --- a/internal/client/git.go +++ b/internal/client/git.go @@ -155,7 +155,7 @@ func keyPath(key string) (string, error) { _, err := ssh.ParsePrivateKey([]byte(key)) if isPasswordError(err) { - return "", fmt.Errorf("git: key is password-protected") + return "", errors.New("git: key is password-protected") } if err == nil { diff --git a/internal/client/gitlab.go b/internal/client/gitlab.go index 6b2274059..09d096b85 100644 --- a/internal/client/gitlab.go +++ b/internal/client/gitlab.go @@ -3,6 +3,7 @@ package client import ( "cmp" "crypto/tls" + "errors" "fmt" "net/http" "os" @@ -75,7 +76,7 @@ func (c *gitlabClient) checkIsPrivateToken() error { if c.authType == gitlab.PrivateToken { return nil } - return fmt.Errorf("the necessary APIs are not available when using CI_JOB_TOKEN") + return errors.New("the necessary APIs are not available when using CI_JOB_TOKEN") } func (c *gitlabClient) Changelog(_ *context.Context, repo Repo, prev, current string) ([]ChangelogItem, error) { diff --git a/internal/git/config.go b/internal/git/config.go index 233f604bb..8753b40ee 100644 --- a/internal/git/config.go +++ b/internal/git/config.go @@ -19,7 +19,7 @@ func ExtractRepoFromConfig(ctx context.Context) (result config.Repo, err error) } out, err := Clean(Run(ctx, "ls-remote", "--get-url")) if err != nil { - return result, fmt.Errorf("no remote configured to list refs from") + return result, errors.New("no remote configured to list refs from") } // This is a relative remote URL and requires some additional processing if out == "." { @@ -32,15 +32,15 @@ func ExtractRepoFromConfig(ctx context.Context) (result config.Repo, err error) func extractRelativeRepoFromConfig(ctx context.Context) (result config.Repo, err error) { out, err := Clean(Run(ctx, "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}")) if err != nil || out == "" { - return result, fmt.Errorf("unable to get upstream while qualifying relative remote") + return result, errors.New("unable to get upstream while qualifying relative remote") } out, err = Clean(Run(ctx, "config", "--get", fmt.Sprintf("branch.%s.remote", out))) if err != nil || out == "" { - return result, fmt.Errorf("unable to get upstream's remote while qualifying relative remote") + return result, errors.New("unable to get upstream's remote while qualifying relative remote") } out, err = Clean(Run(ctx, "ls-remote", "--get-url", out)) if err != nil { - return result, fmt.Errorf("unable to get upstream while qualifying relative remote") + return result, errors.New("unable to get upstream while qualifying relative remote") } return ExtractRepoFromURL(out) } diff --git a/internal/pipe/archive/archive.go b/internal/pipe/archive/archive.go index 8d026d979..1cbfa3492 100644 --- a/internal/pipe/archive/archive.go +++ b/internal/pipe/archive/archive.go @@ -191,7 +191,7 @@ func create(ctx *context.Context, arch config.Archive, binaries []*artifact.Arti return fmt.Errorf("failed to find files to archive: %w", err) } if arch.Meta && len(files) == 0 { - return fmt.Errorf("no files found") + return errors.New("no files found") } for _, f := range files { if err = a.Add(f); err != nil { diff --git a/internal/pipe/blob/blob.go b/internal/pipe/blob/blob.go index 05cd998f1..fb34a3296 100644 --- a/internal/pipe/blob/blob.go +++ b/internal/pipe/blob/blob.go @@ -2,7 +2,7 @@ package blob import ( - "fmt" + "errors" "github.com/goreleaser/goreleaser/v2/internal/pipe" "github.com/goreleaser/goreleaser/v2/internal/semerrgroup" @@ -22,7 +22,7 @@ func (Pipe) Default(ctx *context.Context) error { for i := range ctx.Config.Blobs { blob := &ctx.Config.Blobs[i] if blob.Bucket == "" || blob.Provider == "" { - return fmt.Errorf("bucket or provider cannot be empty") + return errors.New("bucket or provider cannot be empty") } if blob.Directory == "" { blob.Directory = "{{ .ProjectName }}/{{ .Tag }}" diff --git a/internal/pipe/blob/upload.go b/internal/pipe/blob/upload.go index 52c8c19ab..47d164a11 100644 --- a/internal/pipe/blob/upload.go +++ b/internal/pipe/blob/upload.go @@ -1,11 +1,13 @@ package blob import ( + "errors" "fmt" "io" "net/url" "os" "path" + "strconv" "strings" "github.com/aws/aws-sdk-go/aws" @@ -63,7 +65,7 @@ func urlFor(ctx *context.Context, conf config.Blob) (string, error) { if conf.S3ForcePathStyle == nil { query.Add("s3ForcePathStyle", "true") } else { - query.Add("s3ForcePathStyle", fmt.Sprintf("%t", *conf.S3ForcePathStyle)) + query.Add("s3ForcePathStyle", strconv.FormatBool(*conf.S3ForcePathStyle)) } } @@ -109,7 +111,7 @@ func doUpload(ctx *context.Context, conf config.Blob) error { up.beforeWrite = func(asFunc func(interface{}) bool) error { req := &s3manager.UploadInput{} if !asFunc(&req) { - return fmt.Errorf("could not apply before write") + return errors.New("could not apply before write") } req.ACL = aws.String(conf.ACL) return nil diff --git a/internal/pipe/brew/brew.go b/internal/pipe/brew/brew.go index 27f1819c6..726353493 100644 --- a/internal/pipe/brew/brew.go +++ b/internal/pipe/brew/brew.go @@ -189,7 +189,7 @@ func doPublish(ctx *context.Context, formula *artifact.Artifact, cl client.Clien log.Info("brews.pull_request enabled, creating a PR") pcl, ok := cl.(client.PullRequestOpener) if !ok { - return fmt.Errorf("client does not support pull requests") + return errors.New("client does not support pull requests") } return pcl.OpenPullRequest(ctx, base, repo, msg, brew.Repository.PullRequest.Draft) diff --git a/internal/pipe/docker/docker.go b/internal/pipe/docker/docker.go index 65716aab4..7d2550b7f 100644 --- a/internal/pipe/docker/docker.go +++ b/internal/pipe/docker/docker.go @@ -1,6 +1,7 @@ package docker import ( + "errors" "fmt" "io/fs" "net/http" @@ -238,7 +239,7 @@ Previous error: %w`, tmp, strings.Join(files, "\n "), err) } if isBuildxContextError(err.Error()) { - return fmt.Errorf("docker buildx is not set to default context - please switch with 'docker context use default'") + return errors.New("docker buildx is not set to default context - please switch with 'docker context use default'") } return err } diff --git a/internal/pipe/krew/krew.go b/internal/pipe/krew/krew.go index 61d69fc33..e4c98b06f 100644 --- a/internal/pipe/krew/krew.go +++ b/internal/pipe/krew/krew.go @@ -83,10 +83,10 @@ func doRun(ctx *context.Context, krew config.Krew, cl client.ReleaseURLTemplater return pipe.Skip("krew: manifest name is not set") } if krew.Description == "" { - return fmt.Errorf("krew: manifest description is not set") + return errors.New("krew: manifest description is not set") } if krew.ShortDescription == "" { - return fmt.Errorf("krew: manifest short description is not set") + return errors.New("krew: manifest short description is not set") } filters := []artifact.Filter{ @@ -355,7 +355,7 @@ func doPublish(ctx *context.Context, manifest *artifact.Artifact, cl client.Clie log.Info("krews.pull_request enabled, creating a PR") pcl, ok := cl.(client.PullRequestOpener) if !ok { - return fmt.Errorf("client does not support pull requests") + return errors.New("client does not support pull requests") } return pcl.OpenPullRequest(ctx, base, repo, msg, cfg.Repository.PullRequest.Draft) diff --git a/internal/pipe/linkedin/client.go b/internal/pipe/linkedin/client.go index 4d5224f05..391731803 100644 --- a/internal/pipe/linkedin/client.go +++ b/internal/pipe/linkedin/client.go @@ -37,11 +37,11 @@ type postShareRequest struct { func createLinkedInClient(cfg oauthClientConfig) (client, error) { if cfg.Context == nil { - return client{}, fmt.Errorf("context is nil") + return client{}, errors.New("context is nil") } if cfg.AccessToken == "" { - return client{}, fmt.Errorf("empty access token") + return client{}, errors.New("empty access token") } config := oauth2.Config{} @@ -51,7 +51,7 @@ func createLinkedInClient(cfg oauthClientConfig) (client, error) { }) if c == nil { - return client{}, fmt.Errorf("client is nil") + return client{}, errors.New("client is nil") } return client{ diff --git a/internal/pipe/nix/nix.go b/internal/pipe/nix/nix.go index 696a6250f..4d5b74db2 100644 --- a/internal/pipe/nix/nix.go +++ b/internal/pipe/nix/nix.go @@ -406,7 +406,7 @@ func doPublish(ctx *context.Context, prefetcher shaPrefetcher, cl client.Client, log.Info("nix.pull_request enabled, creating a PR") pcl, ok := cl.(client.PullRequestOpener) if !ok { - return fmt.Errorf("client does not support pull requests") + return errors.New("client does not support pull requests") } return pcl.OpenPullRequest(ctx, base, repo, msg, nix.Repository.PullRequest.Draft) diff --git a/internal/pipe/project/project.go b/internal/pipe/project/project.go index c74bf00ef..0aca8d522 100644 --- a/internal/pipe/project/project.go +++ b/internal/pipe/project/project.go @@ -2,7 +2,7 @@ package project import ( - "fmt" + "errors" "os/exec" "strings" @@ -37,7 +37,7 @@ func (Pipe) Default(ctx *context.Context) error { return nil } - return fmt.Errorf("couldn't guess project_name, please add it to your config") + return errors.New("couldn't guess project_name, please add it to your config") } func moduleName() string { diff --git a/internal/pipe/sbom/sbom.go b/internal/pipe/sbom/sbom.go index 5b1fde467..ccbe2481d 100644 --- a/internal/pipe/sbom/sbom.go +++ b/internal/pipe/sbom/sbom.go @@ -2,6 +2,7 @@ package sbom import ( "bytes" + "errors" "fmt" "io" "os" @@ -242,7 +243,7 @@ func catalogArtifact(ctx *context.Context, cfg config.SBOM, a *artifact.Artifact } if len(artifacts) == 0 { - return nil, fmt.Errorf("cataloging artifacts: command did not write any files, check your configuration") + return nil, errors.New("cataloging artifacts: command did not write any files, check your configuration") } return artifacts, nil diff --git a/internal/pipe/scoop/scoop.go b/internal/pipe/scoop/scoop.go index 3740db63f..83b2dfb29 100644 --- a/internal/pipe/scoop/scoop.go +++ b/internal/pipe/scoop/scoop.go @@ -4,6 +4,7 @@ package scoop import ( "bytes" "encoding/json" + "errors" "fmt" "os" "path" @@ -273,7 +274,7 @@ func doPublish(ctx *context.Context, manifest *artifact.Artifact, cl client.Clie log.Info("scoop.pull_request enabled, creating a PR") pcl, ok := cl.(client.PullRequestOpener) if !ok { - return fmt.Errorf("client does not support pull requests") + return errors.New("client does not support pull requests") } return pcl.OpenPullRequest(ctx, base, repo, commitMessage, scoop.Repository.PullRequest.Draft) diff --git a/internal/pipe/smtp/smtp.go b/internal/pipe/smtp/smtp.go index afaf80c0a..73997f034 100644 --- a/internal/pipe/smtp/smtp.go +++ b/internal/pipe/smtp/smtp.go @@ -2,6 +2,7 @@ package smtp import ( "crypto/tls" + "errors" "fmt" "github.com/caarlos0/env/v11" @@ -90,9 +91,9 @@ func (Pipe) Announce(ctx *context.Context) error { } var ( - errNoPort = fmt.Errorf("SMTP: missing smtp.port or $SMTP_PORT") - errNoUsername = fmt.Errorf("SMTP: missing smtp.username or $SMTP_USERNAME") - errNoHost = fmt.Errorf("SMTP: missing smtp.host or $SMTP_HOST") + errNoPort = errors.New("SMTP: missing smtp.port or $SMTP_PORT") + errNoUsername = errors.New("SMTP: missing smtp.username or $SMTP_USERNAME") + errNoHost = errors.New("SMTP: missing smtp.host or $SMTP_HOST") ) func getConfig(smtp config.SMTP) (Config, error) { diff --git a/internal/pipe/snapcraft/snapcraft.go b/internal/pipe/snapcraft/snapcraft.go index 5948ccf05..5932245f8 100644 --- a/internal/pipe/snapcraft/snapcraft.go +++ b/internal/pipe/snapcraft/snapcraft.go @@ -130,10 +130,10 @@ func (Pipe) Default(ctx *context.Context) error { snap.Confinement = "strict" } if snap.Description == "" { - return fmt.Errorf("description is required") + return errors.New("description is required") } if snap.Summary == "" { - return fmt.Errorf("summary is required") + return errors.New("summary is required") } if len(snap.ChannelTemplates) == 0 { switch snap.Grade { diff --git a/internal/pipe/snapshot/snapshot.go b/internal/pipe/snapshot/snapshot.go index 951afcc9e..4c1897dc9 100644 --- a/internal/pipe/snapshot/snapshot.go +++ b/internal/pipe/snapshot/snapshot.go @@ -2,6 +2,7 @@ package snapshot import ( + "errors" "fmt" "github.com/caarlos0/log" @@ -34,7 +35,7 @@ func (Pipe) Run(ctx *context.Context) error { return fmt.Errorf("failed to parse snapshot name: %w", err) } if name == "" { - return fmt.Errorf("empty snapshot name") + return errors.New("empty snapshot name") } ctx.Version = name log.WithField("version", ctx.Version).Infof("building snapshot...") diff --git a/internal/pipe/winget/winget.go b/internal/pipe/winget/winget.go index c2ac58b66..1cb09b86b 100644 --- a/internal/pipe/winget/winget.go +++ b/internal/pipe/winget/winget.go @@ -1,6 +1,7 @@ package winget import ( + "errors" "fmt" "os" "path" @@ -351,7 +352,7 @@ func doPublish(ctx *context.Context, cl client.Client, wingets []*artifact.Artif log.Info("winget.pull_request enabled, creating a PR") pcl, ok := cl.(client.PullRequestOpener) if !ok { - return fmt.Errorf("client does not support pull requests") + return errors.New("client does not support pull requests") } return pcl.OpenPullRequest(ctx, base, repo, msg, winget.Repository.PullRequest.Draft)