2020-12-08 08:23:35 -07:00
|
|
|
#!/bin/sh
|
|
|
|
|
2020-12-30 08:26:25 -07:00
|
|
|
verbose="${VERBOSE:-0}"
|
|
|
|
|
2021-05-19 10:31:20 -07:00
|
|
|
# Set verbosity.
|
2020-12-30 08:26:25 -07:00
|
|
|
if [ "$verbose" -gt '0' ]
|
|
|
|
then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Set $EXIT_ON_ERROR to zero to see all errors.
|
2021-05-19 10:31:20 -07:00
|
|
|
if [ "${EXIT_ON_ERROR:-1}" -eq '0' ]
|
2020-12-30 08:26:25 -07:00
|
|
|
then
|
|
|
|
set +e
|
|
|
|
else
|
|
|
|
set -e
|
|
|
|
fi
|
2020-12-08 08:23:35 -07:00
|
|
|
|
2021-05-19 10:31:20 -07:00
|
|
|
# We don't need glob expansions and we want to see errors about unset variables.
|
2020-12-08 08:23:35 -07:00
|
|
|
set -f -u
|
|
|
|
|
2021-02-04 04:15:34 -07:00
|
|
|
|
|
|
|
|
|
|
|
# Deferred Helpers
|
|
|
|
|
2021-05-19 10:31:20 -07:00
|
|
|
not_found_msg='
|
2020-12-10 04:35:07 -07:00
|
|
|
looks like a binary not found error.
|
|
|
|
make sure you have installed the linter binaries using:
|
|
|
|
|
2020-12-30 08:26:25 -07:00
|
|
|
$ make go-tools
|
2020-12-10 04:35:07 -07:00
|
|
|
'
|
2021-05-19 10:31:20 -07:00
|
|
|
readonly not_found_msg
|
2020-12-10 04:35:07 -07:00
|
|
|
|
2021-05-19 10:31:20 -07:00
|
|
|
# TODO(a.garipov): Put it into a separate script and source it both here and in
|
|
|
|
# txt-lint.sh?
|
2020-12-10 04:35:07 -07:00
|
|
|
not_found() {
|
2021-05-19 10:31:20 -07:00
|
|
|
if [ "$?" -eq '127' ]
|
2020-12-10 04:35:07 -07:00
|
|
|
then
|
2021-05-19 10:31:20 -07: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 04:35:07 -07: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 04:15:34 -07:00
|
|
|
|
|
|
|
|
2021-04-22 10:06:34 -07:00
|
|
|
# Warnings
|
|
|
|
|
2022-01-19 05:06:23 -07:00
|
|
|
go_version="$( "${GO:-go}" version )"
|
2021-08-23 08:10:36 -07:00
|
|
|
readonly go_version
|
|
|
|
|
2022-08-03 04:36:18 -07:00
|
|
|
go_min_version='go1.18'
|
2021-05-19 10:31:20 -07:00
|
|
|
go_version_msg="
|
2021-08-23 08:10:36 -07:00
|
|
|
warning: your go version (${go_version}) is different from the recommended minimal one (${go_min_version}).
|
2021-04-22 10:06:34 -07:00
|
|
|
if you have the version installed, please set the GO environment variable.
|
|
|
|
for example:
|
|
|
|
|
|
|
|
export GO='${go_min_version}'
|
|
|
|
"
|
2021-06-25 08:30:30 -07:00
|
|
|
readonly go_min_version go_version_msg
|
2021-05-19 10:31:20 -07:00
|
|
|
|
2021-08-23 08:10:36 -07:00
|
|
|
case "$go_version"
|
2021-04-22 10:06:34 -07:00
|
|
|
in
|
2021-06-25 08:30:30 -07:00
|
|
|
('go version'*"$go_min_version"*)
|
2021-04-22 10:06:34 -07:00
|
|
|
# Go on.
|
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
echo "$go_version_msg" 1>&2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-04 04:15:34 -07:00
|
|
|
# Simple Analyzers
|
|
|
|
|
2021-07-29 07:40:31 -07:00
|
|
|
# blocklist_imports is a simple check against unwanted packages. The following
|
|
|
|
# packages are banned:
|
|
|
|
#
|
|
|
|
# * Packages errors and log are replaced by our own packages in the
|
|
|
|
# github.com/AdguardTeam/golibs module.
|
|
|
|
#
|
2022-01-19 05:06:23 -07:00
|
|
|
# * Package io/ioutil is soft-deprecated.
|
|
|
|
#
|
2021-07-29 07:40:31 -07:00
|
|
|
# * 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.
|
|
|
|
#
|
2022-01-19 05:06:23 -07:00
|
|
|
# * Package golang.org/x/net/context has been moved into stdlib.
|
|
|
|
#
|
2020-12-10 04:35:07 -07:00
|
|
|
blocklist_imports() {
|
2021-07-29 07:40:31 -07:00
|
|
|
git grep\
|
|
|
|
-e '[[:space:]]"errors"$'\
|
|
|
|
-e '[[:space:]]"io/ioutil"$'\
|
|
|
|
-e '[[:space:]]"log"$'\
|
|
|
|
-e '[[:space:]]"reflect"$'\
|
|
|
|
-e '[[:space:]]"unsafe"$'\
|
2022-01-19 05:06:23 -07:00
|
|
|
-e '[[:space:]]"golang.org/x/net/context"$'\
|
|
|
|
-n\
|
|
|
|
-- '*.go'\
|
|
|
|
| sed -e 's/^\([^[:space:]]\+\)\(.*\)$/\1 blocked import:\2/'\
|
|
|
|
|| exit 0
|
2020-12-08 08:23:35 -07:00
|
|
|
}
|
|
|
|
|
2021-05-19 10:31:20 -07: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 04:15:34 -07:00
|
|
|
method_const() {
|
2022-01-19 05:06:23 -07:00
|
|
|
git grep -F\
|
|
|
|
-e '"DELETE"'\
|
|
|
|
-e '"GET"'\
|
|
|
|
-e '"POST"'\
|
|
|
|
-e '"PUT"'\
|
|
|
|
-n\
|
|
|
|
-- '*.go'\
|
|
|
|
| sed -e 's/^\([^[:space:]]\+\)\(.*\)$/\1 http method literal:\2/'\
|
|
|
|
|| exit 0
|
2021-02-04 04:15:34 -07:00
|
|
|
}
|
|
|
|
|
2022-01-19 05:06:23 -07:00
|
|
|
# underscores is a simple check against Go filenames with underscores. Add new
|
|
|
|
# build tags and OS as you go. The main goal of this check is to discourage the
|
|
|
|
# use of filenames like client_manager.go.
|
2020-12-10 04:35:07 -07:00
|
|
|
underscores() {
|
2022-01-19 05:06:23 -07:00
|
|
|
underscore_files="$(
|
|
|
|
git ls-files '*_*.go'\
|
|
|
|
| grep -F\
|
|
|
|
-e '_big.go'\
|
|
|
|
-e '_bsd.go'\
|
|
|
|
-e '_darwin.go'\
|
|
|
|
-e '_freebsd.go'\
|
|
|
|
-e '_linux.go'\
|
|
|
|
-e '_little.go'\
|
2022-03-31 09:56:50 -07:00
|
|
|
-e '_openbsd.go'\
|
2022-01-19 05:06:23 -07:00
|
|
|
-e '_others.go'\
|
|
|
|
-e '_test.go'\
|
|
|
|
-e '_unix.go'\
|
2022-04-26 10:50:09 -07:00
|
|
|
-e '_v1.go'\
|
2022-01-19 05:06:23 -07:00
|
|
|
-e '_windows.go' \
|
|
|
|
-v\
|
|
|
|
| sed -e 's/./\t\0/'
|
|
|
|
)"
|
|
|
|
readonly underscore_files
|
|
|
|
|
|
|
|
if [ "$underscore_files" != '' ]
|
|
|
|
then
|
|
|
|
echo 'found file names with underscores:'
|
|
|
|
echo "$underscore_files"
|
|
|
|
fi
|
2020-12-08 08:23:35 -07:00
|
|
|
}
|
|
|
|
|
2022-08-02 10:48:14 -07:00
|
|
|
# TODO(a.garipov): Add an analyzer to look for `fallthrough`, `goto`, and `new`?
|
2021-05-21 08:30:57 -07:00
|
|
|
|
2021-02-04 04:15:34 -07:00
|
|
|
|
|
|
|
|
|
|
|
# Helpers
|
|
|
|
|
2021-05-19 10:31:20 -07:00
|
|
|
# exit_on_output exits with a nonzero exit code if there is anything in the
|
|
|
|
# command's combined output.
|
2021-01-27 10:45:00 -07:00
|
|
|
exit_on_output() (
|
|
|
|
set +e
|
|
|
|
|
|
|
|
if [ "$VERBOSE" -lt '2' ]
|
|
|
|
then
|
|
|
|
set +x
|
|
|
|
fi
|
2020-12-08 08:23:35 -07:00
|
|
|
|
|
|
|
cmd="$1"
|
|
|
|
shift
|
|
|
|
|
2021-04-22 10:06:34 -07:00
|
|
|
output="$( "$cmd" "$@" 2>&1 )"
|
2021-01-27 10:45:00 -07:00
|
|
|
exitcode="$?"
|
2022-01-19 05:06:23 -07:00
|
|
|
if [ "$exitcode" -ne '0' ]
|
2021-01-27 10:45:00 -07:00
|
|
|
then
|
|
|
|
echo "'$cmd' failed with code $exitcode"
|
|
|
|
fi
|
|
|
|
|
2020-12-08 08:23:35 -07:00
|
|
|
if [ "$output" != '' ]
|
|
|
|
then
|
|
|
|
if [ "$*" != '' ]
|
|
|
|
then
|
2022-01-19 05:06:23 -07:00
|
|
|
echo "combined output of linter '$cmd $*':"
|
2020-12-08 08:23:35 -07:00
|
|
|
else
|
2022-01-19 05:06:23 -07:00
|
|
|
echo "combined output of linter '$cmd':"
|
2020-12-08 08:23:35 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$output"
|
|
|
|
|
2021-05-19 10:31:20 -07:00
|
|
|
if [ "$exitcode" -eq '0' ]
|
2021-01-27 10:45:00 -07:00
|
|
|
then
|
|
|
|
exitcode='1'
|
|
|
|
fi
|
2020-12-08 08:23:35 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
return "$exitcode"
|
2021-01-27 10:45:00 -07:00
|
|
|
)
|
2020-12-08 08:23:35 -07:00
|
|
|
|
2021-02-04 04:15:34 -07:00
|
|
|
|
|
|
|
|
|
|
|
# Checks
|
|
|
|
|
2020-12-10 04:35:07 -07:00
|
|
|
exit_on_output blocklist_imports
|
2020-12-08 08:23:35 -07:00
|
|
|
|
2021-02-04 04:15:34 -07:00
|
|
|
exit_on_output method_const
|
|
|
|
|
2020-12-10 04:35:07 -07:00
|
|
|
exit_on_output underscores
|
2020-12-08 08:23:35 -07:00
|
|
|
|
2021-12-22 06:34:51 -07:00
|
|
|
exit_on_output gofumpt --extra -e -l .
|
2020-12-08 08:23:35 -07:00
|
|
|
|
2022-08-02 10:48:14 -07:00
|
|
|
# TODO(a.garipov): golint is deprecated, and seems to cause more and more
|
|
|
|
# issues with each release. Find a suitable replacement.
|
|
|
|
#
|
|
|
|
# golint --set_exit_status ./...
|
2020-12-08 08:23:35 -07:00
|
|
|
|
|
|
|
"$GO" vet ./...
|
|
|
|
|
2021-05-31 10:11:06 -07:00
|
|
|
# Apply more lax standards to the code we haven't properly refactored yet.
|
2022-09-05 07:01:33 -07:00
|
|
|
gocyclo --over 17 ./internal/querylog/
|
|
|
|
gocyclo --over 15 ./internal/home/ ./internal/dhcpd
|
2022-08-29 06:51:47 -07:00
|
|
|
gocyclo --over 13 ./internal/filtering/
|
2021-05-31 10:11:06 -07:00
|
|
|
|
2021-08-27 04:50:37 -07:00
|
|
|
# Apply stricter standards to new or somewhat refactored code.
|
2021-05-31 10:11:06 -07:00
|
|
|
gocyclo --over 10 ./internal/aghio/ ./internal/aghnet/ ./internal/aghos/\
|
2022-09-06 09:03:47 -07:00
|
|
|
./internal/aghtest/ ./internal/dnsforward/ ./internal/stats/\
|
|
|
|
./internal/tools/ ./internal/updater/ ./internal/v1/ ./internal/version/\
|
|
|
|
./main.go
|
2020-12-08 08:23:35 -07:00
|
|
|
|
2021-03-26 03:03:36 -07:00
|
|
|
ineffassign ./...
|
2020-12-08 08:23:35 -07:00
|
|
|
|
|
|
|
unparam ./...
|
|
|
|
|
2021-05-19 10:31:20 -07:00
|
|
|
git ls-files -- '*.go' '*.mod' '*.sh' 'Makefile' | xargs misspell --error
|
2020-12-08 08:23:35 -07:00
|
|
|
|
|
|
|
looppointer ./...
|
|
|
|
|
|
|
|
nilness ./...
|
|
|
|
|
2021-03-12 04:32:08 -07:00
|
|
|
exit_on_output shadow --strict ./...
|
2020-12-08 08:23:35 -07:00
|
|
|
|
2022-01-19 05:06:23 -07:00
|
|
|
# TODO(a.garipov): Enable in v0.108.0.
|
|
|
|
# gosec --quiet ./...
|
|
|
|
|
2021-05-24 07:28:11 -07:00
|
|
|
# TODO(a.garipov): Enable --blank?
|
|
|
|
errcheck --asserts ./...
|
2020-12-08 08:23:35 -07:00
|
|
|
|
2020-12-17 03:32:46 -07:00
|
|
|
staticcheck ./...
|