1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-04-15 11:56:56 +02:00

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.
This commit is contained in:
Oleksandr Redko 2024-11-18 19:07:22 +02:00 committed by GitHub
parent bae3bacc7d
commit ae4f6aa662
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
23 changed files with 57 additions and 37 deletions

View File

@ -20,6 +20,7 @@ linters:
- gocritic - gocritic
- nolintlint - nolintlint
- noctx - noctx
- perfsprint
linters-settings: linters-settings:
gocritic: gocritic:
disabled-checks: disabled-checks:
@ -43,6 +44,12 @@ linters-settings:
deny: deny:
- pkg: "github.com/pkg/errors" - pkg: "github.com/pkg/errors"
desc: "use stdlib instead" desc: "use stdlib instead"
perfsprint:
int-conversion: false
err-error: false
errorf: true
sprintf1: false
strconcat: false
revive: revive:
enable-all-rules: false enable-all-rules: false
rules: rules:
@ -80,3 +87,4 @@ issues:
- path: _test\.go - path: _test\.go
linters: linters:
- noctx - noctx
- perfsprint

View File

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"errors"
"fmt" "fmt"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -229,7 +230,7 @@ func (w withOutputPipe) String() string {
func (w withOutputPipe) Run(ctx *context.Context) error { func (w withOutputPipe) Run(ctx *context.Context) error {
bins := ctx.Artifacts.Filter(artifact.ByType(artifact.Binary)).List() bins := ctx.Artifacts.Filter(artifact.ByType(artifact.Binary)).List()
if len(bins) == 0 { if len(bins) == 0 {
return fmt.Errorf("no binary found") return errors.New("no binary found")
} }
path := bins[0].Path path := bins[0].Path
out := w.output out := w.output

View File

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -62,7 +63,7 @@ func newCheckCmd() *checkCmd {
if ctx.Deprecated { if ctx.Deprecated {
errs = append(errs, wrapErrorWithCode( errs = append(errs, wrapErrorWithCode(
fmt.Errorf("configuration is valid, but uses deprecated properties"), errors.New("configuration is valid, but uses deprecated properties"),
2, 2,
path, path,
)) ))

View File

@ -1,7 +1,7 @@
package cmd package cmd
import ( import (
"fmt" "errors"
"io" "io"
"os/exec" "os/exec"
"sync" "sync"
@ -71,7 +71,7 @@ func newHealthcheckCmd() *healthcheckCmd {
return nil 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 { }); err != nil {
return err return err
} }

View File

@ -2,6 +2,7 @@
package client package client
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
@ -20,11 +21,11 @@ const (
) )
// ErrNotImplemented is returned when a client does not implement certain feature. // 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 // ErrReleaseDisabled happens when a configuration tries to use the default
// url_template even though the release is disabled. // 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. // Info of the repository.
type Info struct { type Info struct {

View File

@ -155,7 +155,7 @@ func keyPath(key string) (string, error) {
_, err := ssh.ParsePrivateKey([]byte(key)) _, err := ssh.ParsePrivateKey([]byte(key))
if isPasswordError(err) { if isPasswordError(err) {
return "", fmt.Errorf("git: key is password-protected") return "", errors.New("git: key is password-protected")
} }
if err == nil { if err == nil {

View File

@ -3,6 +3,7 @@ package client
import ( import (
"cmp" "cmp"
"crypto/tls" "crypto/tls"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
@ -75,7 +76,7 @@ func (c *gitlabClient) checkIsPrivateToken() error {
if c.authType == gitlab.PrivateToken { if c.authType == gitlab.PrivateToken {
return nil 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) { func (c *gitlabClient) Changelog(_ *context.Context, repo Repo, prev, current string) ([]ChangelogItem, error) {

View File

@ -19,7 +19,7 @@ func ExtractRepoFromConfig(ctx context.Context) (result config.Repo, err error)
} }
out, err := Clean(Run(ctx, "ls-remote", "--get-url")) out, err := Clean(Run(ctx, "ls-remote", "--get-url"))
if err != nil { 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 // This is a relative remote URL and requires some additional processing
if out == "." { 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) { func extractRelativeRepoFromConfig(ctx context.Context) (result config.Repo, err error) {
out, err := Clean(Run(ctx, "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}")) out, err := Clean(Run(ctx, "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"))
if err != nil || out == "" { 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))) out, err = Clean(Run(ctx, "config", "--get", fmt.Sprintf("branch.%s.remote", out)))
if err != nil || 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)) out, err = Clean(Run(ctx, "ls-remote", "--get-url", out))
if err != nil { 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) return ExtractRepoFromURL(out)
} }

View File

@ -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) return fmt.Errorf("failed to find files to archive: %w", err)
} }
if arch.Meta && len(files) == 0 { if arch.Meta && len(files) == 0 {
return fmt.Errorf("no files found") return errors.New("no files found")
} }
for _, f := range files { for _, f := range files {
if err = a.Add(f); err != nil { if err = a.Add(f); err != nil {

View File

@ -2,7 +2,7 @@
package blob package blob
import ( import (
"fmt" "errors"
"github.com/goreleaser/goreleaser/v2/internal/pipe" "github.com/goreleaser/goreleaser/v2/internal/pipe"
"github.com/goreleaser/goreleaser/v2/internal/semerrgroup" "github.com/goreleaser/goreleaser/v2/internal/semerrgroup"
@ -22,7 +22,7 @@ func (Pipe) Default(ctx *context.Context) error {
for i := range ctx.Config.Blobs { for i := range ctx.Config.Blobs {
blob := &ctx.Config.Blobs[i] blob := &ctx.Config.Blobs[i]
if blob.Bucket == "" || blob.Provider == "" { 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 == "" { if blob.Directory == "" {
blob.Directory = "{{ .ProjectName }}/{{ .Tag }}" blob.Directory = "{{ .ProjectName }}/{{ .Tag }}"

View File

@ -1,11 +1,13 @@
package blob package blob
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"net/url" "net/url"
"os" "os"
"path" "path"
"strconv"
"strings" "strings"
"github.com/aws/aws-sdk-go/aws" "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 { if conf.S3ForcePathStyle == nil {
query.Add("s3ForcePathStyle", "true") query.Add("s3ForcePathStyle", "true")
} else { } 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 { up.beforeWrite = func(asFunc func(interface{}) bool) error {
req := &s3manager.UploadInput{} req := &s3manager.UploadInput{}
if !asFunc(&req) { 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) req.ACL = aws.String(conf.ACL)
return nil return nil

View File

@ -189,7 +189,7 @@ func doPublish(ctx *context.Context, formula *artifact.Artifact, cl client.Clien
log.Info("brews.pull_request enabled, creating a PR") log.Info("brews.pull_request enabled, creating a PR")
pcl, ok := cl.(client.PullRequestOpener) pcl, ok := cl.(client.PullRequestOpener)
if !ok { 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) return pcl.OpenPullRequest(ctx, base, repo, msg, brew.Repository.PullRequest.Draft)

View File

@ -1,6 +1,7 @@
package docker package docker
import ( import (
"errors"
"fmt" "fmt"
"io/fs" "io/fs"
"net/http" "net/http"
@ -238,7 +239,7 @@ Previous error:
%w`, tmp, strings.Join(files, "\n "), err) %w`, tmp, strings.Join(files, "\n "), err)
} }
if isBuildxContextError(err.Error()) { 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 return err
} }

View File

@ -83,10 +83,10 @@ func doRun(ctx *context.Context, krew config.Krew, cl client.ReleaseURLTemplater
return pipe.Skip("krew: manifest name is not set") return pipe.Skip("krew: manifest name is not set")
} }
if krew.Description == "" { if krew.Description == "" {
return fmt.Errorf("krew: manifest description is not set") return errors.New("krew: manifest description is not set")
} }
if krew.ShortDescription == "" { 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{ 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") log.Info("krews.pull_request enabled, creating a PR")
pcl, ok := cl.(client.PullRequestOpener) pcl, ok := cl.(client.PullRequestOpener)
if !ok { 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) return pcl.OpenPullRequest(ctx, base, repo, msg, cfg.Repository.PullRequest.Draft)

View File

@ -37,11 +37,11 @@ type postShareRequest struct {
func createLinkedInClient(cfg oauthClientConfig) (client, error) { func createLinkedInClient(cfg oauthClientConfig) (client, error) {
if cfg.Context == nil { if cfg.Context == nil {
return client{}, fmt.Errorf("context is nil") return client{}, errors.New("context is nil")
} }
if cfg.AccessToken == "" { if cfg.AccessToken == "" {
return client{}, fmt.Errorf("empty access token") return client{}, errors.New("empty access token")
} }
config := oauth2.Config{} config := oauth2.Config{}
@ -51,7 +51,7 @@ func createLinkedInClient(cfg oauthClientConfig) (client, error) {
}) })
if c == nil { if c == nil {
return client{}, fmt.Errorf("client is nil") return client{}, errors.New("client is nil")
} }
return client{ return client{

View File

@ -406,7 +406,7 @@ func doPublish(ctx *context.Context, prefetcher shaPrefetcher, cl client.Client,
log.Info("nix.pull_request enabled, creating a PR") log.Info("nix.pull_request enabled, creating a PR")
pcl, ok := cl.(client.PullRequestOpener) pcl, ok := cl.(client.PullRequestOpener)
if !ok { 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) return pcl.OpenPullRequest(ctx, base, repo, msg, nix.Repository.PullRequest.Draft)

View File

@ -2,7 +2,7 @@
package project package project
import ( import (
"fmt" "errors"
"os/exec" "os/exec"
"strings" "strings"
@ -37,7 +37,7 @@ func (Pipe) Default(ctx *context.Context) error {
return nil 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 { func moduleName() string {

View File

@ -2,6 +2,7 @@ package sbom
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -242,7 +243,7 @@ func catalogArtifact(ctx *context.Context, cfg config.SBOM, a *artifact.Artifact
} }
if len(artifacts) == 0 { 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 return artifacts, nil

View File

@ -4,6 +4,7 @@ package scoop
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"os" "os"
"path" "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") log.Info("scoop.pull_request enabled, creating a PR")
pcl, ok := cl.(client.PullRequestOpener) pcl, ok := cl.(client.PullRequestOpener)
if !ok { 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) return pcl.OpenPullRequest(ctx, base, repo, commitMessage, scoop.Repository.PullRequest.Draft)

View File

@ -2,6 +2,7 @@ package smtp
import ( import (
"crypto/tls" "crypto/tls"
"errors"
"fmt" "fmt"
"github.com/caarlos0/env/v11" "github.com/caarlos0/env/v11"
@ -90,9 +91,9 @@ func (Pipe) Announce(ctx *context.Context) error {
} }
var ( var (
errNoPort = fmt.Errorf("SMTP: missing smtp.port or $SMTP_PORT") errNoPort = errors.New("SMTP: missing smtp.port or $SMTP_PORT")
errNoUsername = fmt.Errorf("SMTP: missing smtp.username or $SMTP_USERNAME") errNoUsername = errors.New("SMTP: missing smtp.username or $SMTP_USERNAME")
errNoHost = fmt.Errorf("SMTP: missing smtp.host or $SMTP_HOST") errNoHost = errors.New("SMTP: missing smtp.host or $SMTP_HOST")
) )
func getConfig(smtp config.SMTP) (Config, error) { func getConfig(smtp config.SMTP) (Config, error) {

View File

@ -130,10 +130,10 @@ func (Pipe) Default(ctx *context.Context) error {
snap.Confinement = "strict" snap.Confinement = "strict"
} }
if snap.Description == "" { if snap.Description == "" {
return fmt.Errorf("description is required") return errors.New("description is required")
} }
if snap.Summary == "" { if snap.Summary == "" {
return fmt.Errorf("summary is required") return errors.New("summary is required")
} }
if len(snap.ChannelTemplates) == 0 { if len(snap.ChannelTemplates) == 0 {
switch snap.Grade { switch snap.Grade {

View File

@ -2,6 +2,7 @@
package snapshot package snapshot
import ( import (
"errors"
"fmt" "fmt"
"github.com/caarlos0/log" "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) return fmt.Errorf("failed to parse snapshot name: %w", err)
} }
if name == "" { if name == "" {
return fmt.Errorf("empty snapshot name") return errors.New("empty snapshot name")
} }
ctx.Version = name ctx.Version = name
log.WithField("version", ctx.Version).Infof("building snapshot...") log.WithField("version", ctx.Version).Infof("building snapshot...")

View File

@ -1,6 +1,7 @@
package winget package winget
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path" "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") log.Info("winget.pull_request enabled, creating a PR")
pcl, ok := cl.(client.PullRequestOpener) pcl, ok := cl.(client.PullRequestOpener)
if !ok { 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) return pcl.OpenPullRequest(ctx, base, repo, msg, winget.Repository.PullRequest.Draft)