AdGuardHome/scripts/make/go-lint.sh
Ainar Garipov c6888326b0 Pull request: all: update go and backend tools
Closes #2576.
Updates #2275.
Updates #2419.
Updates #2443.

Squashed commit of the following:

commit b1a4809ada298d675de12740051ba26fb9945957
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri May 21 14:01:40 2021 +0300

    all: add --local-frontend, upd docker

commit 619ee7c82f27e3405753003dbec556ffb056d025
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu May 20 15:02:33 2021 +0300

    bamboo-specs: bump docker version

commit 5c2b2fbce80afdcc81fd0cb83674dc3d64facbf1
Merge: 6536b32d 9c60aef6
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu May 20 15:01:47 2021 +0300

    Merge branch 'master' into 2275-upd-go

commit 6536b32dd4580425f7dedde6765463a79b9bd699
Merge: 9bb32bc4 6f7fd33a
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed May 19 20:38:48 2021 +0300

    Merge branch 'master' into 2275-upd-go

commit 9bb32bc4c0ac0f3a97195adc75359e48c9c58897
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed May 19 18:48:50 2021 +0300

    all: fix build, imp err handling

commit 6868eac7f7d2980fb706881f53e72afe5f7c3447
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed May 19 18:09:32 2021 +0300

    all: fix github lint

commit ebbb9c55f32fbd57e34e8b161016aa6b291c097c
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed May 19 17:36:56 2021 +0300

    all: update go and backend tools
2021-05-21 14:55:42 +03:00

202 lines
3.5 KiB
Bash

#!/bin/sh
verbose="${VERBOSE:-0}"
# Set verbosity.
if [ "$verbose" -gt '0' ]
then
set -x
fi
# Set $EXIT_ON_ERROR to zero to see all errors.
if [ "${EXIT_ON_ERROR:-1}" -eq '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
'
readonly not_found_msg
# TODO(a.garipov): Put it into a separate script and source it both here and in
# txt-lint.sh?
not_found() {
if [ "$?" -eq '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
# Warnings
go_min_version='go1.16'
go_min_version_prefix="go version ${go_min_version}"
go_version_msg="
warning: your go version is different from the recommended minimal one (${go_min_version}).
if you have the version installed, please set the GO environment variable.
for example:
export GO='${go_min_version}'
"
readonly go_min_version go_min_version_prefix go_version_msg
case "$( "$GO" version )"
in
("$go_min_version_prefix"*)
# Go on.
;;
(*)
echo "$go_version_msg" 1>&2
;;
esac
# Simple Analyzers
# blocklist_imports is a simple check against unwanted packages. Package
# io/ioutil is soft-deprecated. Package log is replaced by our own package
# github.com/AdguardTeam/golibs/log.
blocklist_imports() {
git grep -F -e '"io/ioutil"' -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 '_big.go'\
-e '_bsd.go'\
-e '_darwin.go'\
-e '_freebsd.go'\
-e '_linux.go'\
-e '_little.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" -eq '0' ]
then
exitcode='1'
fi
fi
return "$exitcode"
)
# Constants
go_files='./main.go ./internal/'
readonly go_files
# 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 ./...
# Here and below, don't use quotes to get word splitting.
gocyclo --over 17 $go_files
gosec --quiet $go_files
ineffassign ./...
unparam ./...
git ls-files -- '*.go' '*.mod' '*.sh' 'Makefile' | xargs misspell --error
looppointer ./...
nilness ./...
exit_on_output 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 ./...