2020-12-08 15:23:35 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2020-12-30 15:26:25 +00:00
|
|
|
verbose="${VERBOSE:-0}"
|
|
|
|
|
2021-05-19 17:31:20 +00: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 17:31:20 +00: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 17:31:20 +00: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 17:31:20 +00: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 17:31:20 +00:00
|
|
|
readonly not_found_msg
|
2020-12-10 11:35:07 +00:00
|
|
|
|
2021-05-19 17:31:20 +00: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 17:31:20 +00:00
|
|
|
if [ "$?" -eq '127' ]
|
2020-12-10 11:35:07 +00:00
|
|
|
then
|
2021-05-19 17:31:20 +00: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 17:06:34 +00:00
|
|
|
# Warnings
|
|
|
|
|
2021-08-23 15:10:36 +00:00
|
|
|
go_version="$( "$GO" version )"
|
|
|
|
readonly go_version
|
|
|
|
|
2021-12-22 13:34:51 +00:00
|
|
|
go_min_version='go1.17'
|
2021-05-19 17:31:20 +00:00
|
|
|
go_version_msg="
|
2021-08-23 15:10:36 +00:00
|
|
|
warning: your go version (${go_version}) is different from the recommended minimal one (${go_min_version}).
|
2021-04-22 17:06:34 +00:00
|
|
|
if you have the version installed, please set the GO environment variable.
|
|
|
|
for example:
|
|
|
|
|
|
|
|
export GO='${go_min_version}'
|
|
|
|
"
|
2021-06-25 15:30:30 +00:00
|
|
|
readonly go_min_version go_version_msg
|
2021-05-19 17:31:20 +00:00
|
|
|
|
2021-08-23 15:10:36 +00:00
|
|
|
|
|
|
|
case "$go_version"
|
2021-04-22 17:06:34 +00:00
|
|
|
in
|
2021-06-25 15:30:30 +00:00
|
|
|
('go version'*"$go_min_version"*)
|
2021-04-22 17:06:34 +00:00
|
|
|
# Go on.
|
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
echo "$go_version_msg" 1>&2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-04 11:15:34 +00:00
|
|
|
# Simple Analyzers
|
|
|
|
|
2021-07-29 14:40:31 +00:00
|
|
|
# blocklist_imports is a simple check against unwanted packages. The following
|
|
|
|
# packages are banned:
|
|
|
|
#
|
|
|
|
# * Package io/ioutil is soft-deprecated.
|
|
|
|
#
|
|
|
|
# * Packages errors and log are replaced by our own packages in the
|
|
|
|
# github.com/AdguardTeam/golibs module.
|
|
|
|
#
|
|
|
|
# * 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.
|
|
|
|
#
|
2020-12-10 11:35:07 +00:00
|
|
|
blocklist_imports() {
|
2021-07-29 14:40:31 +00:00
|
|
|
git grep\
|
|
|
|
-e '[[:space:]]"errors"$'\
|
|
|
|
-e '[[:space:]]"io/ioutil"$'\
|
|
|
|
-e '[[:space:]]"log"$'\
|
|
|
|
-e '[[:space:]]"reflect"$'\
|
|
|
|
-e '[[:space:]]"unsafe"$'\
|
|
|
|
-- '*.go' || exit 0;
|
2020-12-08 15:23:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 17:31:20 +00: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() {
|
|
|
|
git grep -F -e '"GET"' -e '"POST"' -- '*.go' || exit 0;
|
|
|
|
}
|
|
|
|
|
2020-12-08 15:23:35 +00:00
|
|
|
# underscores is a simple check against Go filenames with underscores.
|
2020-12-10 11:35:07 +00:00
|
|
|
underscores() {
|
2021-03-16 18:00:17 +00:00
|
|
|
git ls-files '*_*.go' | {
|
|
|
|
grep -F\
|
|
|
|
-e '_big.go'\
|
2021-05-17 10:27:40 +00:00
|
|
|
-e '_bsd.go'\
|
2021-03-16 18:00:17 +00:00
|
|
|
-e '_darwin.go'\
|
|
|
|
-e '_freebsd.go'\
|
2021-08-12 14:33:53 +00:00
|
|
|
-e '_openbsd.go'\
|
2021-03-16 18:00:17 +00:00
|
|
|
-e '_linux.go'\
|
|
|
|
-e '_little.go'\
|
|
|
|
-e '_others.go'\
|
|
|
|
-e '_test.go'\
|
|
|
|
-e '_unix.go'\
|
|
|
|
-e '_windows.go' \
|
|
|
|
-v\
|
|
|
|
|| exit 0
|
|
|
|
}
|
2020-12-08 15:23:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 15:30:57 +00: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 17:31:20 +00: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 17:06:34 +00:00
|
|
|
output="$( "$cmd" "$@" 2>&1 )"
|
2021-01-27 17:45:00 +00:00
|
|
|
exitcode="$?"
|
|
|
|
if [ "$exitcode" != '0' ]
|
|
|
|
then
|
|
|
|
echo "'$cmd' failed with code $exitcode"
|
|
|
|
fi
|
|
|
|
|
2020-12-08 15:23:35 +00:00
|
|
|
if [ "$output" != '' ]
|
|
|
|
then
|
|
|
|
if [ "$*" != '' ]
|
|
|
|
then
|
2021-05-21 16:20:42 +00:00
|
|
|
echo "combined output of '$cmd $*':"
|
2020-12-08 15:23:35 +00:00
|
|
|
else
|
|
|
|
echo "combined output of '$cmd':"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$output"
|
|
|
|
|
2021-05-19 17:31:20 +00: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
|
|
|
|
|
|
|
|
2021-03-26 10:03:36 +00:00
|
|
|
# Constants
|
|
|
|
|
2021-05-21 11:55:42 +00:00
|
|
|
go_files='./main.go ./internal/'
|
2021-05-19 17:31:20 +00:00
|
|
|
readonly go_files
|
2021-03-26 10:03:36 +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 17:11:06 +00:00
|
|
|
# Apply more lax standards to the code we haven't properly refactored yet.
|
|
|
|
gocyclo --over 17 ./internal/dhcpd/ ./internal/dnsforward/\
|
2021-08-27 11:50:37 +00:00
|
|
|
./internal/filtering/ ./internal/home/ ./internal/querylog/
|
2021-05-31 17:11:06 +00:00
|
|
|
|
2021-08-27 11:50:37 +00:00
|
|
|
# Apply stricter standards to new or somewhat refactored code.
|
2021-05-31 17:11:06 +00:00
|
|
|
gocyclo --over 10 ./internal/aghio/ ./internal/aghnet/ ./internal/aghos/\
|
2021-08-27 11:50:37 +00:00
|
|
|
./internal/aghtest/ ./internal/stats/ ./internal/tools/\
|
|
|
|
./internal/updater/ ./internal/version/ ./main.go
|
2020-12-08 15:23:35 +00:00
|
|
|
|
2021-03-26 10:03:36 +00:00
|
|
|
gosec --quiet $go_files
|
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 17:31:20 +00: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
|
|
|
|
2021-05-24 14:28:11 +00: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 ./...
|