2020-12-08 15:23:35 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2020-12-30 15:26:25 +00:00
|
|
|
verbose="${VERBOSE:-0}"
|
|
|
|
|
2021-05-19 18:31:20 +01:00
|
|
|
# Set verbosity.
|
2020-12-30 15:26:25 +00:00
|
|
|
if [ "$verbose" -gt '0' ]
|
|
|
|
then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Set $EXIT_ON_ERROR to zero to see all errors.
|
2021-05-19 18:31:20 +01:00
|
|
|
if [ "${EXIT_ON_ERROR:-1}" -eq '0' ]
|
2020-12-30 15:26:25 +00:00
|
|
|
then
|
|
|
|
set +e
|
|
|
|
else
|
|
|
|
set -e
|
|
|
|
fi
|
2020-12-08 15:23:35 +00:00
|
|
|
|
2021-05-19 18:31:20 +01:00
|
|
|
# We don't need glob expansions and we want to see errors about unset variables.
|
2020-12-08 15:23:35 +00:00
|
|
|
set -f -u
|
|
|
|
|
2021-02-04 11:15:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Deferred Helpers
|
|
|
|
|
2021-05-19 18:31:20 +01:00
|
|
|
not_found_msg='
|
2020-12-10 11:35:07 +00:00
|
|
|
looks like a binary not found error.
|
|
|
|
make sure you have installed the linter binaries using:
|
|
|
|
|
2020-12-30 15:26:25 +00:00
|
|
|
$ make go-tools
|
2020-12-10 11:35:07 +00:00
|
|
|
'
|
2021-05-19 18:31:20 +01:00
|
|
|
readonly not_found_msg
|
2020-12-10 11:35:07 +00:00
|
|
|
|
2021-05-19 18:31:20 +01:00
|
|
|
# TODO(a.garipov): Put it into a separate script and source it both here and in
|
|
|
|
# txt-lint.sh?
|
2020-12-10 11:35:07 +00:00
|
|
|
not_found() {
|
2021-05-19 18:31:20 +01:00
|
|
|
if [ "$?" -eq '127' ]
|
2020-12-10 11:35:07 +00:00
|
|
|
then
|
2021-05-19 18:31:20 +01:00
|
|
|
# Code 127 is the exit status a shell uses when a command or
|
|
|
|
# a file is not found, according to the Bash Hackers wiki.
|
2020-12-10 11:35:07 +00:00
|
|
|
#
|
|
|
|
# See https://wiki.bash-hackers.org/dict/terms/exit_status.
|
|
|
|
echo "$not_found_msg" 1>&2
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
trap not_found EXIT
|
|
|
|
|
2021-02-04 11:15:34 +00:00
|
|
|
|
|
|
|
|
2021-04-22 18:06:34 +01:00
|
|
|
# Warnings
|
|
|
|
|
2022-01-19 12:06:23 +00:00
|
|
|
go_version="$( "${GO:-go}" version )"
|
2021-08-23 16:10:36 +01:00
|
|
|
readonly go_version
|
|
|
|
|
2021-12-22 13:34:51 +00:00
|
|
|
go_min_version='go1.17'
|
2021-05-19 18:31:20 +01:00
|
|
|
go_version_msg="
|
2021-08-23 16:10:36 +01:00
|
|
|
warning: your go version (${go_version}) is different from the recommended minimal one (${go_min_version}).
|
2021-04-22 18:06:34 +01:00
|
|
|
if you have the version installed, please set the GO environment variable.
|
|
|
|
for example:
|
|
|
|
|
|
|
|
export GO='${go_min_version}'
|
|
|
|
"
|
2021-06-25 16:30:30 +01:00
|
|
|
readonly go_min_version go_version_msg
|
2021-05-19 18:31:20 +01:00
|
|
|
|
2021-08-23 16:10:36 +01:00
|
|
|
case "$go_version"
|
2021-04-22 18:06:34 +01:00
|
|
|
in
|
2021-06-25 16:30:30 +01:00
|
|
|
('go version'*"$go_min_version"*)
|
2021-04-22 18:06:34 +01:00
|
|
|
# Go on.
|
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
echo "$go_version_msg" 1>&2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-04 11:15:34 +00:00
|
|
|
# Simple Analyzers
|
|
|
|
|
2021-07-29 15:40:31 +01: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 12:06:23 +00:00
|
|
|
# * Package io/ioutil is soft-deprecated.
|
|
|
|
#
|
2021-07-29 15:40:31 +01: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.
|
|
|
|
#
|
|
|
|
# * Package unsafe is… unsafe.
|
|
|
|
#
|
2022-01-19 12:06:23 +00:00
|
|
|
# * Package golang.org/x/net/context has been moved into stdlib.
|
|
|
|
#
|
2020-12-10 11:35:07 +00:00
|
|
|
blocklist_imports() {
|
2021-07-29 15:40:31 +01:00
|
|
|
git grep\
|
|
|
|
-e '[[:space:]]"errors"$'\
|
|
|
|
-e '[[:space:]]"io/ioutil"$'\
|
|
|
|
-e '[[:space:]]"log"$'\
|
|
|
|
-e '[[:space:]]"reflect"$'\
|
|
|
|
-e '[[:space:]]"unsafe"$'\
|
2022-01-19 12:06:23 +00:00
|
|
|
-e '[[:space:]]"golang.org/x/net/context"$'\
|
|
|
|
-n\
|
|
|
|
-- '*.go'\
|
|
|
|
| sed -e 's/^\([^[:space:]]\+\)\(.*\)$/\1 blocked import:\2/'\
|
|
|
|
|| exit 0
|
2020-12-08 15:23:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 18:31:20 +01: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 11:15:34 +00:00
|
|
|
method_const() {
|
2022-01-19 12:06:23 +00:00
|
|
|
git grep -F\
|
|
|
|
-e '"DELETE"'\
|
|
|
|
-e '"GET"'\
|
|
|
|
-e '"POST"'\
|
|
|
|
-e '"PUT"'\
|
|
|
|
-n\
|
|
|
|
-- '*.go'\
|
|
|
|
| sed -e 's/^\([^[:space:]]\+\)\(.*\)$/\1 http method literal:\2/'\
|
|
|
|
|| exit 0
|
2021-02-04 11:15:34 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 12:06:23 +00: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 11:35:07 +00:00
|
|
|
underscores() {
|
2022-01-19 12:06:23 +00: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-03-31 17:56:50 +01:00
|
|
|
-e '_openbsd.go'\
|
2022-01-19 12:06:23 +00:00
|
|
|
-e '_others.go'\
|
|
|
|
-e '_test.go'\
|
|
|
|
-e '_unix.go'\
|
2022-04-26 18:50:09 +01:00
|
|
|
-e '_v1.go'\
|
2022-01-19 12:06:23 +00:00
|
|
|
-e '_windows.go' \
|
|
|
|
-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 15:23:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 16:30:57 +01:00
|
|
|
# TODO(a.garipov): Add an analyser to look for `fallthrough`, `goto`, and `new`?
|
|
|
|
|
2021-02-04 11:15:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Helpers
|
|
|
|
|
2021-05-19 18:31:20 +01:00
|
|
|
# exit_on_output exits with a nonzero exit code if there is anything in the
|
|
|
|
# command's combined output.
|
2021-01-27 17:45:00 +00:00
|
|
|
exit_on_output() (
|
|
|
|
set +e
|
|
|
|
|
|
|
|
if [ "$VERBOSE" -lt '2' ]
|
|
|
|
then
|
|
|
|
set +x
|
|
|
|
fi
|
2020-12-08 15:23:35 +00:00
|
|
|
|
|
|
|
cmd="$1"
|
|
|
|
shift
|
|
|
|
|
2021-04-22 18:06:34 +01:00
|
|
|
output="$( "$cmd" "$@" 2>&1 )"
|
2021-01-27 17:45:00 +00:00
|
|
|
exitcode="$?"
|
2022-01-19 12:06:23 +00:00
|
|
|
if [ "$exitcode" -ne '0' ]
|
2021-01-27 17:45:00 +00:00
|
|
|
then
|
|
|
|
echo "'$cmd' failed with code $exitcode"
|
|
|
|
fi
|
|
|
|
|
2020-12-08 15:23:35 +00:00
|
|
|
if [ "$output" != '' ]
|
|
|
|
then
|
|
|
|
if [ "$*" != '' ]
|
|
|
|
then
|
2022-01-19 12:06:23 +00:00
|
|
|
echo "combined output of linter '$cmd $*':"
|
2020-12-08 15:23:35 +00:00
|
|
|
else
|
2022-01-19 12:06:23 +00:00
|
|
|
echo "combined output of linter '$cmd':"
|
2020-12-08 15:23:35 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$output"
|
|
|
|
|
2021-05-19 18:31:20 +01:00
|
|
|
if [ "$exitcode" -eq '0' ]
|
2021-01-27 17:45:00 +00:00
|
|
|
then
|
|
|
|
exitcode='1'
|
|
|
|
fi
|
2020-12-08 15:23:35 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
return "$exitcode"
|
2021-01-27 17:45:00 +00:00
|
|
|
)
|
2020-12-08 15:23:35 +00:00
|
|
|
|
2021-02-04 11:15:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Checks
|
|
|
|
|
2020-12-10 11:35:07 +00:00
|
|
|
exit_on_output blocklist_imports
|
2020-12-08 15:23:35 +00:00
|
|
|
|
2021-02-04 11:15:34 +00:00
|
|
|
exit_on_output method_const
|
|
|
|
|
2020-12-10 11:35:07 +00:00
|
|
|
exit_on_output underscores
|
2020-12-08 15:23:35 +00:00
|
|
|
|
2021-12-22 13:34:51 +00:00
|
|
|
exit_on_output gofumpt --extra -e -l .
|
2020-12-08 15:23:35 +00:00
|
|
|
|
|
|
|
golint --set_exit_status ./...
|
|
|
|
|
|
|
|
"$GO" vet ./...
|
|
|
|
|
2021-05-31 18:11:06 +01:00
|
|
|
# Apply more lax standards to the code we haven't properly refactored yet.
|
|
|
|
gocyclo --over 17 ./internal/dhcpd/ ./internal/dnsforward/\
|
2021-08-27 12:50:37 +01:00
|
|
|
./internal/filtering/ ./internal/home/ ./internal/querylog/
|
2021-05-31 18:11:06 +01:00
|
|
|
|
2021-08-27 12:50:37 +01:00
|
|
|
# Apply stricter standards to new or somewhat refactored code.
|
2021-05-31 18:11:06 +01:00
|
|
|
gocyclo --over 10 ./internal/aghio/ ./internal/aghnet/ ./internal/aghos/\
|
2021-08-27 12:50:37 +01:00
|
|
|
./internal/aghtest/ ./internal/stats/ ./internal/tools/\
|
2022-04-26 18:50:09 +01:00
|
|
|
./internal/updater/ ./internal/v1/ ./internal/version/ ./main.go\
|
2020-12-08 15:23:35 +00:00
|
|
|
|
2021-03-26 10:03:36 +00:00
|
|
|
ineffassign ./...
|
2020-12-08 15:23:35 +00:00
|
|
|
|
|
|
|
unparam ./...
|
|
|
|
|
2021-05-19 18:31:20 +01:00
|
|
|
git ls-files -- '*.go' '*.mod' '*.sh' 'Makefile' | xargs misspell --error
|
2020-12-08 15:23:35 +00:00
|
|
|
|
|
|
|
looppointer ./...
|
|
|
|
|
|
|
|
nilness ./...
|
|
|
|
|
2021-03-12 11:32:08 +00:00
|
|
|
exit_on_output shadow --strict ./...
|
2020-12-08 15:23:35 +00:00
|
|
|
|
2022-01-19 12:06:23 +00:00
|
|
|
# TODO(a.garipov): Enable in v0.108.0.
|
|
|
|
# gosec --quiet ./...
|
|
|
|
|
2021-05-24 15:28:11 +01:00
|
|
|
# TODO(a.garipov): Enable --blank?
|
|
|
|
errcheck --asserts ./...
|
2020-12-08 15:23:35 +00:00
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
staticcheck ./...
|