Rework Go mod tidy/vendor checks

This change reworks the Go mod tidy/vendor checks to run for all tracked Go modules by the project and fail for any uncommitted changes.

Signed-off-by: Austin Vazquez <austin.vazquez@docker.com>
(cherry picked from commit f6e1bf280807028fa1d54b91c33049b20452ad77)
Signed-off-by: Austin Vazquez <austin.vazquez@docker.com>
diff --git a/hack/validate/vendor b/hack/validate/vendor
index eac995f..b01a45c 100755
--- a/hack/validate/vendor
+++ b/hack/validate/vendor
@@ -5,38 +5,30 @@
 SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 source "${SCRIPTDIR}/.validate"
 
-tidy_files=('vendor.mod' 'vendor.sum')
+modules_files=('man/go.mod' 'vendor.mod')
+tidy_files=("${modules_files[@]}" 'man/go.sum' 'vendor.sum')
 vendor_files=("${tidy_files[@]}" 'vendor/')
 
-validate_vendor_files_exist() {
-	local file
-	for file in "${vendor_files[@]}"; do
-		if [ ! -e "$file" ]; then
-			echo >&2 "ERROR: required file '$file' does not exist"
+validate_tidy_modules() {
+	# check that all go.mod files exist in HEAD; go.sum files are generated by 'go mod tidy'
+	# so we don't need to check for their existence beforehand
+	for f in "${modules_files[@]}"; do
+		if [ ! -f "$f" ]; then
+			echo >&2 "ERROR: missing $f"
 			return 1
 		fi
 	done
-	return 0
-}
-
-validate_vendor_tidy() {
 	# run mod tidy
 	./hack/vendor.sh tidy
 	# check if any files have changed
-	git diff --quiet HEAD -- "${tidy_files[@]}"
+	git diff --quiet HEAD -- "${tidy_files[@]}" && [ -z "$(git ls-files --others --exclude-standard)" ]
 }
 
 validate_vendor_diff() {
-	mapfile -t changed_files < <(validate_diff --diff-filter=ACMR --name-only -- "${vendor_files[@]}")
-
-	if [ -n "${TEST_FORCE_VALIDATE:-}" ] || [ "${#changed_files[@]}" -gt 0 ]; then
-		# recreate vendor/
-		./hack/vendor.sh vendor
-		# check if any files have changed
-		git diff --quiet HEAD -- "${vendor_files[@]}"
-	else
-		echo >&2 'INFO: no vendor changes in diff; skipping vendor check.'
-	fi
+	# recreate vendor/
+	./hack/vendor.sh vendor
+	# check if any files have changed
+	git diff --quiet HEAD -- "${vendor_files[@]}" && [ -z "$(git ls-files --others --exclude-standard)" ]
 }
 
 validate_vendor_license() {
@@ -48,26 +40,22 @@
 	done < <(awk '/^# /{ print $2 }' vendor/modules.txt)
 }
 
-# First check the required files exist as git diff will not report missing files.
-if ! validate_vendor_files_exist; then
-	{
-		echo 'FAIL: Vendoring was not performed correctly!'
-		echo
-		echo 'Please revendor with hack/vendor.sh'
-	} >&2
-	exit 1
-fi
-
-if validate_vendor_tidy && validate_vendor_diff && validate_vendor_license; then
+if validate_tidy_modules && validate_vendor_diff && validate_vendor_license; then
 	echo >&2 'PASS: Vendoring has been performed correctly!'
 else
 	{
 		echo 'FAIL: Vendoring was not performed correctly!'
 		echo
-		echo 'The following files changed during re-vendor:'
-		echo
-		git diff --name-status HEAD -- "${vendor_files[@]}"
-		echo
+		if [ -n "$(git ls-files --others --exclude-standard)" ]; then
+			echo 'The following files are missing:'
+			git ls-files --others --exclude-standard
+			echo
+		fi
+		if [ -n "$(git diff --name-status HEAD -- "${vendor_files[@]}")" ]; then
+			echo 'The following files changed during re-vendor:'
+			git diff --name-status HEAD -- "${vendor_files[@]}"
+			echo
+		fi
 		echo 'Please revendor with hack/vendor.sh'
 		echo
 		git diff --diff-filter=M -- "${vendor_files[@]}"
diff --git a/hack/vendor.sh b/hack/vendor.sh
index 32538a3..8d55056 100755
--- a/hack/vendor.sh
+++ b/hack/vendor.sh
@@ -7,15 +7,32 @@
 set -e
 
 SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+PROJECT_DIR="$(cd "$SCRIPTDIR/.." && pwd)"
 
 tidy() (
+	(
 		set -x
 		"${SCRIPTDIR}"/with-go-mod.sh go mod tidy -modfile vendor.mod -compat 1.18
+	)
+
+	(
+		set -x
+		cd man
+		go mod tidy
+	)
 )
 
 vendor() (
+	(
 		set -x
 		"${SCRIPTDIR}"/with-go-mod.sh go mod vendor -modfile vendor.mod
+	)
+
+	(
+		set -x
+		cd man
+		go mod vendor
+	)
 )
 
 help() {