From e28186a28a6efd174972e326f90cc43b8ef7e254 Mon Sep 17 00:00:00 2001
From: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Wed, 19 Jan 2022 15:06:23 +0300
Subject: [PATCH] cherry-pick: scripts: imp sh lint

---
 scripts/make/go-lint.sh | 87 ++++++++++++++++++++++++-----------------
 1 file changed, 52 insertions(+), 35 deletions(-)

diff --git a/scripts/make/go-lint.sh b/scripts/make/go-lint.sh
index 6ed9b503..4021d773 100644
--- a/scripts/make/go-lint.sh
+++ b/scripts/make/go-lint.sh
@@ -49,7 +49,7 @@ trap not_found EXIT
 
 # Warnings
 
-go_version="$( "$GO" version )"
+go_version="$( "${GO:-go}" version )"
 readonly go_version
 
 go_min_version='go1.16'
@@ -62,7 +62,6 @@ for example:
 "
 readonly go_min_version go_version_msg
 
-
 case "$go_version"
 in
 ('go version'*"$go_min_version"*)
@@ -80,11 +79,11 @@ esac
 # 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 io/ioutil is soft-deprecated.
+#
 #   *  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
@@ -94,6 +93,8 @@ esac
 #
 #   *  Package unsafe is… unsafe.
 #
+#   *  Package golang.org/x/net/context has been moved into stdlib.
+#
 blocklist_imports() {
 	git grep\
 		-e '[[:space:]]"errors"$'\
@@ -101,33 +102,55 @@ blocklist_imports() {
 		-e '[[:space:]]"log"$'\
 		-e '[[:space:]]"reflect"$'\
 		-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
 # numbers where one should use named constants.
 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() {
-	git ls-files '*_*.go' | {
-		grep -F\
-		-e '_big.go'\
-		-e '_bsd.go'\
-		-e '_darwin.go'\
-		-e '_freebsd.go'\
-		-e '_openbsd.go'\
-		-e '_linux.go'\
-		-e '_little.go'\
-		-e '_others.go'\
-		-e '_test.go'\
-		-e '_unix.go'\
-		-e '_windows.go' \
-		-v\
-		|| exit 0
-	}
+	underscore_files="$(
+		git ls-files '*_*.go'\
+			| grep -F\
+			-e '_big.go'\
+			-e '_bsd.go'\
+			-e '_darwin.go'\
+			-e '_freebsd.go'\
+			-e '_openbsd.go'\
+			-e '_linux.go'\
+			-e '_little.go'\
+			-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
 }
 
 # TODO(a.garipov): Add an analyser to look for `fallthrough`, `goto`, and `new`?
@@ -151,7 +174,7 @@ exit_on_output() (
 
 	output="$( "$cmd" "$@" 2>&1 )"
 	exitcode="$?"
-	if [ "$exitcode" != '0' ]
+	if [ "$exitcode" -ne '0' ]
 	then
 		echo "'$cmd' failed with code $exitcode"
 	fi
@@ -160,9 +183,9 @@ exit_on_output() (
 	then
 		if [ "$*" != '' ]
 		then
-			echo "combined output of '$cmd $*':"
+			echo "combined output of linter '$cmd $*':"
 		else
-			echo "combined output of '$cmd':"
+			echo "combined output of linter '$cmd':"
 		fi
 
 		echo "$output"
@@ -178,13 +201,6 @@ exit_on_output() (
 
 
 
-# Constants
-
-go_files='./main.go ./internal/'
-readonly go_files
-
-
-
 # Checks
 
 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/updater/ ./internal/version/ ./main.go
 
-gosec --quiet $go_files
-
 ineffassign ./...
 
 unparam ./...
@@ -222,6 +236,9 @@ nilness ./...
 
 exit_on_output shadow --strict ./...
 
+# TODO(a.garipov): Enable in v0.108.0.
+# gosec --quiet ./...
+
 # TODO(a.garipov): Enable --blank?
 errcheck --asserts ./...