2020-12-08 15:23:35 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2020-12-30 15:26:25 +00:00
|
|
|
verbose="${VERBOSE:-0}"
|
|
|
|
|
2020-12-08 15:23:35 +00:00
|
|
|
# Verbosity levels:
|
|
|
|
# 0 = Don't print anything except for errors.
|
|
|
|
# 1 = Print commands, but not nested commands.
|
|
|
|
# 2 = Print everything.
|
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.
|
|
|
|
if [ "${EXIT_ON_ERROR:-1}" = '0' ]
|
|
|
|
then
|
|
|
|
set +e
|
|
|
|
else
|
|
|
|
set -e
|
|
|
|
fi
|
2020-12-08 15:23:35 +00:00
|
|
|
|
|
|
|
# We don't need glob expansions and we want to see errors about unset
|
|
|
|
# variables.
|
|
|
|
set -f -u
|
|
|
|
|
2021-02-04 11:15:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Deferred Helpers
|
|
|
|
|
2021-04-22 17:06:34 +00:00
|
|
|
readonly 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
|
|
|
'
|
|
|
|
|
|
|
|
not_found() {
|
|
|
|
if [ "$?" = '127' ]
|
|
|
|
then
|
|
|
|
# Code 127 is the exit status a shell uses when
|
|
|
|
# a command or a file is not found, according to the
|
|
|
|
# Bash Hackers wiki.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
|
|
|
readonly go_min_version='go1.15'
|
|
|
|
readonly go_min_version_prefix="go version ${go_min_version}"
|
|
|
|
readonly go_version_msg="
|
|
|
|
warning: your go version different from the recommended minimal one (${go_min_version}).
|
|
|
|
if you have the version installed, please set the GO environment variable.
|
|
|
|
for example:
|
|
|
|
|
|
|
|
export GO='${go_min_version}'
|
|
|
|
"
|
|
|
|
case "$( "$GO" version )"
|
|
|
|
in
|
|
|
|
("$go_min_version_prefix"*)
|
|
|
|
# Go on.
|
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
echo "$go_version_msg" 1>&2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-04 11:15:34 +00:00
|
|
|
# Simple Analyzers
|
|
|
|
|
2020-12-10 11:35:07 +00:00
|
|
|
# blocklist_imports is a simple check against unwanted packages.
|
2020-12-08 15:23:35 +00:00
|
|
|
# Currently it only looks for package log which is replaced by our own
|
|
|
|
# package github.com/AdguardTeam/golibs/log.
|
2020-12-10 11:35:07 +00:00
|
|
|
blocklist_imports() {
|
2020-12-08 15:23:35 +00:00
|
|
|
git grep -F -e '"log"' -- '*.go' || exit 0;
|
|
|
|
}
|
|
|
|
|
2021-02-04 11:15:34 +00:00
|
|
|
# method_const is a simple check against the usage of some raw strings
|
|
|
|
# and numbers where one should use named constants.
|
|
|
|
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'\
|
|
|
|
-e '_darwin.go'\
|
|
|
|
-e '_freebsd.go'\
|
|
|
|
-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-02-04 11:15:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Helpers
|
|
|
|
|
2020-12-10 11:35:07 +00:00
|
|
|
# exit_on_output exits with a nonzero exit code if there is anything in
|
2020-12-08 15:23:35 +00:00
|
|
|
# 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
|
|
|
|
echo "combined output of '$cmd $@':"
|
|
|
|
else
|
|
|
|
echo "combined output of '$cmd':"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$output"
|
|
|
|
|
2021-01-27 17:45:00 +00:00
|
|
|
if [ "$exitcode" = '0' ]
|
|
|
|
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
|
|
|
|
|
|
|
|
readonly go_files='./main.go ./tools.go ./internal/'
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-12-10 11:35:07 +00:00
|
|
|
exit_on_output gofumpt --extra -l -s .
|
2020-12-08 15:23:35 +00:00
|
|
|
|
|
|
|
golint --set_exit_status ./...
|
|
|
|
|
|
|
|
"$GO" vet ./...
|
|
|
|
|
2021-03-26 10:03:36 +00:00
|
|
|
# Here and below, don't use quotes to get word splitting.
|
2021-03-26 10:29:33 +00:00
|
|
|
gocyclo --over 17 $go_files
|
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-04-09 12:07:18 +00:00
|
|
|
git ls-files -- '*.go' '*.md' '*.mod' '*.sh' '*.yaml' '*.yml' 'Makefile'\
|
2020-12-30 15:26:25 +00:00
|
|
|
| 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
|
|
|
|
|
|
|
# TODO(a.garipov): Enable errcheck fully after handling all errors,
|
2021-01-11 12:59:42 +00:00
|
|
|
# including the deferred and generated ones, properly. Also, perhaps,
|
|
|
|
# enable --blank.
|
|
|
|
#
|
2020-12-08 15:23:35 +00:00
|
|
|
# errcheck ./...
|
2020-12-10 11:35:07 +00:00
|
|
|
exit_on_output sh -c '
|
2021-01-11 12:59:42 +00:00
|
|
|
errcheck --asserts --ignoregenerated ./... |\
|
2021-02-04 12:12:34 +00:00
|
|
|
{ grep -e "defer" -v || exit 0; }
|
2020-12-08 15:23:35 +00:00
|
|
|
'
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
staticcheck ./...
|