diff --git a/.golangci.yml b/.golangci.yml index 61903c51..0b2d5f11 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -23,6 +23,7 @@ linters: - testifylint - godot - copyloopvar + - perfsprint issues: max-same-issues: 0 diff --git a/cmd/pint/ci.go b/cmd/pint/ci.go index a90fbf8c..f0704765 100644 --- a/cmd/pint/ci.go +++ b/cmd/pint/ci.go @@ -2,6 +2,7 @@ package main import ( "context" + "errors" "fmt" "log/slog" "os" @@ -80,7 +81,7 @@ func actionCI(c *cli.Context) error { } currentBranch, err := git.CurrentBranch(git.RunGit) if err != nil { - return fmt.Errorf("failed to get the name of current branch") + return errors.New("failed to get the name of current branch") } slog.Debug("Got branch information", slog.String("base", baseBranch), slog.String("current", currentBranch)) if currentBranch == strings.Split(baseBranch, "/")[len(strings.Split(baseBranch, "/"))-1] { @@ -134,7 +135,7 @@ func actionCI(c *cli.Context) error { if meta.cfg.Repository != nil && meta.cfg.Repository.BitBucket != nil { token, ok := os.LookupEnv("BITBUCKET_AUTH_TOKEN") if !ok { - return fmt.Errorf("BITBUCKET_AUTH_TOKEN env variable is required when reporting to BitBucket") + return errors.New("BITBUCKET_AUTH_TOKEN env variable is required when reporting to BitBucket") } timeout, _ := time.ParseDuration(meta.cfg.Repository.BitBucket.Timeout) @@ -154,7 +155,7 @@ func actionCI(c *cli.Context) error { if meta.cfg.Repository != nil && meta.cfg.Repository.GitLab != nil { token, ok := os.LookupEnv("GITLAB_AUTH_TOKEN") if !ok { - return fmt.Errorf("GITLAB_AUTH_TOKEN env variable is required when reporting to GitLab") + return errors.New("GITLAB_AUTH_TOKEN env variable is required when reporting to GitLab") } timeout, _ := time.ParseDuration(meta.cfg.Repository.GitLab.Timeout) @@ -177,12 +178,12 @@ func actionCI(c *cli.Context) error { if meta.cfg.Repository != nil && meta.cfg.Repository.GitHub != nil { token, ok := os.LookupEnv("GITHUB_AUTH_TOKEN") if !ok { - return fmt.Errorf("GITHUB_AUTH_TOKEN env variable is required when reporting to GitHub") + return errors.New("GITHUB_AUTH_TOKEN env variable is required when reporting to GitHub") } prVal, ok := os.LookupEnv("GITHUB_PULL_REQUEST_NUMBER") if !ok { - return fmt.Errorf("GITHUB_PULL_REQUEST_NUMBER env variable is required when reporting to GitHub") + return errors.New("GITHUB_PULL_REQUEST_NUMBER env variable is required when reporting to GitHub") } var prNum int @@ -234,7 +235,7 @@ func actionCI(c *cli.Context) error { } if problemsFound { - return fmt.Errorf("problems found") + return errors.New("problems found") } return nil diff --git a/cmd/pint/lint.go b/cmd/pint/lint.go index ce49071d..32a1d961 100644 --- a/cmd/pint/lint.go +++ b/cmd/pint/lint.go @@ -2,6 +2,7 @@ package main import ( "context" + "errors" "fmt" "log/slog" "os" @@ -58,7 +59,7 @@ func actionLint(c *cli.Context) error { paths := c.Args().Slice() if len(paths) == 0 { - return fmt.Errorf("at least one file or directory required") + return errors.New("at least one file or directory required") } slog.Info("Finding all rules to check", slog.Any("paths", paths)) diff --git a/cmd/pint/parse.go b/cmd/pint/parse.go index 3ad7c0c0..e00806f3 100644 --- a/cmd/pint/parse.go +++ b/cmd/pint/parse.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "strings" @@ -123,7 +124,7 @@ func actionParse(c *cli.Context) (err error) { parts := c.Args().Slice() if len(parts) == 0 { - return fmt.Errorf("a query string is required") + return errors.New("a query string is required") } query := strings.Join(parts, " ") return parseQuery(query) diff --git a/cmd/pint/scan.go b/cmd/pint/scan.go index 1a9b03a8..261f5716 100644 --- a/cmd/pint/scan.go +++ b/cmd/pint/scan.go @@ -218,7 +218,7 @@ func scanWorker(ctx context.Context, jobs <-chan scanJob, results chan<- reporte Last: commentErr.Line, }, Reporter: pintCommentReporter, - Text: fmt.Sprintf("This comment is not a valid pint control comment: %s", commentErr.Error()), + Text: "This comment is not a valid pint control comment: " + commentErr.Error(), Severity: checks.Warning, }, Owner: job.entry.Owner, diff --git a/cmd/pint/watch.go b/cmd/pint/watch.go index dfa7d643..0ba7f5c1 100644 --- a/cmd/pint/watch.go +++ b/cmd/pint/watch.go @@ -52,7 +52,7 @@ var watchCmd = &cli.Command{ paths := c.Args().Slice() if len(paths) == 0 { - return fmt.Errorf("at least one file or directory required") + return errors.New("at least one file or directory required") } slog.Debug("Starting glob watch", slog.Any("paths", paths)) @@ -72,7 +72,7 @@ var watchCmd = &cli.Command{ args := c.Args().Slice() if len(args) != 1 { - return fmt.Errorf("exactly one argument required with the URI of Prometheus server to query") + return errors.New("exactly one argument required with the URI of Prometheus server to query") } gen := config.NewPrometheusGenerator(meta.cfg, prometheus.NewRegistry()) diff --git a/internal/checks/alerts_annotation.go b/internal/checks/alerts_annotation.go index d5858667..c987a080 100644 --- a/internal/checks/alerts_annotation.go +++ b/internal/checks/alerts_annotation.go @@ -165,7 +165,7 @@ func (c AnnotationCheck) checkValue(rule parser.Rule, value string, lines parser func maybeComment(c string) string { if c != "" { - return fmt.Sprintf("Rule comment: %s", c) + return "Rule comment: " + c } return "" } diff --git a/internal/checks/promql_series.go b/internal/checks/promql_series.go index ef77d1f8..f3875d61 100644 --- a/internal/checks/promql_series.go +++ b/internal/checks/promql_series.go @@ -614,7 +614,7 @@ func (c SeriesCheck) getMinAge(rule parser.Rule, selector promParser.VectorSelec minAge = time.Hour * 2 bareSelector := stripLabels(selector) prefixes := []string{ - fmt.Sprintf("%s min-age ", c.Reporter()), + c.Reporter() + " min-age ", fmt.Sprintf("%s(%s) min-age ", c.Reporter(), bareSelector.String()), fmt.Sprintf("%s(%s) min-age ", c.Reporter(), selector.String()), } diff --git a/internal/checks/rule_link_test.go b/internal/checks/rule_link_test.go index f5538eed..18dfdbbc 100644 --- a/internal/checks/rule_link_test.go +++ b/internal/checks/rule_link_test.go @@ -210,7 +210,7 @@ func TestRuleLinkCheck(t *testing.T) { checker: func(_ *promapi.FailoverGroup) checks.RuleChecker { return checks.NewRuleLinkCheck( checks.MustTemplatedRegexp("http://(.*)"), - fmt.Sprintf(srv.URL+"/rewrite"), + srv.URL+"/rewrite", time.Second, map[string]string{"X-Host": "rewrite.example.com"}, "", diff --git a/internal/comments/comments_test.go b/internal/comments/comments_test.go index cc41b2ec..9cae1d03 100644 --- a/internal/comments/comments_test.go +++ b/internal/comments/comments_test.go @@ -55,7 +55,7 @@ func TestParse(t *testing.T) { Type: comments.InvalidComment, Value: comments.Invalid{Err: comments.CommentError{ Line: 1, - Err: fmt.Errorf(`unexpected comment suffix: "this file"`), + Err: errors.New(`unexpected comment suffix: "this file"`), }}, }, }, @@ -73,7 +73,7 @@ func TestParse(t *testing.T) { Type: comments.InvalidComment, Value: comments.Invalid{Err: comments.CommentError{ Line: 1, - Err: fmt.Errorf(`unexpected comment suffix: "this line"`), + Err: errors.New(`unexpected comment suffix: "this line"`), }}, }, }, @@ -91,7 +91,7 @@ func TestParse(t *testing.T) { Type: comments.InvalidComment, Value: comments.Invalid{Err: comments.CommentError{ Line: 1, - Err: fmt.Errorf(`unexpected comment suffix: "here"`), + Err: errors.New(`unexpected comment suffix: "here"`), }}, }, }, @@ -109,7 +109,7 @@ func TestParse(t *testing.T) { Type: comments.InvalidComment, Value: comments.Invalid{Err: comments.CommentError{ Line: 1, - Err: fmt.Errorf(`unexpected comment suffix: "here"`), + Err: errors.New(`unexpected comment suffix: "here"`), }}, }, }, @@ -127,7 +127,7 @@ func TestParse(t *testing.T) { Type: comments.InvalidComment, Value: comments.Invalid{Err: comments.CommentError{ Line: 1, - Err: fmt.Errorf(`unexpected comment suffix: "here"`), + Err: errors.New(`unexpected comment suffix: "here"`), }}, }, }, @@ -145,7 +145,7 @@ func TestParse(t *testing.T) { Type: comments.InvalidComment, Value: comments.Invalid{Err: comments.CommentError{ Line: 1, - Err: fmt.Errorf("missing file/owner value"), + Err: errors.New("missing file/owner value"), }}, }, }, @@ -166,7 +166,7 @@ func TestParse(t *testing.T) { Type: comments.InvalidComment, Value: comments.Invalid{Err: comments.CommentError{ Line: 1, - Err: fmt.Errorf("missing rule/owner value"), + Err: errors.New("missing rule/owner value"), }}, }, }, @@ -187,7 +187,7 @@ func TestParse(t *testing.T) { Type: comments.InvalidComment, Value: comments.Invalid{Err: comments.CommentError{ Line: 1, - Err: fmt.Errorf("missing file/disable value"), + Err: errors.New("missing file/disable value"), }}, }, }, @@ -208,7 +208,7 @@ func TestParse(t *testing.T) { Type: comments.InvalidComment, Value: comments.Invalid{Err: comments.CommentError{ Line: 1, - Err: fmt.Errorf("missing disable value"), + Err: errors.New("missing disable value"), }}, }, }, @@ -238,7 +238,7 @@ func TestParse(t *testing.T) { Type: comments.InvalidComment, Value: comments.Invalid{Err: comments.CommentError{ Line: 1, - Err: fmt.Errorf("missing file/snooze value"), + Err: errors.New("missing file/snooze value"), }}, }, }, @@ -250,7 +250,7 @@ func TestParse(t *testing.T) { Type: comments.InvalidComment, Value: comments.Invalid{Err: comments.CommentError{ Line: 1, - Err: fmt.Errorf(`invalid snooze comment, expected '$TIME $MATCH' got "2023-12-31"`), + Err: errors.New(`invalid snooze comment, expected '$TIME $MATCH' got "2023-12-31"`), }}, }, }, @@ -262,7 +262,7 @@ func TestParse(t *testing.T) { Type: comments.InvalidComment, Value: comments.Invalid{Err: comments.CommentError{ Line: 1, - Err: fmt.Errorf(`invalid snooze comment, expected '$TIME $MATCH' got "abc"`), + Err: errors.New(`invalid snooze comment, expected '$TIME $MATCH' got "abc"`), }}, }, }, @@ -298,7 +298,7 @@ func TestParse(t *testing.T) { Type: comments.InvalidComment, Value: comments.Invalid{Err: comments.CommentError{ Line: 1, - Err: fmt.Errorf("missing snooze value"), + Err: errors.New("missing snooze value"), }}, }, }, @@ -310,7 +310,7 @@ func TestParse(t *testing.T) { Type: comments.InvalidComment, Value: comments.Invalid{Err: comments.CommentError{ Line: 1, - Err: fmt.Errorf(`invalid snooze comment, expected '$TIME $MATCH' got "2023-12-31"`), + Err: errors.New(`invalid snooze comment, expected '$TIME $MATCH' got "2023-12-31"`), }}, }, }, @@ -322,7 +322,7 @@ func TestParse(t *testing.T) { Type: comments.InvalidComment, Value: comments.Invalid{Err: comments.CommentError{ Line: 1, - Err: fmt.Errorf(`invalid snooze comment, expected '$TIME $MATCH' got "abc"`), + Err: errors.New(`invalid snooze comment, expected '$TIME $MATCH' got "abc"`), }}, }, }, @@ -370,7 +370,7 @@ func TestParse(t *testing.T) { Type: comments.InvalidComment, Value: comments.Invalid{Err: comments.CommentError{ Line: 1, - Err: fmt.Errorf("missing rule/set value"), + Err: errors.New("missing rule/set value"), }}, }, }, @@ -452,7 +452,7 @@ func TestParse(t *testing.T) { Type: comments.InvalidComment, Value: comments.Invalid{Err: comments.CommentError{ Line: 1, - Err: fmt.Errorf(`unexpected comment suffix: "# pint ignore/file"`), + Err: errors.New(`unexpected comment suffix: "# pint ignore/file"`), }}, }, }, diff --git a/internal/config/config.go b/internal/config/config.go index 755b887d..af2b8365 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -208,7 +208,7 @@ func getContext() *hcl.EvalContext { vars := map[string]cty.Value{} for _, e := range os.Environ() { if k, v, ok := strings.Cut(e, "="); ok { - vars[fmt.Sprintf("ENV_%s", k)] = cty.StringVal(v) + vars["ENV_"+k] = cty.StringVal(v) } } return &hcl.EvalContext{Variables: vars} diff --git a/internal/config/cost.go b/internal/config/cost.go index a2dca4f8..9977d333 100644 --- a/internal/config/cost.go +++ b/internal/config/cost.go @@ -1,7 +1,7 @@ package config import ( - "fmt" + "errors" "github.com/cloudflare/pint/internal/checks" ) @@ -22,13 +22,13 @@ func (cs CostSettings) validate() error { } } if cs.MaxSeries < 0 { - return fmt.Errorf("maxSeries value must be >= 0") + return errors.New("maxSeries value must be >= 0") } if cs.MaxTotalSamples < 0 { - return fmt.Errorf("maxTotalSamples value must be >= 0") + return errors.New("maxTotalSamples value must be >= 0") } if cs.MaxPeakSamples < 0 { - return fmt.Errorf("maxPeakSamples value must be >= 0") + return errors.New("maxPeakSamples value must be >= 0") } if cs.MaxEvaluationDuration != "" { if _, err := parseDuration(cs.MaxEvaluationDuration); err != nil { diff --git a/internal/config/match.go b/internal/config/match.go index 9766a382..ef0c1573 100644 --- a/internal/config/match.go +++ b/internal/config/match.go @@ -2,6 +2,7 @@ package config import ( "context" + "errors" "fmt" "regexp" "strings" @@ -76,7 +77,7 @@ func (m Match) validate(allowEmpty bool) error { } if !allowEmpty && m.Path == "" && m.Name == "" && m.Kind == "" && m.Label == nil && m.Annotation == nil && m.Command == nil && m.For == "" { - return fmt.Errorf("ignore block must have at least one condition") + return errors.New("ignore block must have at least one condition") } return nil diff --git a/internal/config/prometheus.go b/internal/config/prometheus.go index a8469ff8..da0e30a3 100644 --- a/internal/config/prometheus.go +++ b/internal/config/prometheus.go @@ -29,7 +29,7 @@ type TLSConfig struct { func (t TLSConfig) validate() error { if (t.ClientCert != "") != (t.ClientKey != "") { - return fmt.Errorf("clientCert and clientKey must be set together") + return errors.New("clientCert and clientKey must be set together") } return nil } diff --git a/internal/config/repository.go b/internal/config/repository.go index 71ec0d8b..bb4b8418 100644 --- a/internal/config/repository.go +++ b/internal/config/repository.go @@ -1,6 +1,7 @@ package config import ( + "errors" "fmt" "net/url" "os" @@ -21,16 +22,16 @@ func (bb BitBucket) validate() error { return err } if bb.Project == "" { - return fmt.Errorf("project cannot be empty") + return errors.New("project cannot be empty") } if bb.Repository == "" { - return fmt.Errorf("repository cannot be empty") + return errors.New("repository cannot be empty") } if bb.URI == "" { - return fmt.Errorf("uri cannot be empty") + return errors.New("uri cannot be empty") } if bb.MaxComments < 0 { - return fmt.Errorf("maxComments cannot be negative") + return errors.New("maxComments cannot be negative") } return nil } @@ -51,17 +52,17 @@ func (gh GitHub) validate() error { return fmt.Errorf("GITHUB_REPOSITORY is set, but with an invalid repository format: %s", repo) } if gh.Repo == "" && parts[1] == "" { - return fmt.Errorf("repo cannot be empty") + return errors.New("repo cannot be empty") } if gh.Owner == "" && parts[0] == "" { - return fmt.Errorf("owner cannot be empty") + return errors.New("owner cannot be empty") } } else { if gh.Repo == "" { - return fmt.Errorf("repo cannot be empty") + return errors.New("repo cannot be empty") } if gh.Owner == "" { - return fmt.Errorf("owner cannot be empty") + return errors.New("owner cannot be empty") } } if _, err := parseDuration(gh.Timeout); err != nil { @@ -80,7 +81,7 @@ func (gh GitHub) validate() error { } } if gh.MaxComments < 0 { - return fmt.Errorf("maxComments cannot be negative") + return errors.New("maxComments cannot be negative") } return nil } @@ -94,10 +95,10 @@ type GitLab struct { func (gl GitLab) validate() error { if gl.Project <= 0 { - return fmt.Errorf("project must be set") + return errors.New("project must be set") } if gl.MaxComments < 0 { - return fmt.Errorf("maxComments cannot be negative") + return errors.New("maxComments cannot be negative") } return nil } diff --git a/internal/discovery/git_branch_test.go b/internal/discovery/git_branch_test.go index 5dfcbacc..7b0f848e 100644 --- a/internal/discovery/git_branch_test.go +++ b/internal/discovery/git_branch_test.go @@ -22,7 +22,7 @@ func gitCommit(t *testing.T, message string) { t.Setenv("GIT_AUTHOR_EMAIL", "pint@example.com") t.Setenv("GIT_COMMITTER_NAME", "pint") t.Setenv("GIT_COMMITTER_EMAIL", "pint") - _, err := git.RunGit("commit", "-am", fmt.Sprintf("commit %s", message)) + _, err := git.RunGit("commit", "-am", "commit "+message) require.NoError(t, err, "git commit %s", message) } diff --git a/internal/discovery/glob.go b/internal/discovery/glob.go index e0688402..ba6376fa 100644 --- a/internal/discovery/glob.go +++ b/internal/discovery/glob.go @@ -1,6 +1,7 @@ package discovery import ( + "errors" "fmt" "io/fs" "log/slog" @@ -53,7 +54,7 @@ func (f GlobFinder) Find() (entries []Entry, err error) { } if len(paths) == 0 { - return nil, fmt.Errorf("no matching files") + return nil, errors.New("no matching files") } for _, fp := range paths { diff --git a/internal/git/changes.go b/internal/git/changes.go index d536d366..7253106f 100644 --- a/internal/git/changes.go +++ b/internal/git/changes.go @@ -69,7 +69,7 @@ type FileChange struct { } func Changes(cmd CommandRunner, baseBranch string, filter PathFilter) ([]*FileChange, error) { - out, err := cmd("log", "--reverse", "--no-merges", "--first-parent", "--format=%H", "--name-status", fmt.Sprintf("%s..HEAD", baseBranch)) + out, err := cmd("log", "--reverse", "--no-merges", "--first-parent", "--format=%H", "--name-status", baseBranch+"..HEAD") if err != nil { return nil, fmt.Errorf("failed to get the list of modified files from git: %w", err) } diff --git a/internal/git/changes_test.go b/internal/git/changes_test.go index ad9dd564..f90a925a 100644 --- a/internal/git/changes_test.go +++ b/internal/git/changes_test.go @@ -37,7 +37,7 @@ func gitCommit(t *testing.T, message string) { t.Setenv("GIT_AUTHOR_EMAIL", "pint@example.com") t.Setenv("GIT_COMMITTER_NAME", "pint") t.Setenv("GIT_COMMITTER_EMAIL", "pint") - mustRun(t, "commit", "-am", fmt.Sprintf("commit %s", message)) + mustRun(t, "commit", "-am", "commit "+message) } func TestChanges(t *testing.T) { diff --git a/internal/git/git_test.go b/internal/git/git_test.go index 7b330420..083d9f37 100644 --- a/internal/git/git_test.go +++ b/internal/git/git_test.go @@ -145,7 +145,7 @@ func TestCurrentBranch(t *testing.T) { testCases := []testCaseT{ { mock: func(_ ...string) ([]byte, error) { - return nil, fmt.Errorf("mock error") + return nil, errors.New("mock error") }, output: "", shouldError: true, @@ -227,7 +227,7 @@ func TestCommitMessage(t *testing.T) { testCases := []testCaseT{ { mock: func(_ ...string) ([]byte, error) { - return nil, fmt.Errorf("mock error") + return nil, errors.New("mock error") }, output: "", shouldError: true, diff --git a/internal/parser/parser_test.go b/internal/parser/parser_test.go index cda00a20..38cf6326 100644 --- a/internal/parser/parser_test.go +++ b/internal/parser/parser_test.go @@ -1,7 +1,7 @@ package parser_test import ( - "fmt" + "errors" "strconv" "testing" @@ -39,7 +39,7 @@ func TestParse(t *testing.T) { output: []parser.Rule{ { Lines: parser.LineRange{First: 1, Last: 6}, - Error: parser.ParseError{Err: fmt.Errorf("incomplete rule, no alert or record key"), Line: 6}, + Error: parser.ParseError{Err: errors.New("incomplete rule, no alert or record key"), Line: 6}, }, }, }, @@ -48,7 +48,7 @@ func TestParse(t *testing.T) { output: []parser.Rule{ { Lines: parser.LineRange{First: 1, Last: 2}, - Error: parser.ParseError{Err: fmt.Errorf("missing expr key"), Line: 2}, + Error: parser.ParseError{Err: errors.New("missing expr key"), Line: 2}, }, }, }, @@ -57,7 +57,7 @@ func TestParse(t *testing.T) { output: []parser.Rule{ { Lines: parser.LineRange{First: 1, Last: 1}, - Error: parser.ParseError{Err: fmt.Errorf("incomplete rule, no alert or record key"), Line: 1}, + Error: parser.ParseError{Err: errors.New("incomplete rule, no alert or record key"), Line: 1}, }, }, }, @@ -66,7 +66,7 @@ func TestParse(t *testing.T) { output: []parser.Rule{ { Lines: parser.LineRange{First: 1, Last: 1}, - Error: parser.ParseError{Err: fmt.Errorf("missing expr key"), Line: 1}, + Error: parser.ParseError{Err: errors.New("missing expr key"), Line: 1}, }, }, }, @@ -75,7 +75,7 @@ func TestParse(t *testing.T) { output: []parser.Rule{ { Lines: parser.LineRange{First: 1, Last: 2}, - Error: parser.ParseError{Err: fmt.Errorf("got both record and alert keys in a single rule"), Line: 1}, + Error: parser.ParseError{Err: errors.New("got both record and alert keys in a single rule"), Line: 1}, }, }, }, @@ -84,7 +84,7 @@ func TestParse(t *testing.T) { output: []parser.Rule{ { Lines: parser.LineRange{First: 1, Last: 3}, - Error: parser.ParseError{Err: fmt.Errorf("missing expr key"), Line: 1}, + Error: parser.ParseError{Err: errors.New("missing expr key"), Line: 1}, }, }, }, @@ -109,7 +109,7 @@ func TestParse(t *testing.T) { output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 4}, - Error: parser.ParseError{Err: fmt.Errorf("duplicated expr key"), Line: 4}, + Error: parser.ParseError{Err: errors.New("duplicated expr key"), Line: 4}, }, }, }, @@ -122,7 +122,7 @@ func TestParse(t *testing.T) { output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 4}, - Error: parser.ParseError{Err: fmt.Errorf("duplicated record key"), Line: 4}, + Error: parser.ParseError{Err: errors.New("duplicated record key"), Line: 4}, }, }, }, @@ -135,7 +135,7 @@ func TestParse(t *testing.T) { output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 3}, - Error: parser.ParseError{Err: fmt.Errorf("duplicated alert key"), Line: 3}, + Error: parser.ParseError{Err: errors.New("duplicated alert key"), Line: 3}, }, }, }, @@ -149,7 +149,7 @@ func TestParse(t *testing.T) { output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 5}, - Error: parser.ParseError{Err: fmt.Errorf("duplicated for key"), Line: 5}, + Error: parser.ParseError{Err: errors.New("duplicated for key"), Line: 5}, }, }, }, @@ -163,7 +163,7 @@ func TestParse(t *testing.T) { output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 5}, - Error: parser.ParseError{Err: fmt.Errorf("duplicated keep_firing_for key"), Line: 5}, + Error: parser.ParseError{Err: errors.New("duplicated keep_firing_for key"), Line: 5}, }, }, }, @@ -177,7 +177,7 @@ func TestParse(t *testing.T) { output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 5}, - Error: parser.ParseError{Err: fmt.Errorf("duplicated labels key"), Line: 5}, + Error: parser.ParseError{Err: errors.New("duplicated labels key"), Line: 5}, }, }, }, @@ -191,7 +191,7 @@ func TestParse(t *testing.T) { output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 5}, - Error: parser.ParseError{Err: fmt.Errorf("duplicated labels key"), Line: 5}, + Error: parser.ParseError{Err: errors.New("duplicated labels key"), Line: 5}, }, }, }, @@ -205,7 +205,7 @@ func TestParse(t *testing.T) { output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 5}, - Error: parser.ParseError{Err: fmt.Errorf("duplicated annotations key"), Line: 5}, + Error: parser.ParseError{Err: errors.New("duplicated annotations key"), Line: 5}, }, }, }, @@ -214,7 +214,7 @@ func TestParse(t *testing.T) { output: []parser.Rule{ { Lines: parser.LineRange{First: 1, Last: 3}, - Error: parser.ParseError{Err: fmt.Errorf("invalid key(s) found: extra"), Line: 3}, + Error: parser.ParseError{Err: errors.New("invalid key(s) found: extra"), Line: 3}, }, }, }, @@ -1103,7 +1103,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 1, Last: 2}, - Error: parser.ParseError{Err: fmt.Errorf("alert value cannot be empty"), Line: 1}, + Error: parser.ParseError{Err: errors.New("alert value cannot be empty"), Line: 1}, }, }, }, @@ -1112,7 +1112,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 1, Last: 2}, - Error: parser.ParseError{Err: fmt.Errorf("expr value cannot be empty"), Line: 2}, + Error: parser.ParseError{Err: errors.New("expr value cannot be empty"), Line: 2}, }, }, }, @@ -1121,7 +1121,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 1, Last: 1}, - Error: parser.ParseError{Err: fmt.Errorf("missing expr key"), Line: 1}, + Error: parser.ParseError{Err: errors.New("missing expr key"), Line: 1}, }, }, }, @@ -1130,7 +1130,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 1, Last: 2}, - Error: parser.ParseError{Err: fmt.Errorf("record value cannot be empty"), Line: 1}, + Error: parser.ParseError{Err: errors.New("record value cannot be empty"), Line: 1}, }, }, }, @@ -1139,7 +1139,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 1, Last: 2}, - Error: parser.ParseError{Err: fmt.Errorf("expr value cannot be empty"), Line: 2}, + Error: parser.ParseError{Err: errors.New("expr value cannot be empty"), Line: 2}, }, }, }, @@ -1148,7 +1148,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 1, Last: 1}, - Error: parser.ParseError{Err: fmt.Errorf("missing expr key"), Line: 1}, + Error: parser.ParseError{Err: errors.New("missing expr key"), Line: 1}, }, }, }, @@ -1303,7 +1303,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 3}, - Error: parser.ParseError{Err: fmt.Errorf("invalid recording rule name: invalid metric name"), Line: 2}, + Error: parser.ParseError{Err: errors.New("invalid recording rule name: invalid metric name"), Line: 2}, }, }, }, @@ -1317,7 +1317,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 5}, - Error: parser.ParseError{Err: fmt.Errorf("invalid label name: foo bar"), Line: 5}, + Error: parser.ParseError{Err: errors.New("invalid label name: foo bar"), Line: 5}, }, }, }, @@ -1331,7 +1331,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 5}, - Error: parser.ParseError{Err: fmt.Errorf("invalid label name: foo bar"), Line: 5}, + Error: parser.ParseError{Err: errors.New("invalid label name: foo bar"), Line: 5}, }, }, }, @@ -1345,7 +1345,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 5}, - Error: parser.ParseError{Err: fmt.Errorf("invalid label name: {{ $value }}"), Line: 5}, + Error: parser.ParseError{Err: errors.New("invalid label name: {{ $value }}"), Line: 5}, }, }, }, @@ -1359,7 +1359,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 5}, - Error: parser.ParseError{Err: fmt.Errorf("invalid annotation name: foo bar"), Line: 5}, + Error: parser.ParseError{Err: errors.New("invalid annotation name: foo bar"), Line: 5}, }, }, }, @@ -1383,7 +1383,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 5}, - Error: parser.ParseError{Err: fmt.Errorf("invalid annotation name: {{ $value }}"), Line: 5}, + Error: parser.ParseError{Err: errors.New("invalid annotation name: {{ $value }}"), Line: 5}, }, }, }, @@ -1396,7 +1396,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 4}, - Error: parser.ParseError{Err: fmt.Errorf("invalid field 'keep_firing_for' in recording rule"), Line: 4}, + Error: parser.ParseError{Err: errors.New("invalid field 'keep_firing_for' in recording rule"), Line: 4}, }, }, }, @@ -1409,7 +1409,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 4}, - Error: parser.ParseError{Err: fmt.Errorf("invalid field 'for' in recording rule"), Line: 4}, + Error: parser.ParseError{Err: errors.New("invalid field 'for' in recording rule"), Line: 4}, }, }, }, @@ -1423,7 +1423,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 5}, - Error: parser.ParseError{Err: fmt.Errorf("invalid field 'annotations' in recording rule"), Line: 4}, + Error: parser.ParseError{Err: errors.New("invalid field 'annotations' in recording rule"), Line: 4}, }, }, }, @@ -1436,7 +1436,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 3}, - Error: parser.ParseError{Err: fmt.Errorf("record value must be a YAML string, got integer instead"), Line: 2}, + Error: parser.ParseError{Err: errors.New("record value must be a YAML string, got integer instead"), Line: 2}, }, }, }, @@ -1448,7 +1448,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 3}, - Error: parser.ParseError{Err: fmt.Errorf("alert value must be a YAML string, got integer instead"), Line: 2}, + Error: parser.ParseError{Err: errors.New("alert value must be a YAML string, got integer instead"), Line: 2}, }, }, }, @@ -1460,7 +1460,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 3}, - Error: parser.ParseError{Err: fmt.Errorf("expr value must be a YAML string, got integer instead"), Line: 3}, + Error: parser.ParseError{Err: errors.New("expr value must be a YAML string, got integer instead"), Line: 3}, }, }, }, @@ -1473,7 +1473,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 4}, - Error: parser.ParseError{Err: fmt.Errorf("for value must be a YAML string, got integer instead"), Line: 4}, + Error: parser.ParseError{Err: errors.New("for value must be a YAML string, got integer instead"), Line: 4}, }, }, }, @@ -1486,7 +1486,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 4}, - Error: parser.ParseError{Err: fmt.Errorf("keep_firing_for value must be a YAML string, got integer instead"), Line: 4}, + Error: parser.ParseError{Err: errors.New("keep_firing_for value must be a YAML string, got integer instead"), Line: 4}, }, }, }, @@ -1499,7 +1499,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 4}, - Error: parser.ParseError{Err: fmt.Errorf("labels value must be a YAML mapping, got list instead"), Line: 4}, + Error: parser.ParseError{Err: errors.New("labels value must be a YAML mapping, got list instead"), Line: 4}, }, }, }, @@ -1513,7 +1513,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 5}, - Error: parser.ParseError{Err: fmt.Errorf("annotations value must be a YAML mapping, got list instead"), Line: 5}, + Error: parser.ParseError{Err: errors.New("annotations value must be a YAML mapping, got list instead"), Line: 5}, }, }, }, @@ -1529,7 +1529,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 7}, - Error: parser.ParseError{Err: fmt.Errorf("labels foo value must be a YAML string, got integer instead"), Line: 5}, + Error: parser.ParseError{Err: errors.New("labels foo value must be a YAML string, got integer instead"), Line: 5}, }, }, }, @@ -1545,7 +1545,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 7}, - Error: parser.ParseError{Err: fmt.Errorf("annotations bar value must be a YAML string, got integer instead"), Line: 7}, + Error: parser.ParseError{Err: errors.New("annotations bar value must be a YAML string, got integer instead"), Line: 7}, }, }, }, @@ -1558,7 +1558,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 4}, - Error: parser.ParseError{Err: fmt.Errorf("labels value must be a YAML mapping, got integer instead"), Line: 4}, + Error: parser.ParseError{Err: errors.New("labels value must be a YAML mapping, got integer instead"), Line: 4}, }, }, }, @@ -1571,7 +1571,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 4}, - Error: parser.ParseError{Err: fmt.Errorf("labels value must be a YAML mapping, got bool instead"), Line: 4}, + Error: parser.ParseError{Err: errors.New("labels value must be a YAML mapping, got bool instead"), Line: 4}, }, }, }, @@ -1614,7 +1614,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 3}, - Error: parser.ParseError{Err: fmt.Errorf("record value must be a YAML string, got bool instead"), Line: 2}, + Error: parser.ParseError{Err: errors.New("record value must be a YAML string, got bool instead"), Line: 2}, }, }, }, @@ -1627,7 +1627,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 4}, - Error: parser.ParseError{Err: fmt.Errorf("record value must be a YAML string, got mapping instead"), Line: 3}, + Error: parser.ParseError{Err: errors.New("record value must be a YAML string, got mapping instead"), Line: 3}, }, }, }, @@ -1640,7 +1640,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 4}, - Error: parser.ParseError{Err: fmt.Errorf("labels value must be a YAML mapping, got string instead"), Line: 4}, + Error: parser.ParseError{Err: errors.New("labels value must be a YAML mapping, got string instead"), Line: 4}, }, }, }, @@ -1653,7 +1653,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 4}, - Error: parser.ParseError{Err: fmt.Errorf("labels value must be a YAML mapping, got binary data instead"), Line: 4}, + Error: parser.ParseError{Err: errors.New("labels value must be a YAML mapping, got binary data instead"), Line: 4}, }, }, }, @@ -1666,7 +1666,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 4}, - Error: parser.ParseError{Err: fmt.Errorf("for value must be a YAML string, got float instead"), Line: 4}, + Error: parser.ParseError{Err: errors.New("for value must be a YAML string, got float instead"), Line: 4}, }, }, }, @@ -1679,7 +1679,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 4}, - Error: parser.ParseError{Err: fmt.Errorf("labels value must be a YAML mapping, got garbage instead"), Line: 4}, + Error: parser.ParseError{Err: errors.New("labels value must be a YAML mapping, got garbage instead"), Line: 4}, }, }, }, @@ -1700,7 +1700,7 @@ data: output: []parser.Rule{ { Lines: parser.LineRange{First: 2, Last: 4}, - Error: parser.ParseError{Err: fmt.Errorf("labels value must be a YAML mapping, got string instead"), Line: 4}, + Error: parser.ParseError{Err: errors.New("labels value must be a YAML mapping, got string instead"), Line: 4}, }, }, }, @@ -1730,7 +1730,7 @@ data: }, { Lines: parser.LineRange{First: 5, Last: 5}, - Error: parser.ParseError{Err: fmt.Errorf("incomplete rule, no alert or record key"), Line: 5}, + Error: parser.ParseError{Err: errors.New("incomplete rule, no alert or record key"), Line: 5}, }, }, }, @@ -1761,7 +1761,7 @@ data: }, { Lines: parser.LineRange{First: 5, Last: 7}, - Error: parser.ParseError{Err: fmt.Errorf("duplicated expr key"), Line: 7}, + Error: parser.ParseError{Err: errors.New("duplicated expr key"), Line: 7}, }, }, }, diff --git a/internal/promapi/metadata.go b/internal/promapi/metadata.go index b4c031f5..bef10ed6 100644 --- a/internal/promapi/metadata.go +++ b/internal/promapi/metadata.go @@ -76,7 +76,7 @@ func (q metadataQuery) CacheTTL() time.Duration { func (p *Prometheus) Metadata(ctx context.Context, metric string) (*MetadataResult, error) { slog.Debug("Scheduling Prometheus metrics metadata query", slog.String("uri", p.safeURI), slog.String("metric", metric)) - key := fmt.Sprintf("/api/v1/metadata/%s", metric) + key := "/api/v1/metadata/" + metric p.locker.lock(key) defer p.locker.unlock(key) diff --git a/internal/promapi/query.go b/internal/promapi/query.go index 92b3876a..5fcc6ee6 100644 --- a/internal/promapi/query.go +++ b/internal/promapi/query.go @@ -81,7 +81,7 @@ func (q instantQuery) CacheTTL() time.Duration { func (p *Prometheus) Query(ctx context.Context, expr string) (*QueryResult, error) { slog.Debug("Scheduling prometheus query", slog.String("uri", p.safeURI), slog.String("query", expr)) - key := fmt.Sprintf("/api/v1/query/%s", expr) + key := "/api/v1/query/" + expr p.locker.lock(key) defer p.locker.unlock(key) @@ -184,7 +184,7 @@ func streamSamples(r io.Reader) (samples []Sample, stats QueryStats, err error) } if resultType != "vector" { - return nil, stats, APIError{Status: status, ErrorType: v1.ErrBadResponse, Err: fmt.Sprintf("invalid result type, expected vector, got %s", resultType)} + return nil, stats, APIError{Status: status, ErrorType: v1.ErrBadResponse, Err: "invalid result type, expected vector, got " + resultType} } return samples, stats, nil diff --git a/internal/promapi/range.go b/internal/promapi/range.go index 7d8d966a..3d19fd78 100644 --- a/internal/promapi/range.go +++ b/internal/promapi/range.go @@ -347,7 +347,7 @@ func streamSampleStream(r io.Reader, step time.Duration) (dst MetricTimeRanges, } if resultType != "matrix" { - return nil, stats, APIError{Status: status, ErrorType: v1.ErrBadResponse, Err: fmt.Sprintf("invalid result type, expected matrix, got %s", resultType)} + return nil, stats, APIError{Status: status, ErrorType: v1.ErrBadResponse, Err: "invalid result type, expected matrix, got " + resultType} } return dst, stats, nil diff --git a/internal/reporter/bitbucket.go b/internal/reporter/bitbucket.go index e38db1cf..594e12a6 100644 --- a/internal/reporter/bitbucket.go +++ b/internal/reporter/bitbucket.go @@ -1,6 +1,7 @@ package reporter import ( + "errors" "fmt" "log/slog" "time" @@ -119,7 +120,7 @@ func (bb BitBucketReporter) Submit(summary Summary) (err error) { } if summary.HasFatalProblems() { - return fmt.Errorf("fatal error(s) reported") + return errors.New("fatal error(s) reported") } return nil diff --git a/internal/reporter/bitbucket_api.go b/internal/reporter/bitbucket_api.go index fe8d7ded..d920c9bf 100644 --- a/internal/reporter/bitbucket_api.go +++ b/internal/reporter/bitbucket_api.go @@ -285,7 +285,7 @@ func (bb bitBucketAPI) request(method, path string, body io.Reader) ([]byte, err return nil, err } req.Header.Set("Content-Type", "application/json") - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", bb.authToken)) + req.Header.Set("Authorization", "Bearer "+bb.authToken) netClient := &http.Client{ Timeout: bb.timeout, @@ -345,7 +345,7 @@ func (bb bitBucketAPI) createReport(summary Summary, commit string) error { } payload, _ := json.Marshal(BitBucketReport{ - Title: fmt.Sprintf("pint %s", bb.pintVersion), + Title: "pint " + bb.pintVersion, Result: result, Reporter: "Prometheus rule linter", Details: BitBucketDescription, diff --git a/internal/reporter/bitbucket_test.go b/internal/reporter/bitbucket_test.go index 9fd58252..096e79d0 100644 --- a/internal/reporter/bitbucket_test.go +++ b/internal/reporter/bitbucket_test.go @@ -2009,7 +2009,7 @@ func TestBitBucketReporter(t *testing.T) { assert.NoError(t, err) default: w.WriteHeader(500) - _, _ = w.Write([]byte(fmt.Sprintf("Unhandled path: %s", r.URL.Path))) + _, _ = w.Write([]byte("Unhandled path: " + r.URL.Path)) t.Errorf("Unhandled path: %s", r.URL.Path) } })) diff --git a/internal/reporter/github_test.go b/internal/reporter/github_test.go index e295c8fc..1d9d636e 100644 --- a/internal/reporter/github_test.go +++ b/internal/reporter/github_test.go @@ -484,7 +484,7 @@ filename %s return } token := auth[0] - if token != fmt.Sprintf("Bearer %s", tc.token) { + if token != "Bearer "+tc.token { w.WriteHeader(500) _, _ = w.Write([]byte("Invalid token")) t.Fatalf("got a request with invalid token (got %s)", token) diff --git a/internal/reporter/gitlab.go b/internal/reporter/gitlab.go index 7f3729c8..6a06d16e 100644 --- a/internal/reporter/gitlab.go +++ b/internal/reporter/gitlab.go @@ -255,7 +255,7 @@ func (gl GitLabReporter) IsEqual(existing ExistingCommentV2, pending PendingComm func (gl GitLabReporter) CanCreate(done int) (bool, error) { if done >= gl.maxComments { - return false, fmt.Errorf(tooManyCommentsMsg(done, gl.maxComments)) + return false, errors.New(tooManyCommentsMsg(done, gl.maxComments)) } return true, nil }