Maintenance: upgrade linting setup

- Update Golangci-lint to v1.40.1
- Add more linters
- Remove rules from .golangci.yml in favor of inline nolint comments
- Fix linting errors
This commit is contained in:
Quentin McGaw
2021-05-19 01:00:42 +00:00
parent 803232e670
commit 844904aa7b
12 changed files with 46 additions and 81 deletions

View File

@@ -6,18 +6,6 @@ linters-settings:
issues:
exclude-rules:
- path: cmd/
text: buildInfo is a global variable
linters:
- gochecknoglobals
- path: cmd/
text: commit is a global variable
linters:
- gochecknoglobals
- path: cmd/
text: buildDate is a global variable
linters:
- gochecknoglobals
- path: cmd/updater/main.go
text: "mnd: Magic number: 4, in <argument> detected"
linters:
@@ -26,17 +14,6 @@ issues:
text: "mnd: Magic number: 2, in <argument> detected"
linters:
- gomnd
- path: internal/regex/regex.go
text: "regexpMust: for const patterns like "
linters:
- gocritic
- path: internal/settings/
linters:
- maligned
- dupl
- path: pkg/publicip/.*_test.go
linters:
- dupl
linters:
disable-all: true
@@ -59,32 +36,36 @@ linters:
- godot
- goheader
- goimports
- golint
- gomnd
- goprintffuncname
- gosec
# - goerr113 # TODO
- gosimple
- govet
- importas
- ineffassign
- interfacer
- lll
- maligned
- misspell
- nakedret
- nestif
- nilerr
- noctx
- nolintlint
- prealloc
- predeclared
- rowserrcheck
- scopelint
- exportloopref
- sqlclosecheck
- staticcheck
- structcheck
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- varcheck
- wastedassign
- whitespace
run:

View File

@@ -25,7 +25,7 @@ ENV CGO_ENABLED=1
RUN apk --update add g++
FROM --platform=$BUILDPLATFORM base AS lint
ARG GOLANGCI_LINT_VERSION=v1.37.0
ARG GOLANGCI_LINT_VERSION=v1.40.1
RUN wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | \
sh -s -- -b /usr/local/bin ${GOLANGCI_LINT_VERSION}
COPY .golangci.yml ./

View File

@@ -30,6 +30,7 @@ import (
"github.com/qdm12/golibs/network/connectivity"
)
//nolint:gochecknoglobals
var (
buildInfo models.BuildInformation
version = "unknown"

View File

@@ -66,10 +66,8 @@ func extractAllSettings(jsonBytes []byte) (allSettings []settings.Settings, warn
if err := json.Unmarshal(jsonBytes, &rawConfig); err != nil {
return nil, nil, err
}
matcher, err := regex.NewMatcher()
if err != nil {
return nil, nil, err
}
matcher := regex.NewMatcher()
for i, common := range config.CommonSettings {
newSettings, newWarnings, err := makeSettingsFromObject(common, rawConfig.Settings[i], matcher)
warnings = append(warnings, newWarnings...)

View File

@@ -74,7 +74,7 @@ func (db *Database) Check() error {
case event.IP == nil:
return fmt.Errorf("IP %d of %d is empty for record %s", i+1, len(record.Events), record)
case event.Time.IsZero():
return fmt.Errorf("Time of IP %d of %d is empty for record %s", i+1, len(record.Events), record)
return fmt.Errorf("time of IP %d of %d is empty for record %s", i+1, len(record.Events), record)
}
}
}

View File

@@ -19,45 +19,30 @@ type matcher struct {
cloudflareUserServiceKey, dnsOMaticUsername, dnsOMaticPassword, gandiKey *regexp.Regexp
}
func NewMatcher() (m Matcher, err error) {
matcher := &matcher{}
matcher.gandiKey, err = regexp.Compile(`^[A-Za-z0-9]{24}$`)
if err != nil {
return nil, err
var (
gandiKey = regexp.MustCompile(`^[A-Za-z0-9]{24}$`)
goDaddyKey = regexp.MustCompile(`^[A-Za-z0-9]{8,14}\_[A-Za-z0-9]{21,22}$`)
duckDNSToken = regexp.MustCompile(`^[a-f0-9]{8}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{12}$`)
namecheapPassword = regexp.MustCompile(`^[a-f0-9]{32}$`)
dreamhostKey = regexp.MustCompile(`^[a-zA-Z0-9]{16}$`)
cloudflareKey = regexp.MustCompile(`^[a-zA-Z0-9]+$`)
cloudflareUserServiceKey = regexp.MustCompile(`^v1\.0.+$`)
dnsOMaticUsername = regexp.MustCompile(`^[a-zA-Z0-9._-]{3,25}$`)
dnsOMaticPassword = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]{5,19}$`)
)
func NewMatcher() Matcher {
return &matcher{
gandiKey: gandiKey,
goDaddyKey: goDaddyKey,
duckDNSToken: duckDNSToken,
namecheapPassword: namecheapPassword,
dreamhostKey: dreamhostKey,
cloudflareKey: cloudflareKey,
cloudflareUserServiceKey: cloudflareUserServiceKey,
dnsOMaticUsername: dnsOMaticUsername,
dnsOMaticPassword: dnsOMaticPassword,
}
matcher.goDaddyKey, err = regexp.Compile(`^[A-Za-z0-9]{8,14}\_[A-Za-z0-9]{21,22}$`)
if err != nil {
return nil, err
}
matcher.duckDNSToken, err = regexp.Compile(`^[a-f0-9]{8}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{12}$`)
if err != nil {
return nil, err
}
matcher.namecheapPassword, err = regexp.Compile(`^[a-f0-9]{32}$`)
if err != nil {
return nil, err
}
matcher.dreamhostKey, err = regexp.Compile(`^[a-zA-Z0-9]{16}$`)
if err != nil {
return nil, err
}
matcher.cloudflareKey, err = regexp.Compile(`^[a-zA-Z0-9]+$`)
if err != nil {
return nil, err
}
matcher.cloudflareUserServiceKey, err = regexp.Compile(`^v1\.0.+$`)
if err != nil {
return nil, err
}
matcher.dnsOMaticUsername, err = regexp.Compile(`^[a-zA-Z0-9._-]{3,25}$`)
if err != nil {
return nil, err
}
matcher.dnsOMaticPassword, err = regexp.Compile(`^[a-zA-Z0-9][a-zA-Z0-9._-]{5,19}$`)
if err != nil {
return nil, err
}
return matcher, nil
}
func (m *matcher) GandiKey(s string) bool { return m.gandiKey.MatchString(s) }

View File

@@ -158,7 +158,7 @@ func (d *dnsomatic) Update(ctx context.Context, client *http.Client, ip net.IP)
if strings.Contains(s, "nochg") || strings.Contains(s, "good") {
ipsV4 := verification.NewVerifier().SearchIPv4(s)
ipsV6 := verification.NewVerifier().SearchIPv6(s)
ips := append(ipsV4, ipsV6...)
ips := append(ipsV4, ipsV6...) //nolint:gocritic
if ips == nil {
return nil, fmt.Errorf("no IP address in response")
}

View File

@@ -198,7 +198,7 @@ func (d *dreamhost) getRecords(ctx context.Context, client *http.Client) (
return records, nil
}
func (d *dreamhost) removeRecord(ctx context.Context, client *http.Client, ip net.IP) error {
func (d *dreamhost) removeRecord(ctx context.Context, client *http.Client, ip net.IP) error { //nolint:dupl
recordType := A
if ip.To4() == nil {
recordType = AAAA
@@ -245,7 +245,7 @@ func (d *dreamhost) removeRecord(ctx context.Context, client *http.Client, ip ne
return nil
}
func (d *dreamhost) createRecord(ctx context.Context, client *http.Client, ip net.IP) error {
func (d *dreamhost) createRecord(ctx context.Context, client *http.Client, ip net.IP) error { //nolint:dupl
recordType := A
if ip.To4() == nil {
recordType = AAAA

View File

@@ -145,7 +145,7 @@ func (g *google) Update(ctx context.Context, client *http.Client, ip net.IP) (ne
if strings.Contains(s, "nochg") || strings.Contains(s, "good") {
ipsV4 := verification.NewVerifier().SearchIPv4(s)
ipsV6 := verification.NewVerifier().SearchIPv6(s)
ips := append(ipsV4, ipsV6...)
ips := append(ipsV4, ipsV6...) //nolint:gocritic
if len(ips) == 0 {
return nil, ErrNoResultReceived
}

View File

@@ -130,7 +130,7 @@ func (h *he) Update(ctx context.Context, client *http.Client, ip net.IP) (newIP
verifier := verification.NewVerifier()
ipsV4 := verifier.SearchIPv4(s)
ipsV6 := verifier.SearchIPv6(s)
ips := append(ipsV4, ipsV6...)
ips := append(ipsV4, ipsV6...) //nolint:gocritic
if ips == nil {
return nil, ErrNoResultReceived
}

View File

@@ -17,7 +17,7 @@ import (
func uint32Ptr(n uint32) *uint32 { return &n }
func Test_fetcher_IP(t *testing.T) {
func Test_fetcher_IP(t *testing.T) { //nolint:dupl
t.Parallel()
ctx := context.Background()
@@ -61,7 +61,7 @@ func Test_fetcher_IP(t *testing.T) {
assert.Equal(t, expectedFetcher, initialFetcher)
}
func Test_fetcher_IP4(t *testing.T) {
func Test_fetcher_IP4(t *testing.T) { //nolint:dupl
t.Parallel()
ctx := context.Background()

View File

@@ -20,7 +20,7 @@ func Test_newDefaultSettings(t *testing.T) {
assert.GreaterOrEqual(t, int(settings.timeout), int(time.Millisecond))
}
func Test_SetProvidersIP(t *testing.T) {
func Test_SetProvidersIP(t *testing.T) { //nolint:dupl
t.Parallel()
testCases := map[string]struct {
@@ -72,7 +72,7 @@ func Test_SetProvidersIP(t *testing.T) {
}
}
func Test_SetProvidersIP4(t *testing.T) {
func Test_SetProvidersIP4(t *testing.T) { //nolint:dupl
t.Parallel()
testCases := map[string]struct {
@@ -124,7 +124,7 @@ func Test_SetProvidersIP4(t *testing.T) {
}
}
func Test_SetProvidersIP6(t *testing.T) {
func Test_SetProvidersIP6(t *testing.T) { //nolint:dupl
t.Parallel()
testCases := map[string]struct {