Skip to content

Commit 7d40e07

Browse files
.*: add verify script for go directive changes
This commit adds a script that checks the changed version in the go.mod file with a certain maximum version that the go directive can have. We set the maximum version of the go directive as 1.20 here because the oldest go directive that exists on our supported release branches in k/k is 1.20. Signed-off-by: Madhav Jivrajani <[email protected]>
1 parent 9cff633 commit 7d40e07

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ all:
44

55
test:
66
go test -race ./...
7-
./hack/verify-examples.sh
87
go -C v2 test -race ./...
98
(cd v2 && ./hack/verify-examples.sh)
9+
10+
# We verify for the maximum version of the go directive as 1.20
11+
# here because the oldest go directive that exists on our supported
12+
# release branches in k/k is 1.20.
13+
verify:
14+
./hack/verify-examples.sh
15+
./hack/verify-go-directive.sh 1.20

hack/verify-go-directive.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2024 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
function usage {
22+
local script="$(basename $0)"
23+
24+
echo >&2 "Usage: ${script} <maximum go directive>
25+
This script should be run at the root of a module.
26+
27+
Compare the go directive in the local working copy's go.mod
28+
to the specified maximum version it can be. Versions provided
29+
here are of the form 1.x.y, without the 'go' prefix.
30+
Examples:
31+
${script} 1.20
32+
${script} 1.21.6
33+
"
34+
exit 1
35+
}
36+
37+
max="$1"
38+
# If max is empty, print usage and error
39+
if [[ -z "${max}" ]]; then
40+
usage;
41+
fi
42+
43+
# Don't specify the version with the go prefix, just 1.x.y will do.
44+
if [[ ! "${max}" =~ ^[0-9]\.[0-9]+(\.[0-9]+)?$ ]]; then
45+
usage
46+
fi
47+
48+
current=$(awk '/^go / {print $2;}' go.mod)
49+
if [[ -z "${current}" ]]; then
50+
echo >&2 "FAIL: could not get value of go directive from go.mod"
51+
exit 1
52+
fi
53+
54+
if ! printf '%s\n' "${current}" "${max}" | sort --check=silent --version-sort; then
55+
echo >&2 "FAIL: current Go directive ${current} is greater than ${max}"
56+
exit 1
57+
fi

0 commit comments

Comments
 (0)