Skip to content

Commit 9298a44

Browse files
committed
hack/verify-go-modules.sh: exit on dependency mismatch
On-behalf-of: SAP [email protected] Signed-off-by: Robert Vasek <[email protected]>
1 parent 1ac46c6 commit 9298a44

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

hack/verify-go-modules.sh

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,22 @@ set -o pipefail
2626

2727
REPO_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
2828

29-
mapfile -t DIRS < <(find "${REPO_ROOT}" -name go.mod -print0 | xargs -0 dirname)
29+
# This is the list of directories that host the individual kcp go modules.
30+
#
31+
# KEEP IT UP TO DATE and SORTED!
32+
#
33+
# The items must be sorted such that the module that is being dependent on
34+
# comes before the one that depends on it (i.e. topological order).
35+
DIRS=(
36+
"${REPO_ROOT}/sdk"
37+
"${REPO_ROOT}/cli"
38+
"${REPO_ROOT}"
39+
"${REPO_ROOT}/docs/generators/cli-doc"
40+
)
41+
42+
# Check that DIRS is up to date.
43+
[[ "$(printf '%s\n' "${DIRS[@]}" | sort)" == "$(find "${REPO_ROOT}" -name go.mod -print0 | xargs -0 dirname | sort)" ]] \
44+
|| { echo "Error: DIRS list in ${BASH_SOURCE[0]} is out-of-date, and needs to be manually updated" 1>&2 ; exit 1 ; }
3045

3146
# list_deps lists dependencies of the supplied go.mod file (dependencies
3247
# with version "v0.0.0" are skipped). The output is a json dictionary in the
@@ -75,22 +90,29 @@ function diff_version_deps {
7590
' "${has_deps}" "${wants_deps}"
7691
}
7792

78-
# print_diff_version_deps prints the output of diff_version_deps as a
79-
# human-readable multi-line text.
93+
# print_diff_version_deps prints the output of diff_version_deps (expected in stdin)
94+
# as a human-readable multi-line text.
95+
# Returns 1 if any lines were printed (i.e. errors were found).
8096
function print_diff_version_deps {
81-
jq -r "to_entries | map(\"Warning: version mismatch: has \(.key)@\(.value.has), but \(.value.wants) expected\") | join(\"\\n\")"
97+
jq -r '
98+
to_entries
99+
| map("Error: version mismatch: \(.key) is \(.value.has), but \(.value.wants) expected")
100+
| join("\n")
101+
| halt_error(1)
102+
'
82103
}
83104

84105
# Compares versions of dependencies in the supplied go.mod to
85106
# makes sure they are in line with the ones declared in
86107
# k8s.io/kubernetes module and prints the result.
87-
function compare_mod_versions {
108+
function compare_mod_versions_or_fail {
88109
gomod_file="${1}"
89110
echo "Verifying dependency versions in ${gomod_file} against ${k8s_gomod}"
90111
deps="$(list_deps ${gomod_file})"
91112

92113
diff_version_deps <(echo "${deps}") <(echo "${k8s_deps}") \
93-
| print_diff_version_deps "${gomod_file}"
114+
| print_diff_version_deps "${gomod_file}" 1>&2 \
115+
|| { echo ; exit 1 ; }
94116
}
95117

96118
function gomod_filepath_for {
@@ -111,9 +133,9 @@ for dir in "${DIRS[@]}"; do
111133
exit 1
112134
fi
113135

114-
compare_mod_versions "${dir}/go.mod"
136+
compare_mod_versions_or_fail "${dir}/go.mod"
115137
)
116138
done
117139

118-
compare_mod_versions "$(gomod_filepath_for github.com/kcp-dev/client-go)"
119-
compare_mod_versions "$(gomod_filepath_for github.com/kcp-dev/apimachinery/v2)"
140+
compare_mod_versions_or_fail "$(gomod_filepath_for github.com/kcp-dev/client-go)"
141+
compare_mod_versions_or_fail "$(gomod_filepath_for github.com/kcp-dev/apimachinery/v2)"

0 commit comments

Comments
 (0)