Files
AdGuardHome/scripts/make/go-lint.sh
T
Fedor Setrakov 594849b5af Pull request 2506: AGDNS-3334-imp-home
Squashed commit of the following:

commit ae110ad162ea7f30854750255a378f3b6484de94
Merge: 965d052f9 3bebde610
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Sat Nov 1 13:05:43 2025 +0300

    Merge branch 'master' into AGDNS-3334-imp-home

commit 965d052f96e5e12f7b8e63a9367494528de99841
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Mon Oct 27 15:58:20 2025 +0300

    home: imp docs

commit e53565e055b6ba58afa6976488c20d98012adb94
Merge: 3bafcd30c 8b8bfb21e
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Mon Oct 27 15:50:12 2025 +0300

    Merge branch 'master' into AGDNS-3334-imp-home

commit 3bafcd30cdfbb8d995c8367c69ba7f7c3fb33ede
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Fri Oct 24 16:53:48 2025 +0300

    all: imp style

commit b762eaa51c9ea8c77d60a56933097718031786d2
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Thu Oct 23 17:08:48 2025 +0300

    home: imp docs

commit c54a961c2e46d29c7f32169b6d48deb7c75038bf
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Thu Oct 23 11:21:03 2025 +0300

    home: fix tls server

commit 0801b367f801d1461cb2e302031363dadef0a35a
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Wed Oct 22 18:33:48 2025 +0300

    home: imp style

commit f148198fe059584d6ad6f25a39298596d6302387
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Wed Oct 22 14:35:12 2025 +0300

    home: imp allocs

commit dbe768ec9680f2024852d12fcc67154ae58ab6ae
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Wed Oct 22 14:20:38 2025 +0300

    home: fix docs, style

commit 74e88ba044bdcb8b56f284c27426f7515493c0b4
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Wed Oct 22 14:07:24 2025 +0300

    home: imp docs, imp maintain

commit 6260af1acee9e2b8432eddc534bff3cb26baec8a
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Tue Oct 21 20:00:30 2025 +0300

    scripts: changed gocognit value for home pkg

commit 4a5dfaa618b9deed0b716e6a9a32a9a2d1c2156b
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Tue Oct 21 19:51:38 2025 +0300

    all: imp style

commit 18f9d9a0224e67ef986070df232dad922a1305d5
Author: f.setrakov <f.setrakov@adguard.com>
Date:   Mon Oct 20 19:17:36 2025 +0300

    home: imp cognit complexity
2025-11-01 13:22:48 +03:00

296 lines
7.0 KiB
Bash

#!/bin/sh
# This comment is used to simplify checking local copies of the script. Bump
# this number every time a significant change is made to this script.
#
# AdGuard-Project-Version: 14
verbose="${VERBOSE:-0}"
readonly verbose
if [ "$verbose" -gt '0' ]; then
set -x
fi
# Set $EXIT_ON_ERROR to zero to see all errors.
if [ "${EXIT_ON_ERROR:-1}" -eq '0' ]; then
set +e
else
set -e
fi
set -f -u
# Source the common helpers, including not_found and run_linter.
. ./scripts/make/helper.sh
# Simple analyzers
# blocklist_imports is a simple best-effort check against unwanted packages.
# The following packages are banned:
#
# * Package errors is replaced by our own package in the
# github.com/AdguardTeam/golibs module.
#
# * Packages log and github.com/AdguardTeam/golibs/log are replaced by
# stdlib's new package log/slog and AdGuard's new utilities package
# github.com/AdguardTeam/golibs/logutil/slogutil.
#
# * Package github.com/prometheus/client_golang/prometheus/promauto is not
# recommended, as it encourages reliance on global state.
#
# * Packages golang.org/x/exp/maps, golang.org/x/exp/slices, and
# golang.org/x/net/context have been moved into stdlib.
#
# * Package io/ioutil is soft-deprecated.
#
# * Package reflect is often an overkill, and for deep comparisons there are
# much better functions in module github.com/google/go-cmp. Which is
# already our indirect dependency and which may or may not enter the stdlib
# at some point.
#
# See https://github.com/golang/go/issues/45200.
#
# * Package sort is replaced by package slices.
#
# * Package unsafe is… unsafe.
#
# Currently, the only standard exception are files generated from protobuf
# schemas, which use package reflect. If your project needs more exceptions,
# add and document them.
#
# NOTE: Flag -H for grep is non-POSIX but all of Busybox, GNU, macOS, and
# OpenBSD support it.
#
# NOTE: Exclude the security_windows.go, because it requires unsafe for the OS
# APIs.
#
# TODO(a.garipov): Add golibs/log.
blocklist_imports() {
import_or_tab="$(printf '^\\(import \\|\t\\)')"
readonly import_or_tab
find_with_ignore \
-type 'f' \
-name '*.go' \
'!' '(' \
-name '*.pb.go' \
-o -path './internal/permcheck/security_windows.go' \
')' \
-exec \
'grep' \
'-H' \
'-e' "$import_or_tab"'"errors"$' \
'-e' "$import_or_tab"'"github.com/prometheus/client_golang/prometheus/promauto"$' \
'-e' "$import_or_tab"'"golang.org/x/exp/maps"$' \
'-e' "$import_or_tab"'"golang.org/x/exp/slices"$' \
'-e' "$import_or_tab"'"golang.org/x/net/context"$' \
'-e' "$import_or_tab"'"io/ioutil"$' \
'-e' "$import_or_tab"'"log"$' \
'-e' "$import_or_tab"'"reflect"$' \
'-e' "$import_or_tab"'"sort"$' \
'-e' "$import_or_tab"'"unsafe"$' \
'-n' \
'{}' \
';'
}
# method_const is a simple check against the usage of some raw strings and
# numbers where one should use named constants.
#
# NOTE: Flag -H for grep is non-POSIX but all of Busybox, GNU, macOS, and
# OpenBSD support it.
method_const() {
find_with_ignore \
-type 'f' \
-name '*.go' \
-exec \
'grep' \
'-H' \
'-e' '"DELETE"' \
'-e' '"GET"' \
'-e' '"PATCH"' \
'-e' '"POST"' \
'-e' '"PUT"' \
'-n' \
'{}' \
';'
}
# underscores is a simple check against Go filenames with underscores. Add new
# build tags and OS as you go. The main goal of this check is to discourage the
# use of filenames like client_manager.go.
underscores() {
underscore_files="$(
find_with_ignore \
-type 'f' \
-name '*_*.go' \
'!' '(' \
-name '*_bsd.go' \
-o -name '*_darwin.go' \
-o -name '*_freebsd.go' \
-o -name '*_generate.go' \
-o -name '*_linux.go' \
-o -name '*_next.go' \
-o -name '*_openbsd.go' \
-o -name '*_others.go' \
-o -name '*_test.go' \
-o -name '*_unix.go' \
-o -name '*_windows.go' \
')' \
-exec 'printf' '\t%s\n' '{}' ';'
)"
readonly underscore_files
if [ "$underscore_files" != '' ]; then
printf \
'found file names with underscores:\n%s\n' \
"$underscore_files"
fi
}
# TODO(a.garipov): Add an analyzer to look for `fallthrough`, `goto`, and `new`?
# Checks
run_linter -e blocklist_imports
run_linter -e method_const
run_linter -e underscores
run_linter -e gofumpt --extra -e -l .
run_linter "${GO:-go}" vet ./...
run_linter govulncheck ./...
run_linter gocyclo --over 10 .
# TODO(a.garipov): Enable 10 for all.
run_linter gocognit --over='20' \
./internal/querylog/ \
;
run_linter gocognit --over='14' \
./internal/dhcpd \
;
run_linter gocognit --over='10' \
./internal/aghalg/ \
./internal/aghhttp/ \
./internal/aghnet/ \
./internal/aghos/ \
./internal/aghrenameio/ \
./internal/aghtest/ \
./internal/aghtls/ \
./internal/aghuser/ \
./internal/arpdb/ \
./internal/client/ \
./internal/configmigrate/ \
./internal/dhcpsvc \
./internal/dnsforward/ \
./internal/filtering/ \
./internal/home/ \
./internal/ipset \
./internal/next/ \
./internal/rdns/ \
./internal/schedule/ \
./internal/stats/ \
./internal/updater/ \
./internal/version/ \
./internal/whois/ \
./scripts/ \
;
run_linter ineffassign ./...
run_linter unparam ./...
find_with_ignore \
-type 'f' \
'(' \
-name 'Makefile' \
-o -name '*.conf' \
-o -name '*.go' \
-o -name '*.mod' \
-o -name '*.sh' \
-o -name '*.yaml' \
-o -name '*.yml' \
')' \
-exec 'misspell' '--error' '{}' '+'
run_linter nilness ./...
# TODO(a.garipov): Enable for all.
run_linter fieldalignment \
./internal/aghalg/ \
./internal/aghhttp/ \
./internal/aghos/ \
./internal/aghrenameio/ \
./internal/aghtest/ \
./internal/aghtls/ \
./internal/aghuser/ \
./internal/arpdb/ \
./internal/client/ \
./internal/configmigrate/ \
./internal/dhcpsvc/ \
./internal/filtering/hashprefix/ \
./internal/filtering/rewrite/ \
./internal/filtering/rulelist/ \
./internal/filtering/safesearch/ \
./internal/ipset/ \
./internal/next/... \
./internal/querylog/ \
./internal/rdns/ \
./internal/schedule/ \
./internal/stats/ \
./internal/updater/ \
./internal/version/ \
./internal/whois/ \
;
run_linter -e shadow --strict ./...
# TODO(a.garipov): Enable for all.
# TODO(e.burkov): Re-enable G115.
run_linter gosec --exclude G115 --quiet \
./internal/aghalg/ \
./internal/aghhttp/ \
./internal/aghnet/ \
./internal/aghos/ \
./internal/aghrenameio/ \
./internal/aghtest/ \
./internal/aghuser/ \
./internal/arpdb/ \
./internal/client/ \
./internal/configmigrate/ \
./internal/dhcpd/ \
./internal/dhcpsvc/ \
./internal/dnsforward/ \
./internal/filtering/hashprefix/ \
./internal/filtering/rewrite/ \
./internal/filtering/rulelist/ \
./internal/filtering/safesearch/ \
./internal/ipset/ \
./internal/next/ \
./internal/rdns/ \
./internal/schedule/ \
./internal/stats/ \
./internal/version/ \
./internal/whois/ \
;
run_linter errcheck ./...
staticcheck_matrix='
darwin: GOOS=darwin
freebsd: GOOS=freebsd
linux: GOOS=linux
openbsd: GOOS=openbsd
windows: GOOS=windows
'
readonly staticcheck_matrix
printf '%s' "$staticcheck_matrix" | run_linter staticcheck --matrix ./...