badguardhome/scripts/make/go-lint.sh
Ainar Garipov fa33568fab Pull request: all: imp errcheck in tests
Merge in DNS/adguard-home from imp-errcheck to master

Squashed commit of the following:

commit ed046b8ef59a092a27c623cd14b3fc2ef305fc3d
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Feb 4 15:00:04 2021 +0300

    stats: imp cleanup more

commit e53a9240d3e3eec2417c768b98c267a8cd54d992
Merge: da734317 676f8c76
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Feb 4 14:59:40 2021 +0300

    Merge branch 'master' into imp-errcheck

commit da734317035543b52e5a9030813084bdc92ba90a
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Feb 4 14:37:26 2021 +0300

    stats: imp cleanup

commit 8b4ad150129111a09be6fa2944a21bd06ab8e5a1
Merge: 385c8a6c 5081ead0
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Feb 4 14:34:26 2021 +0300

    Merge branch 'master' into imp-errcheck

commit 385c8a6c91e3bf07a457da370c8cc77820b91600
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Jan 29 20:41:57 2021 +0300

    all: imp errcheck in tests
2021-02-04 15:12:34 +03:00

165 lines
2.9 KiB
Bash

#!/bin/sh
verbose="${VERBOSE:-0}"
# Verbosity levels:
# 0 = Don't print anything except for errors.
# 1 = Print commands, but not nested commands.
# 2 = Print everything.
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
# We don't need glob expansions and we want to see errors about unset
# variables.
set -f -u
# Deferred Helpers
not_found_msg='
looks like a binary not found error.
make sure you have installed the linter binaries using:
$ make go-tools
'
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
# Simple Analyzers
# blocklist_imports is a simple check against unwanted packages.
# Currently it only looks for package log which is replaced by our own
# package github.com/AdguardTeam/golibs/log.
blocklist_imports() {
git grep -F -e '"log"' -- '*.go' || exit 0;
}
# 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;
}
# underscores is a simple check against Go filenames with underscores.
underscores() {
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; }
}
# Helpers
# exit_on_output exits with a nonzero exit code if there is anything in
# the command's combined output.
exit_on_output() (
set +e
if [ "$VERBOSE" -lt '2' ]
then
set +x
fi
cmd="$1"
shift
output="$("$cmd" "$@" 2>&1)"
exitcode="$?"
if [ "$exitcode" != '0' ]
then
echo "'$cmd' failed with code $exitcode"
fi
if [ "$output" != '' ]
then
if [ "$*" != '' ]
then
echo "combined output of '$cmd $@':"
else
echo "combined output of '$cmd':"
fi
echo "$output"
if [ "$exitcode" = '0' ]
then
exitcode='1'
fi
fi
return "$exitcode"
)
# Checks
exit_on_output blocklist_imports
exit_on_output method_const
exit_on_output underscores
exit_on_output gofumpt --extra -l -s .
golint --set_exit_status ./...
"$GO" vet ./...
gocyclo --over 20 .
gosec --quiet .
ineffassign .
unparam ./...
git ls-files -- '*.go' '*.md' '*.mod' '*.sh' '*.yaml' '*.yml'\
'Makefile'\
| xargs misspell --error
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 and generated ones, properly. Also, perhaps,
# enable --blank.
#
# errcheck ./...
exit_on_output sh -c '
errcheck --asserts --ignoregenerated ./... |\
{ grep -e "defer" -v || exit 0; }
'
staticcheck ./...