cherry-pick: scripts: imp sh lint

This commit is contained in:
Ainar Garipov 2022-01-19 15:06:23 +03:00
parent de1a7ce48f
commit e28186a28a
1 changed files with 52 additions and 35 deletions

View File

@ -49,7 +49,7 @@ trap not_found EXIT
# Warnings # Warnings
go_version="$( "$GO" version )" go_version="$( "${GO:-go}" version )"
readonly go_version readonly go_version
go_min_version='go1.16' go_min_version='go1.16'
@ -62,7 +62,6 @@ for example:
" "
readonly go_min_version go_version_msg readonly go_min_version go_version_msg
case "$go_version" case "$go_version"
in in
('go version'*"$go_min_version"*) ('go version'*"$go_min_version"*)
@ -80,11 +79,11 @@ esac
# blocklist_imports is a simple check against unwanted packages. The following # blocklist_imports is a simple check against unwanted packages. The following
# packages are banned: # packages are banned:
# #
# * Package io/ioutil is soft-deprecated.
#
# * Packages errors and log are replaced by our own packages in the # * Packages errors and log are replaced by our own packages in the
# github.com/AdguardTeam/golibs module. # github.com/AdguardTeam/golibs module.
# #
# * Package io/ioutil is soft-deprecated.
#
# * Package reflect is often an overkill, and for deep comparisons there are # * Package reflect is often an overkill, and for deep comparisons there are
# much better functions in module github.com/google/go-cmp. Which is # 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 # already our indirect dependency and which may or may not enter the stdlib
@ -94,6 +93,8 @@ esac
# #
# * Package unsafe is… unsafe. # * Package unsafe is… unsafe.
# #
# * Package golang.org/x/net/context has been moved into stdlib.
#
blocklist_imports() { blocklist_imports() {
git grep\ git grep\
-e '[[:space:]]"errors"$'\ -e '[[:space:]]"errors"$'\
@ -101,33 +102,55 @@ blocklist_imports() {
-e '[[:space:]]"log"$'\ -e '[[:space:]]"log"$'\
-e '[[:space:]]"reflect"$'\ -e '[[:space:]]"reflect"$'\
-e '[[:space:]]"unsafe"$'\ -e '[[:space:]]"unsafe"$'\
-- '*.go' || exit 0; -e '[[:space:]]"golang.org/x/net/context"$'\
-n\
-- '*.go'\
| sed -e 's/^\([^[:space:]]\+\)\(.*\)$/\1 blocked import:\2/'\
|| exit 0
} }
# method_const is a simple check against the usage of some raw strings and # method_const is a simple check against the usage of some raw strings and
# numbers where one should use named constants. # numbers where one should use named constants.
method_const() { method_const() {
git grep -F -e '"GET"' -e '"POST"' -- '*.go' || exit 0; git grep -F\
-e '"DELETE"'\
-e '"GET"'\
-e '"POST"'\
-e '"PUT"'\
-n\
-- '*.go'\
| sed -e 's/^\([^[:space:]]\+\)\(.*\)$/\1 http method literal:\2/'\
|| exit 0
} }
# underscores is a simple check against Go filenames with underscores. # 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.
underscores() { underscores() {
git ls-files '*_*.go' | { underscore_files="$(
grep -F\ git ls-files '*_*.go'\
-e '_big.go'\ | grep -F\
-e '_bsd.go'\ -e '_big.go'\
-e '_darwin.go'\ -e '_bsd.go'\
-e '_freebsd.go'\ -e '_darwin.go'\
-e '_openbsd.go'\ -e '_freebsd.go'\
-e '_linux.go'\ -e '_openbsd.go'\
-e '_little.go'\ -e '_linux.go'\
-e '_others.go'\ -e '_little.go'\
-e '_test.go'\ -e '_others.go'\
-e '_unix.go'\ -e '_test.go'\
-e '_windows.go' \ -e '_unix.go'\
-v\ -e '_windows.go' \
|| exit 0 -v\
} | sed -e 's/./\t\0/'
)"
readonly underscore_files
if [ "$underscore_files" != '' ]
then
echo 'found file names with underscores:'
echo "$underscore_files"
fi
} }
# TODO(a.garipov): Add an analyser to look for `fallthrough`, `goto`, and `new`? # TODO(a.garipov): Add an analyser to look for `fallthrough`, `goto`, and `new`?
@ -151,7 +174,7 @@ exit_on_output() (
output="$( "$cmd" "$@" 2>&1 )" output="$( "$cmd" "$@" 2>&1 )"
exitcode="$?" exitcode="$?"
if [ "$exitcode" != '0' ] if [ "$exitcode" -ne '0' ]
then then
echo "'$cmd' failed with code $exitcode" echo "'$cmd' failed with code $exitcode"
fi fi
@ -160,9 +183,9 @@ exit_on_output() (
then then
if [ "$*" != '' ] if [ "$*" != '' ]
then then
echo "combined output of '$cmd $*':" echo "combined output of linter '$cmd $*':"
else else
echo "combined output of '$cmd':" echo "combined output of linter '$cmd':"
fi fi
echo "$output" echo "$output"
@ -178,13 +201,6 @@ exit_on_output() (
# Constants
go_files='./main.go ./internal/'
readonly go_files
# Checks # Checks
exit_on_output blocklist_imports exit_on_output blocklist_imports
@ -208,8 +224,6 @@ gocyclo --over 10 ./internal/aghio/ ./internal/aghnet/ ./internal/aghos/\
./internal/aghtest/ ./internal/stats/ ./internal/tools/\ ./internal/aghtest/ ./internal/stats/ ./internal/tools/\
./internal/updater/ ./internal/version/ ./main.go ./internal/updater/ ./internal/version/ ./main.go
gosec --quiet $go_files
ineffassign ./... ineffassign ./...
unparam ./... unparam ./...
@ -222,6 +236,9 @@ nilness ./...
exit_on_output shadow --strict ./... exit_on_output shadow --strict ./...
# TODO(a.garipov): Enable in v0.108.0.
# gosec --quiet ./...
# TODO(a.garipov): Enable --blank? # TODO(a.garipov): Enable --blank?
errcheck --asserts ./... errcheck --asserts ./...