2020-12-08 18:23:35 +03:00
|
|
|
#!/bin/sh
|
|
|
|
|
2023-02-21 16:38:22 +03:00
|
|
|
# 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.
|
|
|
|
#
|
2024-11-08 17:18:16 +03:00
|
|
|
# AdGuard-Project-Version: 13
|
2023-02-21 16:38:22 +03:00
|
|
|
|
2020-12-30 18:26:25 +03:00
|
|
|
verbose="${VERBOSE:-0}"
|
2023-02-21 16:38:22 +03:00
|
|
|
readonly verbose
|
2020-12-30 18:26:25 +03:00
|
|
|
|
2024-11-08 17:18:16 +03:00
|
|
|
if [ "$verbose" -gt '0' ]; then
|
2020-12-30 18:26:25 +03:00
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Set $EXIT_ON_ERROR to zero to see all errors.
|
2024-11-08 17:18:16 +03:00
|
|
|
if [ "${EXIT_ON_ERROR:-1}" -eq '0' ]; then
|
2020-12-30 18:26:25 +03:00
|
|
|
set +e
|
|
|
|
else
|
|
|
|
set -e
|
|
|
|
fi
|
2020-12-08 18:23:35 +03:00
|
|
|
|
|
|
|
set -f -u
|
|
|
|
|
2023-02-21 16:38:22 +03:00
|
|
|
# Source the common helpers, including not_found and run_linter.
|
|
|
|
. ./scripts/make/helper.sh
|
2020-12-10 14:35:07 +03:00
|
|
|
|
2023-02-21 16:38:22 +03:00
|
|
|
# Simple analyzers
|
2021-02-04 14:15:34 +03:00
|
|
|
|
2021-07-29 17:40:31 +03:00
|
|
|
# blocklist_imports is a simple check against unwanted packages. The following
|
|
|
|
# packages are banned:
|
|
|
|
#
|
2024-08-20 18:38:04 +03:00
|
|
|
# * Package errors is replaced by our own package in the
|
2024-11-08 17:18:16 +03:00
|
|
|
# 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.
|
2021-07-29 17:40:31 +03:00
|
|
|
#
|
2024-11-08 17:18:16 +03:00
|
|
|
# * 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.
|
2024-08-20 18:38:04 +03:00
|
|
|
#
|
2022-01-19 15:06:23 +03:00
|
|
|
# * Package io/ioutil is soft-deprecated.
|
|
|
|
#
|
2021-07-29 17:40:31 +03:00
|
|
|
# * 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.
|
|
|
|
#
|
2024-02-08 20:39:18 +03:00
|
|
|
# * Package sort is replaced by package slices.
|
2023-02-21 16:38:22 +03:00
|
|
|
#
|
2021-07-29 17:40:31 +03:00
|
|
|
# * Package unsafe is… unsafe.
|
|
|
|
#
|
2024-11-08 17:18:16 +03:00
|
|
|
# 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.
|
2024-10-29 14:28:59 +03:00
|
|
|
#
|
2024-11-08 17:18:16 +03:00
|
|
|
# NOTE: Flag -H for grep is non-POSIX but all of Busybox, GNU, macOS, and
|
|
|
|
# OpenBSD support it.
|
2024-10-29 14:28:59 +03:00
|
|
|
#
|
2024-12-03 18:26:00 +03:00
|
|
|
# NOTE: Exclude the security_windows.go, because it requires unsafe for the OS
|
|
|
|
# APIs.
|
2023-06-13 13:41:13 +03:00
|
|
|
#
|
2024-02-13 18:30:14 +03:00
|
|
|
# TODO(a.garipov): Add golibs/log.
|
2020-12-10 14:35:07 +03:00
|
|
|
blocklist_imports() {
|
2024-11-08 17:18:16 +03:00
|
|
|
find . \
|
|
|
|
-type 'f' \
|
|
|
|
-name '*.go' \
|
|
|
|
'!' '(' \
|
|
|
|
-name '*.pb.go' \
|
2024-12-03 18:26:00 +03:00
|
|
|
-o -path './internal/permcheck/security_windows.go' \
|
2024-11-08 17:18:16 +03:00
|
|
|
')' \
|
|
|
|
-exec \
|
|
|
|
'grep' \
|
|
|
|
'-H' \
|
|
|
|
'-e' '[[:space:]]"errors"$' \
|
|
|
|
'-e' '[[:space:]]"github.com/prometheus/client_golang/prometheus/promauto"$' \
|
|
|
|
'-e' '[[:space:]]"golang.org/x/exp/maps"$' \
|
|
|
|
'-e' '[[:space:]]"golang.org/x/exp/slices"$' \
|
|
|
|
'-e' '[[:space:]]"golang.org/x/net/context"$' \
|
|
|
|
'-e' '[[:space:]]"io/ioutil"$' \
|
|
|
|
'-e' '[[:space:]]"log"$' \
|
|
|
|
'-e' '[[:space:]]"reflect"$' \
|
|
|
|
'-e' '[[:space:]]"sort"$' \
|
|
|
|
'-e' '[[:space:]]"unsafe"$' \
|
|
|
|
'-n' \
|
|
|
|
'{}' \
|
|
|
|
';'
|
2020-12-08 18:23:35 +03:00
|
|
|
}
|
|
|
|
|
2021-05-19 20:31:20 +03:00
|
|
|
# method_const is a simple check against the usage of some raw strings and
|
|
|
|
# numbers where one should use named constants.
|
2021-02-04 14:15:34 +03:00
|
|
|
method_const() {
|
2024-11-08 17:18:16 +03:00
|
|
|
find . \
|
|
|
|
-type 'f' \
|
|
|
|
-name '*.go' \
|
|
|
|
-exec \
|
|
|
|
'grep' \
|
|
|
|
'-H' \
|
|
|
|
'-e' '"DELETE"' \
|
|
|
|
'-e' '"GET"' \
|
|
|
|
'-e' '"PATCH"' \
|
|
|
|
'-e' '"POST"' \
|
|
|
|
'-e' '"PUT"' \
|
|
|
|
'-n' \
|
|
|
|
'{}' \
|
|
|
|
';'
|
2021-02-04 14:15:34 +03:00
|
|
|
}
|
|
|
|
|
2022-01-19 15:06:23 +03:00
|
|
|
# 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.
|
2020-12-10 14:35:07 +03:00
|
|
|
underscores() {
|
2022-01-19 15:06:23 +03:00
|
|
|
underscore_files="$(
|
2024-11-08 17:18:16 +03:00
|
|
|
find . \
|
|
|
|
-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' '{}' ';'
|
2022-01-19 15:06:23 +03:00
|
|
|
)"
|
|
|
|
readonly underscore_files
|
|
|
|
|
2024-11-08 17:18:16 +03:00
|
|
|
if [ "$underscore_files" != '' ]; then
|
|
|
|
printf \
|
|
|
|
'found file names with underscores:\n%s\n' \
|
|
|
|
"$underscore_files"
|
2022-01-19 15:06:23 +03:00
|
|
|
fi
|
2020-12-08 18:23:35 +03:00
|
|
|
}
|
|
|
|
|
2022-08-02 20:48:14 +03:00
|
|
|
# TODO(a.garipov): Add an analyzer to look for `fallthrough`, `goto`, and `new`?
|
2021-05-21 18:30:57 +03:00
|
|
|
|
2021-02-04 14:15:34 +03:00
|
|
|
# Checks
|
|
|
|
|
2023-02-21 16:38:22 +03:00
|
|
|
run_linter -e blocklist_imports
|
2020-12-08 18:23:35 +03:00
|
|
|
|
2023-02-21 16:38:22 +03:00
|
|
|
run_linter -e method_const
|
2021-02-04 14:15:34 +03:00
|
|
|
|
2023-02-21 16:38:22 +03:00
|
|
|
run_linter -e underscores
|
2020-12-08 18:23:35 +03:00
|
|
|
|
2023-02-21 16:38:22 +03:00
|
|
|
run_linter -e gofumpt --extra -e -l .
|
2020-12-08 18:23:35 +03:00
|
|
|
|
2024-08-20 18:38:04 +03:00
|
|
|
run_linter "${GO:-go}" vet ./...
|
2020-12-08 18:23:35 +03:00
|
|
|
|
2023-02-21 16:38:22 +03:00
|
|
|
run_linter govulncheck ./...
|
2022-09-07 14:01:03 +03:00
|
|
|
|
2023-05-26 12:50:03 +03:00
|
|
|
run_linter gocyclo --over 10 .
|
2023-02-21 16:38:22 +03:00
|
|
|
|
2023-08-21 19:56:08 +03:00
|
|
|
# TODO(a.garipov): Enable 10 for all.
|
2024-11-08 17:18:16 +03:00
|
|
|
run_linter gocognit --over='20' \
|
|
|
|
./internal/querylog/ \
|
2023-09-07 14:10:35 +03:00
|
|
|
;
|
|
|
|
|
2024-11-08 17:18:16 +03:00
|
|
|
run_linter gocognit --over='19' \
|
|
|
|
./internal/home/ \
|
2023-09-07 14:10:35 +03:00
|
|
|
;
|
|
|
|
|
2024-11-08 17:18:16 +03:00
|
|
|
run_linter gocognit --over='18' \
|
|
|
|
./internal/aghtls/ \
|
2023-09-07 14:10:35 +03:00
|
|
|
;
|
|
|
|
|
2024-11-08 17:18:16 +03:00
|
|
|
run_linter gocognit --over='15' \
|
|
|
|
./internal/aghos/ \
|
|
|
|
./internal/filtering/ \
|
2023-09-07 14:10:35 +03:00
|
|
|
;
|
|
|
|
|
2024-11-08 17:18:16 +03:00
|
|
|
run_linter gocognit --over='14' \
|
|
|
|
./internal/dhcpd \
|
2023-09-07 14:10:35 +03:00
|
|
|
;
|
|
|
|
|
2024-11-08 17:18:16 +03:00
|
|
|
run_linter gocognit --over='13' \
|
|
|
|
./internal/aghnet/ \
|
2023-09-07 14:10:35 +03:00
|
|
|
;
|
|
|
|
|
2024-11-08 17:18:16 +03:00
|
|
|
run_linter gocognit --over='12' \
|
|
|
|
./internal/filtering/rewrite/ \
|
2023-09-07 14:10:35 +03:00
|
|
|
;
|
|
|
|
|
2024-11-08 17:18:16 +03:00
|
|
|
run_linter gocognit --over='11' \
|
|
|
|
./internal/updater/ \
|
2023-11-20 18:07:23 +03:00
|
|
|
;
|
|
|
|
|
2024-11-08 17:18:16 +03:00
|
|
|
run_linter gocognit --over='10' \
|
|
|
|
./internal/aghalg/ \
|
|
|
|
./internal/aghhttp/ \
|
|
|
|
./internal/aghrenameio/ \
|
|
|
|
./internal/aghtest/ \
|
|
|
|
./internal/arpdb/ \
|
|
|
|
./internal/client/ \
|
|
|
|
./internal/configmigrate/ \
|
|
|
|
./internal/dhcpsvc \
|
|
|
|
./internal/dnsforward/ \
|
|
|
|
./internal/filtering/hashprefix/ \
|
|
|
|
./internal/filtering/rulelist/ \
|
|
|
|
./internal/filtering/safesearch/ \
|
|
|
|
./internal/ipset \
|
|
|
|
./internal/next/ \
|
|
|
|
./internal/rdns/ \
|
|
|
|
./internal/schedule/ \
|
|
|
|
./internal/stats/ \
|
|
|
|
./internal/tools/ \
|
|
|
|
./internal/version/ \
|
|
|
|
./internal/whois/ \
|
|
|
|
./scripts/ \
|
2023-09-07 14:10:35 +03:00
|
|
|
;
|
2023-07-19 16:57:57 +03:00
|
|
|
|
2023-02-21 16:38:22 +03:00
|
|
|
run_linter ineffassign ./...
|
|
|
|
|
|
|
|
run_linter unparam ./...
|
|
|
|
|
2024-11-08 17:18:16 +03:00
|
|
|
find . \
|
|
|
|
-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' '{}' '+'
|
2023-02-21 16:38:22 +03:00
|
|
|
|
|
|
|
run_linter nilness ./...
|
|
|
|
|
2023-09-07 15:05:21 +03:00
|
|
|
# TODO(a.garipov): Enable for all.
|
|
|
|
run_linter fieldalignment \
|
2024-11-08 17:18:16 +03:00
|
|
|
./internal/aghalg/ \
|
|
|
|
./internal/aghhttp/ \
|
|
|
|
./internal/aghos/ \
|
|
|
|
./internal/aghrenameio/ \
|
|
|
|
./internal/aghtest/ \
|
|
|
|
./internal/aghtls/ \
|
|
|
|
./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/ \
|
2023-09-07 15:05:21 +03:00
|
|
|
;
|
2023-02-21 16:38:22 +03:00
|
|
|
|
|
|
|
run_linter -e shadow --strict ./...
|
2020-12-08 18:23:35 +03:00
|
|
|
|
2023-06-29 14:34:06 +03:00
|
|
|
# TODO(a.garipov): Enable for all.
|
2024-09-10 15:14:48 +03:00
|
|
|
# TODO(e.burkov): Re-enable G115.
|
2024-11-08 17:18:16 +03:00
|
|
|
run_linter gosec --exclude G115 --quiet \
|
|
|
|
./internal/aghalg/ \
|
|
|
|
./internal/aghhttp/ \
|
|
|
|
./internal/aghnet/ \
|
|
|
|
./internal/aghos/ \
|
|
|
|
./internal/aghrenameio/ \
|
|
|
|
./internal/aghtest/ \
|
|
|
|
./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/tools/ \
|
|
|
|
./internal/version/ \
|
|
|
|
./internal/whois/ \
|
2023-06-29 14:34:06 +03:00
|
|
|
;
|
2022-01-19 15:06:23 +03:00
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
run_linter errcheck ./...
|
2020-12-08 18:23:35 +03:00
|
|
|
|
2023-06-13 13:41:13 +03:00
|
|
|
staticcheck_matrix='
|
|
|
|
darwin: GOOS=darwin
|
|
|
|
freebsd: GOOS=freebsd
|
|
|
|
linux: GOOS=linux
|
|
|
|
openbsd: GOOS=openbsd
|
|
|
|
windows: GOOS=windows
|
|
|
|
'
|
|
|
|
readonly staticcheck_matrix
|
|
|
|
|
2024-11-08 17:18:16 +03:00
|
|
|
printf '%s' "$staticcheck_matrix" | run_linter staticcheck --matrix ./...
|