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
|
|
|
|
|
2020-12-10 11:35:07 +00:00
|
|
|
not_found_msg='
|
|
|
|
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
|
|
|
|
|
|
|
|
# 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
# underscores is a simple check against Go filenames with underscores.
|
2020-12-10 11:35:07 +00:00
|
|
|
underscores() {
|
2020-12-08 15:23:35 +00:00
|
|
|
git ls-files '*_*.go' | { grep -F -e '_darwin.go' \
|
|
|
|
-e '_freebsd.go' -e '_linux.go' -e '_others.go' \
|
|
|
|
-e '_test.go' -e '_unix.go' -e '_windows.go' \
|
|
|
|
-v || exit 0; }
|
|
|
|
}
|
|
|
|
|
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.
|
2020-12-10 11:35:07 +00:00
|
|
|
exit_on_output() {
|
2020-12-08 15:23:35 +00:00
|
|
|
test "$VERBOSE" -lt '2' && set +x
|
|
|
|
|
|
|
|
cmd="$1"
|
|
|
|
shift
|
|
|
|
|
|
|
|
exitcode='0'
|
|
|
|
output="$("$cmd" "$@" 2>&1)"
|
|
|
|
if [ "$output" != '' ]
|
|
|
|
then
|
|
|
|
if [ "$*" != '' ]
|
|
|
|
then
|
|
|
|
echo "combined output of '$cmd $@':"
|
|
|
|
else
|
|
|
|
echo "combined output of '$cmd':"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$output"
|
|
|
|
|
|
|
|
exitcode='1'
|
|
|
|
fi
|
|
|
|
|
|
|
|
test "$VERBOSE" -gt '0' && set -x
|
|
|
|
|
|
|
|
return "$exitcode"
|
|
|
|
}
|
|
|
|
|
2020-12-10 11:35:07 +00:00
|
|
|
exit_on_output blocklist_imports
|
2020-12-08 15:23:35 +00:00
|
|
|
|
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 ./...
|
|
|
|
|
|
|
|
gocyclo --over 20 .
|
|
|
|
|
|
|
|
gosec --quiet .
|
|
|
|
|
|
|
|
ineffassign .
|
|
|
|
|
|
|
|
unparam ./...
|
|
|
|
|
2020-12-30 15:26:25 +00:00
|
|
|
git ls-files -- '*.go' '*.md' '*.yaml' '*.yml' 'Makefile'\
|
|
|
|
| xargs misspell --error
|
2020-12-08 15:23:35 +00:00
|
|
|
|
|
|
|
looppointer ./...
|
|
|
|
|
|
|
|
nilness ./...
|
|
|
|
|
|
|
|
# TODO(a.garipov): Enable shadow after fixing all of the shadowing.
|
|
|
|
# shadow --strict ./...
|
|
|
|
|
|
|
|
# TODO(a.garipov): Enable errcheck fully after handling all errors,
|
|
|
|
# including the deferred ones, properly. Also, perhaps, enable --blank.
|
|
|
|
# errcheck ./...
|
2020-12-10 11:35:07 +00:00
|
|
|
exit_on_output sh -c '
|
2020-12-08 15:23:35 +00:00
|
|
|
errcheck --asserts ./... |\
|
|
|
|
{ grep -e "defer" -e "_test\.go:" -v || exit 0; }
|
|
|
|
'
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
staticcheck ./...
|