2020-12-08 18:23:35 +03:00
|
|
|
#!/bin/sh
|
|
|
|
|
2020-12-30 18:26:25 +03:00
|
|
|
verbose="${VERBOSE:-0}"
|
|
|
|
|
2021-05-19 20:31:20 +03:00
|
|
|
# Set verbosity.
|
2020-12-30 18:26:25 +03:00
|
|
|
if [ "$verbose" -gt '0' ]
|
|
|
|
then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Set $EXIT_ON_ERROR to zero to see all errors.
|
2021-05-19 20:31:20 +03:00
|
|
|
if [ "${EXIT_ON_ERROR:-1}" -eq '0' ]
|
2020-12-30 18:26:25 +03:00
|
|
|
then
|
|
|
|
set +e
|
|
|
|
else
|
|
|
|
set -e
|
|
|
|
fi
|
2020-12-08 18:23:35 +03:00
|
|
|
|
2021-05-19 20:31:20 +03:00
|
|
|
# We don't need glob expansions and we want to see errors about unset variables.
|
2020-12-08 18:23:35 +03:00
|
|
|
set -f -u
|
|
|
|
|
2021-02-04 14:15:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
# Deferred Helpers
|
|
|
|
|
2021-05-19 20:31:20 +03:00
|
|
|
not_found_msg='
|
2020-12-10 14:35:07 +03:00
|
|
|
looks like a binary not found error.
|
|
|
|
make sure you have installed the linter binaries using:
|
|
|
|
|
2020-12-30 18:26:25 +03:00
|
|
|
$ make go-tools
|
2020-12-10 14:35:07 +03:00
|
|
|
'
|
2021-05-19 20:31:20 +03:00
|
|
|
readonly not_found_msg
|
2020-12-10 14:35:07 +03:00
|
|
|
|
2021-05-19 20:31:20 +03:00
|
|
|
# TODO(a.garipov): Put it into a separate script and source it both here and in
|
|
|
|
# txt-lint.sh?
|
2020-12-10 14:35:07 +03:00
|
|
|
not_found() {
|
2021-05-19 20:31:20 +03:00
|
|
|
if [ "$?" -eq '127' ]
|
2020-12-10 14:35:07 +03:00
|
|
|
then
|
2021-05-19 20:31:20 +03: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 14:35:07 +03: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 14:15:34 +03:00
|
|
|
|
|
|
|
|
2021-04-22 20:06:34 +03:00
|
|
|
# Warnings
|
|
|
|
|
2021-08-23 18:10:36 +03:00
|
|
|
go_version="$( "$GO" version )"
|
|
|
|
readonly go_version
|
|
|
|
|
2021-12-22 16:34:51 +03:00
|
|
|
go_min_version='go1.17'
|
2021-05-19 20:31:20 +03:00
|
|
|
go_version_msg="
|
2021-08-23 18:10:36 +03:00
|
|
|
warning: your go version (${go_version}) is different from the recommended minimal one (${go_min_version}).
|
2021-04-22 20:06:34 +03:00
|
|
|
if you have the version installed, please set the GO environment variable.
|
|
|
|
for example:
|
|
|
|
|
|
|
|
export GO='${go_min_version}'
|
|
|
|
"
|
2021-06-25 18:30:30 +03:00
|
|
|
readonly go_min_version go_version_msg
|
2021-05-19 20:31:20 +03:00
|
|
|
|
2021-08-23 18:10:36 +03:00
|
|
|
|
|
|
|
case "$go_version"
|
2021-04-22 20:06:34 +03:00
|
|
|
in
|
2021-06-25 18:30:30 +03:00
|
|
|
('go version'*"$go_min_version"*)
|
2021-04-22 20:06:34 +03:00
|
|
|
# Go on.
|
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
echo "$go_version_msg" 1>&2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-04 14:15:34 +03:00
|
|
|
# Simple Analyzers
|
|
|
|
|
2021-07-29 17:40:31 +03: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 14:35:07 +03:00
|
|
|
blocklist_imports() {
|
2021-07-29 17:40:31 +03: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 18:23:35 +03:00
|
|
|
}
|
|
|
|
|
2021-05-19 20:31:20 +03: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 14:15:34 +03:00
|
|
|
method_const() {
|
|
|
|
git grep -F -e '"GET"' -e '"POST"' -- '*.go' || exit 0;
|
|
|
|
}
|
|
|
|
|
2020-12-08 18:23:35 +03:00
|
|
|
# underscores is a simple check against Go filenames with underscores.
|
2020-12-10 14:35:07 +03:00
|
|
|
underscores() {
|
2021-03-16 21:00:17 +03:00
|
|
|
git ls-files '*_*.go' | {
|
|
|
|
grep -F\
|
|
|
|
-e '_big.go'\
|
2021-05-17 13:27:40 +03:00
|
|
|
-e '_bsd.go'\
|
2021-03-16 21:00:17 +03:00
|
|
|
-e '_darwin.go'\
|
|
|
|
-e '_freebsd.go'\
|
2021-08-12 17:33:53 +03:00
|
|
|
-e '_openbsd.go'\
|
2021-03-16 21:00:17 +03: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 18:23:35 +03:00
|
|
|
}
|
|
|
|
|
2021-05-21 18:30:57 +03:00
|
|
|
# TODO(a.garipov): Add an analyser to look for `fallthrough`, `goto`, and `new`?
|
|
|
|
|
2021-02-04 14:15:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
# Helpers
|
|
|
|
|
2021-05-19 20:31:20 +03:00
|
|
|
# exit_on_output exits with a nonzero exit code if there is anything in the
|
|
|
|
# command's combined output.
|
2021-01-27 20:45:00 +03:00
|
|
|
exit_on_output() (
|
|
|
|
set +e
|
|
|
|
|
|
|
|
if [ "$VERBOSE" -lt '2' ]
|
|
|
|
then
|
|
|
|
set +x
|
|
|
|
fi
|
2020-12-08 18:23:35 +03:00
|
|
|
|
|
|
|
cmd="$1"
|
|
|
|
shift
|
|
|
|
|
2021-04-22 20:06:34 +03:00
|
|
|
output="$( "$cmd" "$@" 2>&1 )"
|
2021-01-27 20:45:00 +03:00
|
|
|
exitcode="$?"
|
|
|
|
if [ "$exitcode" != '0' ]
|
|
|
|
then
|
|
|
|
echo "'$cmd' failed with code $exitcode"
|
|
|
|
fi
|
|
|
|
|
2020-12-08 18:23:35 +03:00
|
|
|
if [ "$output" != '' ]
|
|
|
|
then
|
|
|
|
if [ "$*" != '' ]
|
|
|
|
then
|
2021-05-21 19:20:42 +03:00
|
|
|
echo "combined output of '$cmd $*':"
|
2020-12-08 18:23:35 +03:00
|
|
|
else
|
|
|
|
echo "combined output of '$cmd':"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$output"
|
|
|
|
|
2021-05-19 20:31:20 +03:00
|
|
|
if [ "$exitcode" -eq '0' ]
|
2021-01-27 20:45:00 +03:00
|
|
|
then
|
|
|
|
exitcode='1'
|
|
|
|
fi
|
2020-12-08 18:23:35 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
return "$exitcode"
|
2021-01-27 20:45:00 +03:00
|
|
|
)
|
2020-12-08 18:23:35 +03:00
|
|
|
|
2021-02-04 14:15:34 +03:00
|
|
|
|
|
|
|
|
2021-03-26 13:03:36 +03:00
|
|
|
# Constants
|
|
|
|
|
2021-05-21 14:55:42 +03:00
|
|
|
go_files='./main.go ./internal/'
|
2021-05-19 20:31:20 +03:00
|
|
|
readonly go_files
|
2021-03-26 13:03:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-04 14:15:34 +03:00
|
|
|
# Checks
|
|
|
|
|
2020-12-10 14:35:07 +03:00
|
|
|
exit_on_output blocklist_imports
|
2020-12-08 18:23:35 +03:00
|
|
|
|
2021-02-04 14:15:34 +03:00
|
|
|
exit_on_output method_const
|
|
|
|
|
2020-12-10 14:35:07 +03:00
|
|
|
exit_on_output underscores
|
2020-12-08 18:23:35 +03:00
|
|
|
|
2021-12-22 16:34:51 +03:00
|
|
|
exit_on_output gofumpt --extra -e -l .
|
2020-12-08 18:23:35 +03:00
|
|
|
|
|
|
|
golint --set_exit_status ./...
|
|
|
|
|
|
|
|
"$GO" vet ./...
|
|
|
|
|
2021-05-31 20:11:06 +03:00
|
|
|
# Apply more lax standards to the code we haven't properly refactored yet.
|
|
|
|
gocyclo --over 17 ./internal/dhcpd/ ./internal/dnsforward/\
|
2021-08-27 14:50:37 +03:00
|
|
|
./internal/filtering/ ./internal/home/ ./internal/querylog/
|
2021-05-31 20:11:06 +03:00
|
|
|
|
2021-08-27 14:50:37 +03:00
|
|
|
# Apply stricter standards to new or somewhat refactored code.
|
2021-05-31 20:11:06 +03:00
|
|
|
gocyclo --over 10 ./internal/aghio/ ./internal/aghnet/ ./internal/aghos/\
|
2021-08-27 14:50:37 +03:00
|
|
|
./internal/aghtest/ ./internal/stats/ ./internal/tools/\
|
|
|
|
./internal/updater/ ./internal/version/ ./main.go
|
2020-12-08 18:23:35 +03:00
|
|
|
|
2021-03-26 13:03:36 +03:00
|
|
|
gosec --quiet $go_files
|
2020-12-08 18:23:35 +03:00
|
|
|
|
2021-03-26 13:03:36 +03:00
|
|
|
ineffassign ./...
|
2020-12-08 18:23:35 +03:00
|
|
|
|
|
|
|
unparam ./...
|
|
|
|
|
2021-05-19 20:31:20 +03:00
|
|
|
git ls-files -- '*.go' '*.mod' '*.sh' 'Makefile' | xargs misspell --error
|
2020-12-08 18:23:35 +03:00
|
|
|
|
|
|
|
looppointer ./...
|
|
|
|
|
|
|
|
nilness ./...
|
|
|
|
|
2021-03-12 14:32:08 +03:00
|
|
|
exit_on_output shadow --strict ./...
|
2020-12-08 18:23:35 +03:00
|
|
|
|
2021-05-24 17:28:11 +03:00
|
|
|
# TODO(a.garipov): Enable --blank?
|
|
|
|
errcheck --asserts ./...
|
2020-12-08 18:23:35 +03:00
|
|
|
|
2020-12-17 13:32:46 +03:00
|
|
|
staticcheck ./...
|