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.
|
|
|
|
#
|
2023-06-13 13:41:13 +03:00
|
|
|
# AdGuard-Project-Version: 4
|
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
|
|
|
|
|
|
|
if [ "$verbose" -gt '0' ]
|
|
|
|
then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Set $EXIT_ON_ERROR to zero to see all errors.
|
2021-05-19 20:31:20 +03:00
|
|
|
if [ "${EXIT_ON_ERROR:-1}" -eq '0' ]
|
2020-12-30 18:26:25 +03:00
|
|
|
then
|
|
|
|
set +e
|
|
|
|
else
|
|
|
|
set -e
|
|
|
|
fi
|
2020-12-08 18:23:35 +03:00
|
|
|
|
|
|
|
set -f -u
|
|
|
|
|
2021-02-04 14:15:34 +03:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-02-04 14:15:34 +03:00
|
|
|
|
|
|
|
|
2021-04-22 20:06:34 +03:00
|
|
|
# Warnings
|
|
|
|
|
2022-01-19 15:06:23 +03:00
|
|
|
go_version="$( "${GO:-go}" version )"
|
2021-08-23 18:10:36 +03:00
|
|
|
readonly go_version
|
|
|
|
|
2023-08-02 13:25:04 +03:00
|
|
|
go_min_version='go1.20.7'
|
2021-05-19 20:31:20 +03:00
|
|
|
go_version_msg="
|
2021-08-23 18:10:36 +03:00
|
|
|
warning: your go version (${go_version}) is different from the recommended minimal one (${go_min_version}).
|
2021-04-22 20:06:34 +03:00
|
|
|
if you have the version installed, please set the GO environment variable.
|
|
|
|
for example:
|
|
|
|
|
|
|
|
export GO='${go_min_version}'
|
|
|
|
"
|
2021-06-25 18:30:30 +03:00
|
|
|
readonly go_min_version go_version_msg
|
2021-05-19 20:31:20 +03:00
|
|
|
|
2021-08-23 18:10:36 +03:00
|
|
|
case "$go_version"
|
2021-04-22 20:06:34 +03:00
|
|
|
in
|
2021-06-25 18:30:30 +03:00
|
|
|
('go version'*"$go_min_version"*)
|
2021-04-22 20:06:34 +03:00
|
|
|
# Go on.
|
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
echo "$go_version_msg" 1>&2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
#
|
|
|
|
# * Packages errors and log are replaced by our own packages in the
|
|
|
|
# github.com/AdguardTeam/golibs module.
|
|
|
|
#
|
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.
|
|
|
|
#
|
2023-02-21 16:38:22 +03:00
|
|
|
# * Package sort is replaced by golang.org/x/exp/slices.
|
|
|
|
#
|
2021-07-29 17:40:31 +03:00
|
|
|
# * Package unsafe is… unsafe.
|
|
|
|
#
|
2022-01-19 15:06:23 +03:00
|
|
|
# * Package golang.org/x/net/context has been moved into stdlib.
|
|
|
|
#
|
2023-06-13 13:41:13 +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.
|
|
|
|
#
|
|
|
|
# TODO(a.garipov): Add deprecated packages golang.org/x/exp/maps and
|
|
|
|
# golang.org/x/exp/slices once all projects switch to Go 1.21.
|
2020-12-10 14:35:07 +03:00
|
|
|
blocklist_imports() {
|
2021-07-29 17:40:31 +03:00
|
|
|
git grep\
|
|
|
|
-e '[[:space:]]"errors"$'\
|
|
|
|
-e '[[:space:]]"io/ioutil"$'\
|
|
|
|
-e '[[:space:]]"log"$'\
|
|
|
|
-e '[[:space:]]"reflect"$'\
|
2023-02-21 16:38:22 +03:00
|
|
|
-e '[[:space:]]"sort"$'\
|
2021-07-29 17:40:31 +03:00
|
|
|
-e '[[:space:]]"unsafe"$'\
|
2022-01-19 15:06:23 +03:00
|
|
|
-e '[[:space:]]"golang.org/x/net/context"$'\
|
|
|
|
-n\
|
|
|
|
-- '*.go'\
|
2023-06-13 13:41:13 +03:00
|
|
|
':!*.pb.go'\
|
2022-01-19 15:06:23 +03:00
|
|
|
| sed -e 's/^\([^[:space:]]\+\)\(.*\)$/\1 blocked import:\2/'\
|
|
|
|
|| exit 0
|
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() {
|
2022-01-19 15:06:23 +03:00
|
|
|
git grep -F\
|
|
|
|
-e '"DELETE"'\
|
|
|
|
-e '"GET"'\
|
2023-06-13 13:41:13 +03:00
|
|
|
-e '"PATCH"'\
|
2022-01-19 15:06:23 +03:00
|
|
|
-e '"POST"'\
|
|
|
|
-e '"PUT"'\
|
|
|
|
-n\
|
|
|
|
-- '*.go'\
|
|
|
|
| sed -e 's/^\([^[:space:]]\+\)\(.*\)$/\1 http method literal:\2/'\
|
|
|
|
|| exit 0
|
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="$(
|
|
|
|
git ls-files '*_*.go'\
|
|
|
|
| grep -F\
|
|
|
|
-e '_big.go'\
|
|
|
|
-e '_bsd.go'\
|
|
|
|
-e '_darwin.go'\
|
|
|
|
-e '_freebsd.go'\
|
|
|
|
-e '_linux.go'\
|
|
|
|
-e '_little.go'\
|
2022-10-04 16:02:55 +03:00
|
|
|
-e '_next.go'\
|
2022-03-31 19:56:50 +03:00
|
|
|
-e '_openbsd.go'\
|
2022-01-19 15:06:23 +03:00
|
|
|
-e '_others.go'\
|
|
|
|
-e '_test.go'\
|
|
|
|
-e '_unix.go'\
|
2023-06-13 13:41:13 +03:00
|
|
|
-e '_windows.go'\
|
2022-01-19 15:06:23 +03:00
|
|
|
-v\
|
|
|
|
| sed -e 's/./\t\0/'
|
|
|
|
)"
|
|
|
|
readonly underscore_files
|
|
|
|
|
|
|
|
if [ "$underscore_files" != '' ]
|
|
|
|
then
|
|
|
|
echo 'found file names with underscores:'
|
|
|
|
echo "$underscore_files"
|
|
|
|
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
|
|
|
|
2023-02-21 16:38:22 +03:00
|
|
|
# TODO(a.garipov): golint is deprecated, find a suitable replacement.
|
2020-12-08 18:23:35 +03:00
|
|
|
|
2023-02-21 16:38:22 +03:00
|
|
|
run_linter "$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.
|
|
|
|
#
|
|
|
|
# TODO(a.garipov): Redo once https://github.com/uudashr/gocognit/issues/22 is
|
|
|
|
# fixed.
|
|
|
|
gocognit_paths="\
|
|
|
|
./internal/aghnet/ 20
|
|
|
|
./internal/querylog/ 20
|
|
|
|
./internal/dnsforward/ 19
|
|
|
|
./internal/home/ 19
|
|
|
|
./internal/aghtls/ 18
|
|
|
|
./internal/filtering 17
|
|
|
|
./internal/filtering/rewrite/ 17
|
|
|
|
./internal/aghos/ 15
|
|
|
|
./internal/dhcpd/ 15
|
|
|
|
./internal/updater/ 12
|
|
|
|
./internal/aghtest/ 11
|
|
|
|
./internal/aghalg/ 10
|
|
|
|
./internal/aghchan/ 10
|
|
|
|
./internal/aghhttp/ 10
|
|
|
|
./internal/aghio/ 10
|
|
|
|
./internal/aghrenameio/ 10
|
2023-08-24 13:42:17 +03:00
|
|
|
./internal/arpdb/ 10
|
2023-08-21 19:56:08 +03:00
|
|
|
./internal/client/ 10
|
|
|
|
./internal/dhcpsvc 10
|
|
|
|
./internal/filtering/hashprefix/ 10
|
|
|
|
./internal/filtering/rulelist/ 10
|
|
|
|
./internal/filtering/safesearch/ 10
|
|
|
|
./internal/next/ 10
|
|
|
|
./internal/rdns/ 10
|
|
|
|
./internal/schedule/ 10
|
|
|
|
./internal/stats/ 10
|
|
|
|
./internal/tools/ 10
|
|
|
|
./internal/version/ 10
|
|
|
|
./internal/whois/ 10
|
|
|
|
./scripts/ 10"
|
|
|
|
|
|
|
|
readonly gocognit_paths
|
|
|
|
|
|
|
|
echo "$gocognit_paths" | while read -r path max
|
|
|
|
do
|
|
|
|
run_linter gocognit --over="$max" "$path"
|
|
|
|
done
|
2023-07-19 16:57:57 +03:00
|
|
|
|
2023-02-21 16:38:22 +03:00
|
|
|
run_linter ineffassign ./...
|
|
|
|
|
|
|
|
run_linter unparam ./...
|
|
|
|
|
2023-06-13 13:41:13 +03:00
|
|
|
git ls-files -- 'Makefile' '*.conf' '*.go' '*.mod' '*.sh' '*.yaml' '*.yml'\
|
|
|
|
| xargs misspell --error\
|
|
|
|
| sed -e 's/^/misspell: /'
|
2023-02-21 16:38:22 +03:00
|
|
|
|
|
|
|
run_linter looppointer ./...
|
|
|
|
|
|
|
|
run_linter nilness ./...
|
|
|
|
|
|
|
|
# TODO(a.garipov): Add fieldalignment?
|
|
|
|
|
|
|
|
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.
|
|
|
|
run_linter gosec --quiet\
|
|
|
|
./internal/aghalg\
|
|
|
|
./internal/aghchan\
|
|
|
|
./internal/aghhttp\
|
|
|
|
./internal/aghio\
|
|
|
|
./internal/aghnet\
|
|
|
|
./internal/aghos\
|
2023-07-25 17:47:24 +03:00
|
|
|
./internal/aghrenameio/\
|
2023-06-29 14:34:06 +03:00
|
|
|
./internal/aghtest\
|
2023-07-18 14:02:32 +03:00
|
|
|
./internal/client\
|
2023-06-29 14:34:06 +03:00
|
|
|
./internal/dhcpd\
|
|
|
|
./internal/dhcpsvc\
|
|
|
|
./internal/dnsforward\
|
2023-07-07 18:27:33 +03:00
|
|
|
./internal/filtering/hashprefix/\
|
|
|
|
./internal/filtering/rulelist/\
|
2023-06-29 14:34:06 +03:00
|
|
|
./internal/next\
|
2023-07-18 14:02:32 +03:00
|
|
|
./internal/rdns\
|
2023-06-29 14:34:06 +03:00
|
|
|
./internal/schedule\
|
|
|
|
./internal/stats\
|
|
|
|
./internal/tools\
|
|
|
|
./internal/version\
|
|
|
|
./internal/whois\
|
|
|
|
;
|
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
|
|
|
|
|
|
|
|
echo "$staticcheck_matrix" | run_linter staticcheck --matrix ./...
|