From e0d89ba267a09fd23644541978ecd183863fe7a6 Mon Sep 17 00:00:00 2001
From: Ainar Garipov <a.garipov@adguard.com>
Date: Thu, 10 Jun 2021 20:09:00 +0300
Subject: [PATCH] Pull request: scripts: imp all

Merge in DNS/adguard-home from imp-sh to master

Squashed commit of the following:

commit d574deae73e5c07e1e9ef43152de8d86b276c623
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Jun 10 20:00:56 2021 +0300

    scripts: imp all
---
 scripts/make/build-docker.sh  |  6 ++---
 scripts/make/build-release.sh | 13 +++++-----
 scripts/make/clean.sh         |  2 +-
 scripts/make/go-build.sh      | 49 ++++++++++++-----------------------
 scripts/make/go-test.sh       | 27 ++++++++++---------
 scripts/make/version.sh       |  6 +++--
 6 files changed, 44 insertions(+), 59 deletions(-)

diff --git a/scripts/make/build-docker.sh b/scripts/make/build-docker.sh
index df1050e6..844d7da2 100644
--- a/scripts/make/build-docker.sh
+++ b/scripts/make/build-docker.sh
@@ -14,9 +14,9 @@ fi
 set -e -f -u
 
 # Require these to be set.  The channel value is validated later.
-channel="$CHANNEL"
-commit="$COMMIT"
-dist_dir="$DIST_DIR"
+channel="${CHANNEL:?please set CHANNEL}"
+commit="${COMMIT:?please set COMMIT}"
+dist_dir="${DIST_DIR:?please set DIST_DIR}"
 readonly channel commit dist_dir
 
 if [ "${VERSION:-}" = 'v0.0.0' ] || [ "${VERSION:-}" = '' ]
diff --git a/scripts/make/build-release.sh b/scripts/make/build-release.sh
index a53d6f8c..2cdac0bc 100644
--- a/scripts/make/build-release.sh
+++ b/scripts/make/build-release.sh
@@ -47,7 +47,7 @@ log 'starting to build AdGuard Home release'
 
 # Require the channel to be set.  Additional validation is performed later by
 # go-build.sh.
-channel="$CHANNEL"
+channel="${CHANNEL:?please set CHANNEL}"
 readonly channel
 
 # Check VERSION against the default value from the Makefile.  If it is that, use
@@ -93,8 +93,8 @@ fi
 # Require the gpg key and passphrase to be set if the signing is required.
 if [ "$sign" -eq '1' ]
 then
-	gpg_key_passphrase="$GPG_KEY_PASSPHRASE"
-	gpg_key="$GPG_KEY"
+	gpg_key_passphrase="${GPG_KEY_PASSPHRASE:?please set GPG_KEY_PASSPHRASE or unset SIGN}"
+	gpg_key="${GPG_KEY:?please set GPG_KEY or unset SIGN}"
 else
 	gpg_key_passphrase=''
 	gpg_key=''
@@ -360,10 +360,9 @@ log "calculating checksums"
 (
 	cd "./${dist}"
 
-	files="$( find . ! -name . -prune \( -name '*.tar.gz' -o -name '*.zip' \) )"
-
-	# Don't use quotes to get word splitting.
-	$sha256sum_cmd $files > ./checksums.txt
+	find . ! -name . -prune \( -name '*.tar.gz' -o -name '*.zip' \)\
+		-exec "$sha256sum_cmd" {} +\
+		> ./checksums.txt
 )
 
 log "writing versions"
diff --git a/scripts/make/clean.sh b/scripts/make/clean.sh
index 7fee73f0..f7384641 100644
--- a/scripts/make/clean.sh
+++ b/scripts/make/clean.sh
@@ -10,7 +10,7 @@ fi
 
 set -e -f -u
 
-dist_dir="$DIST_DIR"
+dist_dir="${DIST_DIR:?please set DIST_DIR}"
 sudo_cmd="${SUDO:-}"
 readonly dist_dir sudo_cmd
 
diff --git a/scripts/make/go-build.sh b/scripts/make/go-build.sh
index da2d106e..be231ad1 100644
--- a/scripts/make/go-build.sh
+++ b/scripts/make/go-build.sh
@@ -17,17 +17,17 @@ if [ "$verbose" -gt '1' ]
 then
 	env
 	set -x
-	v_flags='-v'
-	x_flags='-x'
+	v_flags='-v=1'
+	x_flags='-x=1'
 elif [ "$verbose" -gt '0' ]
 then
 	set -x
-	v_flags='-v'
-	x_flags=''
+	v_flags='-v=1'
+	x_flags='-x=0'
 else
 	set +x
-	v_flags=''
-	x_flags=''
+	v_flags='-v=0'
+	x_flags='-x=0'
 fi
 readonly x_flags v_flags
 
@@ -41,7 +41,7 @@ go="${GO:-go}"
 readonly go
 
 # Require the channel to be set and validate the value.
-channel="$CHANNEL"
+channel="${CHANNEL:?please set CHANNEL}"
 readonly channel
 
 case "$channel"
@@ -88,38 +88,28 @@ then
 fi
 
 # Allow users to limit the build's parallelism.
-parallelism="${PARALLELISM:-}"
+parallelism="${PARALLELISM:-0}"
 readonly parallelism
 
-if [ "${parallelism}" != '' ]
-then
-	par_flags="-p ${parallelism}"
-else
-	par_flags=''
-fi
-readonly par_flags
+p_flags="-p=${parallelism}"
+readonly p_flags
 
 # Allow users to specify a different output name.
-out="${OUT:-}"
+out="${OUT:-AdGuardHome}"
 readonly out
 
-if [ "$out" != '' ]
-then
-	out_flags="-o ${out}"
-else
-	out_flags=''
-fi
-readonly out_flags
+o_flags="-o=${out}"
+readonly o_flags
 
 # Allow users to enable the race detector.  Unfortunately, that means that cgo
 # must be enabled.
 if [ "${RACE:-0}" -eq '0' ]
 then
 	cgo_enabled='0'
-	race_flags=''
+	race_flags='--race=0'
 else
 	cgo_enabled='1'
-	race_flags='--race'
+	race_flags='--race=1'
 fi
 readonly cgo_enabled race_flags
 
@@ -127,11 +117,6 @@ CGO_ENABLED="$cgo_enabled"
 GO111MODULE='on'
 export CGO_ENABLED GO111MODULE
 
-build_flags="${BUILD_FLAGS:-$race_flags --trimpath $out_flags $par_flags $v_flags $x_flags}"
-readonly build_flags
-
 # Don't use quotes with flag variables to get word splitting.
-"$go" generate $v_flags $x_flags ./main.go
-
-# Don't use quotes with flag variables to get word splitting.
-"$go" build --ldflags "$ldflags" $build_flags
+"$go" build --ldflags "$ldflags" "$race_flags" --trimpath "$o_flags" "$p_flags" "$v_flags"\
+	"$x_flags"
diff --git a/scripts/make/go-test.sh b/scripts/make/go-test.sh
index 4586f479..6ab9fadd 100644
--- a/scripts/make/go-test.sh
+++ b/scripts/make/go-test.sh
@@ -10,17 +10,17 @@ readonly verbose
 if [ "$verbose" -gt '1' ]
 then
 	set -x
-	v_flags='-v'
-	x_flags='-x'
+	v_flags='-v=1'
+	x_flags='-x=1'
 elif [ "$verbose" -gt '0' ]
 then
 	set -x
-	v_flags='-v'
-	x_flags=''
+	v_flags='-v=1'
+	x_flags='-x=0'
 else
 	set +x
-	v_flags=''
-	x_flags=''
+	v_flags='-v=0'
+	x_flags='-x=0'
 fi
 readonly v_flags x_flags
 
@@ -28,18 +28,17 @@ set -e -f -u
 
 if [ "${RACE:-1}" -eq '0' ]
 then
-	race_flags=''
+	race_flags='--race=0'
 else
-	race_flags='--race'
+	race_flags='--race=1'
 fi
 readonly race_flags
 
 go="${GO:-go}"
-timeout_flags="${TIMEOUT_FLAGS:---timeout 30s}"
-cover_flags='--coverprofile ./coverage.txt'
-count_flags='--count 1'
+
+count_flags='--count=1'
+cover_flags='--coverprofile=./coverage.txt'
+timeout_flags="${TIMEOUT_FLAGS:---timeout=30s}"
 readonly go timeout_flags cover_flags count_flags
 
-# Don't use quotes with flag variables because we want an empty space if those
-# aren't set.
-"$go" test $count_flags $cover_flags $race_flags $timeout_flags $x_flags $v_flags ./...
+"$go" test "$count_flags" "$cover_flags" "$race_flags" "$timeout_flags" "$x_flags" "$v_flags" ./...
diff --git a/scripts/make/version.sh b/scripts/make/version.sh
index 93dfef5d..45cfd353 100644
--- a/scripts/make/version.sh
+++ b/scripts/make/version.sh
@@ -34,6 +34,8 @@ set -e -f -u
 
 # bump_minor is an awk program that reads a minor release version, increments
 # the minor part of it, and prints the next version.
+#
+# shellcheck disable=SC2016
 bump_minor='/^v[0-9]+\.[0-9]+\.0$/ {
 	print($1 "." $2 + 1 ".0");
 
@@ -66,7 +68,7 @@ get_last_minor_zero() {
 		| head -n 1
 }
 
-channel="$CHANNEL"
+channel="${CHANNEL:?please set CHANNEL}"
 readonly channel
 
 case "$channel"
@@ -86,7 +88,7 @@ in
 	num_commits_since_minor="$( git rev-list "${last_minor_zero}..HEAD" | wc -l )"
 	# The output of darwin's implementation of wc needs to be trimmed from
 	# redundant spaces.
-	num_commits_since_minor="$( echo ${num_commits_since_minor} | tr -d '[:space:]' )"
+	num_commits_since_minor="$( echo "$num_commits_since_minor" | tr -d '[:space:]' )"
 	readonly num_commits_since_minor
 
 	# next_minor is the next minor release version.