Skip to content

Commit

Permalink
chore(golangci-lint): add new linters and reformat code for compliance
Browse files Browse the repository at this point in the history
- godot
- perfsprint
- whitespace
- unconvert
- unused
- usestdlibvars
  • Loading branch information
JosephKav committed Jan 26, 2025
1 parent 26bb616 commit a7a53fe
Show file tree
Hide file tree
Showing 40 changed files with 140 additions and 120 deletions.
16 changes: 15 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@ linters:
- errorlint
- gci
- gocritic
- godot
- goimports
- misspell
- perfsprint
- typecheck
- whitespace
- wrapcheck
- unconvert
- unused
- usestdlibvars

linters-settings:
gci:
sections:
- standard
- default
- prefix(github.com/release-argus/Argus)
goimports:
local-prefixes: github.com/release-argus/Argus
local-prefixes: github.com/release-argus/Argus
perfsprint:
# Optimises into `err.Error()` even if it is only equivalent for non-nil errors.
err-error: true
3 changes: 1 addition & 2 deletions config/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ func (c *Config) Save() {
if indents == 1 &&
strings.HasSuffix(line, ":") &&
parentKey[0] == "service" {

// First service will be on 1 because we remove items and decrement
// currentOrderIndexEnd[currentServiceNumber]. So we want to know when the service
// has started so that the decrements are direct to the service.
Expand Down Expand Up @@ -278,7 +277,7 @@ func (c *Config) Save() {
c.File, err),
logutil.LogFrom{}, err != nil)
logutil.Log.Info(
fmt.Sprintf("Saved service updates to %s", c.File),
"Saved service updates to "+c.File,
logutil.LogFrom{}, true)
}

Expand Down
2 changes: 1 addition & 1 deletion config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (s *Settings) String(prefix string) string {

// SettingsBase for the binary.
//
// (Used in Defaults)
// (Used in Defaults).
type SettingsBase struct {
Log LogSettings `yaml:"log,omitempty"` // Log settings
Data DataSettings `yaml:"data,omitempty"` // Data settings
Expand Down
4 changes: 3 additions & 1 deletion notify/shoutrrr/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// Package shoutrrr provides the shoutrrr notification service to services.
package shoutrrr

import "net/http"

// notifyDefaultOptions are the default options for all notifiers.
func notifyDefaultOptions() map[string]string {
return map[string]string{
Expand Down Expand Up @@ -143,7 +145,7 @@ func (s *SliceDefaults) Default() {
"contenttype": "application/json",
"disabletls": "no",
"messagekey": "message",
"requestmethod": "POST",
"requestmethod": http.MethodPost,
"titlekey": "title"})
newSlice["shoutrrr"] = NewDefaults(
"",
Expand Down
3 changes: 2 additions & 1 deletion notify/shoutrrr/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package shoutrrr

import (
"errors"
"fmt"

"github.com/release-argus/Argus/notify/shoutrrr/types"
Expand Down Expand Up @@ -50,7 +51,7 @@ func FromPayload(
) (*Shoutrrr, string, error) {
// No `name` or `name_previous`.
if payload.NamePrevious == "" && payload.Name == "" {
return nil, "", fmt.Errorf("name and/or name_previous are required")
return nil, "", errors.New("name and/or name_previous are required")
}

name := util.FirstNonDefault(payload.Name, payload.NamePrevious)
Expand Down
5 changes: 2 additions & 3 deletions notify/shoutrrr/shoutrrr.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ func (s *Shoutrrr) BuildURL() (url string) {
case "googlechat":
url = s.GetURLField("raw")
// googlechat://url
url = fmt.Sprintf("googlechat://%s",
url)
url = "googlechat://" + url
case "ifttt":
// ifttt://webhookid/?events=event1,event2
url = fmt.Sprintf("ifttt://%s/?events=%s",
Expand Down Expand Up @@ -448,7 +447,7 @@ func (s *Shoutrrr) send(
func() error {
err := sender.Send(message, params)
if failed := s.parseSend(err, combinedErrs, serviceName, logFrom); failed {
return fmt.Errorf("send failed")
return errors.New("send failed")
}
return nil
},
Expand Down
1 change: 1 addition & 0 deletions notify/shoutrrr/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

goshoutrrr "github.com/containrrr/shoutrrr"

"github.com/release-argus/Argus/util"
)

Expand Down
3 changes: 2 additions & 1 deletion service/deployed_version/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package deployedver

import (
"crypto/tls"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -213,7 +214,7 @@ func (l *Lookup) httpRequest(logFrom logutil.LogFrom) ([]byte, error) {
if err != nil {
// Don't crash on invalid certs.
if strings.Contains(err.Error(), "x509") {
err = fmt.Errorf("x509 (certificate invalid)")
err = errors.New("x509 (certificate invalid)")
logutil.Log.Warn(err, logFrom, true)
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion service/deployed_version/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (l *Lookup) Refresh(
semanticVersioning *string,
) (string, error) {
if l == nil {
return "", fmt.Errorf("lookup is nil")
return "", errors.New("lookup is nil")
}
logFrom := logutil.LogFrom{Primary: "deployed_version/refresh", Secondary: *serviceID}

Expand Down
3 changes: 2 additions & 1 deletion service/deployed_version/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ package deployedver
import (
"encoding/json"
"fmt"
"net/http"

opt "github.com/release-argus/Argus/service/option"
"github.com/release-argus/Argus/service/status"
"github.com/release-argus/Argus/util"
)

var (
supportedTypes = []string{"GET", "POST"}
supportedTypes = []string{http.MethodGet, http.MethodPost}
)

// Base is the base struct for the Lookup struct.
Expand Down
5 changes: 3 additions & 2 deletions service/deployed_version/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package deployedver
import (
"errors"
"fmt"
"net/http"
"regexp"
"strings"

Expand All @@ -34,14 +35,14 @@ func (l *Lookup) CheckValues(prefix string) error {
// Method
l.Method = strings.ToUpper(l.Method)
if l.Method == "" {
l.Method = "GET"
l.Method = http.MethodGet
} else if !util.Contains(supportedTypes, l.Method) {
errs = append(errs,
fmt.Errorf("%smethod: %q <invalid> (only [%s] are allowed)",
prefix, l.Method, strings.Join(supportedTypes, ", ")))
}
// Body unused in GET, ensure it is empty.
if l.Method == "GET" {
if l.Method == http.MethodGet {
l.Body = ""
}

Expand Down
27 changes: 14 additions & 13 deletions service/deployed_version/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package deployedver

import (
"net/http"
"strings"
"testing"

Expand All @@ -40,7 +41,7 @@ func TestLookup_CheckValues(t *testing.T) {
},
"valid service": {
errRegex: `^$`,
method: "GET",
method: http.MethodGet,
url: "https://example.com",
regex: `[0-9.]+`,
defaults: &Defaults{},
Expand All @@ -57,7 +58,7 @@ func TestLookup_CheckValues(t *testing.T) {
},
"method - valid": {
errRegex: `^$`,
method: "GET",
method: http.MethodGet,
url: "https://example.com",
},
"method - case insensitive": {
Expand All @@ -67,53 +68,53 @@ func TestLookup_CheckValues(t *testing.T) {
},
"url - empty string": {
errRegex: `url: <required>`,
method: "GET",
method: http.MethodGet,
url: "",
defaults: &Defaults{},
},
"body - removed for GET": {
errRegex: `^$`,
method: "GET",
method: http.MethodGet,
url: "https://example.com",
body: "foo",
defaults: &Defaults{},
},
"body - not removed for POST": {
errRegex: `^$`,
method: "POST",
method: http.MethodPost,
url: "https://example.com",
body: "foo",
defaults: &Defaults{},
},
"json - invalid, string in square brackets": {
errRegex: `json: .* <invalid>`,
method: "GET",
method: http.MethodGet,
json: "foo[bar]",
defaults: &Defaults{},
},
"regex - invalid": {
errRegex: `regex: .* <invalid>`,
method: "GET",
method: http.MethodGet,
regex: `[0-`,
defaults: &Defaults{},
},
"regexTemplate - with no regex": {
method: "GET",
method: http.MethodGet,
url: "https://example.com",
errRegex: `^$`,
regexTemplate: "$1.$2.$3",
defaults: &Defaults{},
},
"all errs": {
errRegex: `url: <required>`,
method: "GET",
method: http.MethodGet,
url: "",
regex: `[0-`,
defaults: &Defaults{},
},
"no url doesn't fail for Lookup Defaults": {
errRegex: `^$`,
method: "GET",
method: http.MethodGet,
url: "",
defaults: nil,
},
Expand Down Expand Up @@ -159,17 +160,17 @@ func TestLookup_CheckValues(t *testing.T) {
t.Fatalf("RegexTemplate should be nil when Regex is empty")
}
// AND Body is empty when Method is GET
if lookup.Method == "GET" && lookup.Body != "" {
if lookup.Method == http.MethodGet && lookup.Body != "" {
t.Fatalf("Body should be nil when Method is GET")
}
// AND Body is kept when Method is POST
if lookup.Method == "POST" && hadBody != "" && lookup.Body == "" {
if lookup.Method == http.MethodPost && hadBody != "" && lookup.Body == "" {
t.Fatalf("Body should be kept when Method is POST")
}
// AND Method is uppercased
wantMethod := strings.ToUpper(tc.method)
if wantMethod == "" {
wantMethod = "GET"
wantMethod = http.MethodGet
}
if lookup.Method != wantMethod {
t.Fatalf("Method should be uppercased:\nwant: %q\ngot: %q",
Expand Down
1 change: 0 additions & 1 deletion service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ func (s *Service) Init(
&s.Options,
&s.Status,
&s.Defaults.DeployedVersionLookup, &s.HardDefaults.DeployedVersionLookup)

}

// initMetrics will initialise the Prometheus metrics for the Service.
Expand Down
8 changes: 4 additions & 4 deletions service/latest_version/filter/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (d *DockerCheck) CheckValues(prefix string) error {
prefix, d.Image))
// e.g. prometheus = library/prometheus on the docker hub api.
case d.Type == "hub" && strings.Count(d.Image, "/") == 0:
d.Image = fmt.Sprintf("library/%s", d.Image)
d.Image = "library/" + d.Image
}

// Tag
Expand All @@ -307,7 +307,7 @@ func (d *DockerCheck) CheckValues(prefix string) error {
errs = append(errs, fmt.Errorf("%stag: %q <invalid> (didn't pass templating)",
prefix, d.Tag))
default:
if _, err := net_url.Parse(fmt.Sprintf("https://example.com/%s", d.Tag)); err != nil {
if _, err := net_url.Parse("https://example.com/" + d.Tag); err != nil {
errs = append(errs, fmt.Errorf("%stag: %q <invalid> (invalid for URL formatting)",
prefix, d.Tag))
}
Expand Down Expand Up @@ -339,7 +339,7 @@ func (d *DockerCheck) checkToken() error {
return fmt.Errorf("token: <required> (token for %s)",
username)
} else if username == "" && token != "" {
return fmt.Errorf("username: <required> (token is for who?)")
return errors.New("username: <required> (token is for who?)")
}
case "quay", "ghcr":
// Token not required.
Expand Down Expand Up @@ -392,7 +392,7 @@ func (d *DockerCheck) getToken() string {
return util.EvalEnvVars(d.Defaults.getToken(d.GetType()))
}

// getToken returns the token as is,
// getToken returns the token as is.
func (d *DockerCheckDefaults) getToken(dType string) string {
if d == nil {
return ""
Expand Down
3 changes: 2 additions & 1 deletion service/latest_version/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package latestver

import (
"encoding/json"
"errors"
"fmt"
"strings"

Expand All @@ -33,7 +34,7 @@ func Refresh(
semanticVersioning *string, // nil, "true", "false", "null" (unchanged, true, false, default).
) (string, bool, error) {
if lookup == nil {
return "", false, fmt.Errorf("lookup is nil")
return "", false, errors.New("lookup is nil")
}

logFrom := logutil.LogFrom{Primary: "latest_version/refresh", Secondary: *lookup.GetStatus().ServiceID}
Expand Down
2 changes: 1 addition & 1 deletion service/latest_version/types/github/gets.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (l *Lookup) ServiceURL(ignoreWebURL bool) (serviceURL string) {
// GitHub service. Get the non-API URL.
// If "owner/repo" rather than a full path.
if strings.Count(serviceURL, "/") == 1 {
serviceURL = fmt.Sprintf("https://github.com/%s", serviceURL)
serviceURL = "https://github.com/" + serviceURL
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion service/latest_version/types/github/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (l *Lookup) createRequest(logFrom logutil.LogFrom) (*http.Request, error) {
req.Header.Set("Connection", "close")
// Access Token.
if accessToken := l.accessToken(); accessToken != "" {
req.Header.Set("Authorization", fmt.Sprintf("token %s", accessToken))
req.Header.Set("Authorization", "token "+accessToken)
}
// Conditional requests - https://docs.github.com/en/rest/overview/resources-in-the-rest-api#conditional-requests.
if eTag := l.data.ETag(); eTag != "" {
Expand Down
2 changes: 1 addition & 1 deletion service/latest_version/types/github/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func TestGetResponse_ReadError(t *testing.T) {
t.Cleanup(func() { server.Close() })

// AND a request to the mock server's URL.
req, err := http.NewRequest("GET", server.URL, nil)
req, err := http.NewRequest(http.MethodGet, server.URL, nil)
if err != nil {
t.Fatalf("github.Lookup.getResponse() could not create request: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion service/latest_version/types/web/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (l *Lookup) httpRequest(logFrom logutil.LogFrom) ([]byte, error) {
if err != nil {
// Don't crash on invalid certs.
if strings.Contains(err.Error(), "x509") {
err = fmt.Errorf("x509 (certificate invalid)")
err = errors.New("x509 (certificate invalid)")
logutil.Log.Warn(err, logFrom, true)
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion service/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func FromPayload(
if err != nil {
logutil.Log.Error(err, logFrom, true)
logutil.Log.Verbose(
fmt.Sprintf("Payload: %s", buf.String()),
"Payload: "+buf.String(),
logFrom, true)
return nil, err //nolint:wrapcheck
}
Expand Down
Loading

0 comments on commit a7a53fe

Please sign in to comment.