2020-12-08 08:23:35 -07:00
|
|
|
#!/bin/sh
|
|
|
|
|
2023-02-21 06:38:22 -07:00
|
|
|
# This comment is used to simplify checking local copies of the script. Bump
|
|
|
|
# this number every time a significant change is made to this script.
|
|
|
|
#
|
|
|
|
# AdGuard-Project-Version: 3
|
|
|
|
|
2020-12-30 08:26:25 -07:00
|
|
|
verbose="${VERBOSE:-0}"
|
2023-02-21 06:38:22 -07:00
|
|
|
readonly verbose
|
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
|
|
|
|
|
|
|
set -f -u
|
|
|
|
|
2021-02-04 04:15:34 -07:00
|
|
|
|
|
|
|
|
2023-02-21 06:38:22 -07:00
|
|
|
# Source the common helpers, including not_found and run_linter.
|
|
|
|
. ./scripts/make/helper.sh
|
2020-12-10 04:35:07 -07:00
|
|
|
|
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
|
|
|
|
|
2023-04-05 06:12:09 -07:00
|
|
|
go_min_version='go1.19.8'
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-02-21 06:38:22 -07:00
|
|
|
# Simple analyzers
|
2021-02-04 04:15:34 -07:00
|
|
|
|
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.
|
|
|
|
#
|
2023-02-21 06:38:22 -07:00
|
|
|
# * Package sort is replaced by golang.org/x/exp/slices.
|
|
|
|
#
|
2021-07-29 07:40:31 -07:00
|
|
|
# * 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"$'\
|
2023-02-21 06:38:22 -07:00
|
|
|
-e '[[:space:]]"sort"$'\
|
2021-07-29 07:40:31 -07:00
|
|
|
-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-10-04 06:02:55 -07:00
|
|
|
-e '_next.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'\
|
|
|
|
-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
|
|
|
|
|
|
|
|
|
|
|
# Checks
|
|
|
|
|
2023-02-21 06:38:22 -07:00
|
|
|
run_linter -e blocklist_imports
|
2020-12-08 08:23:35 -07:00
|
|
|
|
2023-02-21 06:38:22 -07:00
|
|
|
run_linter -e method_const
|
2021-02-04 04:15:34 -07:00
|
|
|
|
2023-02-21 06:38:22 -07:00
|
|
|
run_linter -e underscores
|
2020-12-08 08:23:35 -07:00
|
|
|
|
2023-02-21 06:38:22 -07:00
|
|
|
run_linter -e gofumpt --extra -e -l .
|
2020-12-08 08:23:35 -07:00
|
|
|
|
2023-02-21 06:38:22 -07:00
|
|
|
# TODO(a.garipov): golint is deprecated, find a suitable replacement.
|
2020-12-08 08:23:35 -07:00
|
|
|
|
2023-02-21 06:38:22 -07:00
|
|
|
run_linter "$GO" vet ./...
|
2020-12-08 08:23:35 -07:00
|
|
|
|
2023-02-21 06:38:22 -07:00
|
|
|
run_linter govulncheck ./...
|
2022-09-07 04:01:03 -07:00
|
|
|
|
2023-05-26 02:50:03 -07:00
|
|
|
run_linter gocyclo --over 10 .
|
2023-02-21 06:38:22 -07:00
|
|
|
|
|
|
|
run_linter ineffassign ./...
|
|
|
|
|
|
|
|
run_linter unparam ./...
|
|
|
|
|
|
|
|
git ls-files -- 'Makefile' '*.go' '*.mod' '*.sh' '*.yaml' '*.yml'\
|
|
|
|
| xargs misspell --error
|
|
|
|
|
|
|
|
run_linter looppointer ./...
|
|
|
|
|
|
|
|
run_linter nilness ./...
|
|
|
|
|
|
|
|
# TODO(a.garipov): Add fieldalignment?
|
|
|
|
|
|
|
|
run_linter -e 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.
|
2023-02-21 06:38:22 -07:00
|
|
|
# run_linter gosec --quiet ./...
|
2022-01-19 05:06:23 -07:00
|
|
|
|
2021-05-24 07:28:11 -07:00
|
|
|
# TODO(a.garipov): Enable --blank?
|
2023-02-21 06:38:22 -07:00
|
|
|
run_linter errcheck --asserts ./...
|
2020-12-08 08:23:35 -07:00
|
|
|
|
2023-02-21 06:38:22 -07:00
|
|
|
run_linter staticcheck ./...
|