Skip to content

Commit 778b210

Browse files
phlogistonjohnmergify[bot]
authored andcommitted
hack: add go-version-check.sh script
This script validates the Dockefile (or other specified container files) or shows the list of current supported golang version images. The goal is to ensure we are continually using a supported Go version and not falling behind. Signed-off-by: John Mulligan <[email protected]>
1 parent 1d9f821 commit 778b210

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

hack/go-version-check.sh

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/bin/bash
2+
#
3+
# List or check that the golang version embedded in files (specifically our
4+
# Dockerfile) is using a supported version of Go.
5+
# Run with --show to list the images the script thinks are current.
6+
# Run with --check (the default) to verify the Dockerfile contains a current
7+
# image & version tag.
8+
#
9+
10+
set -e
11+
SCRIPT_DIR="$(readlink -f "$(dirname "${0}")")"
12+
PROJECT_DIR="$(readlink -f "${SCRIPT_DIR}/..")"
13+
WORKDIR="$(mktemp -d)"
14+
CONTAINER_FILES=("${PROJECT_DIR}/Dockerfile")
15+
GOVERSURL='https://go.dev/dl/?mode=json'
16+
ACTION=check
17+
18+
19+
fetch_versions() {
20+
local url="$1"
21+
local out="$2"
22+
if [ -f "${out}" ]; then
23+
return
24+
fi
25+
echo "Fetching Go versions..." >&2
26+
curl --fail -sL -o "${out}" "${url}"
27+
}
28+
29+
extract_versions() {
30+
local jsonfile="$1"
31+
jq -r '.[].version' < "${jsonfile}" | \
32+
cut -d. -f1,2 | sort -u | sed -e 's,^go,,'
33+
}
34+
35+
to_images() {
36+
for a in "$@"; do
37+
echo "docker.io/golang:${a}"
38+
done
39+
}
40+
41+
cleanup() {
42+
# only used in trap, ignoe unreachable error
43+
# shellcheck disable=SC2317
44+
rm -rf "${WORKDIR}"
45+
}
46+
trap cleanup EXIT
47+
48+
49+
opts=$(getopt --name "$0" \
50+
-o "csf:" -l "check,show,file:,url:,versionsfile:" -- "$@")
51+
eval set -- "$opts"
52+
53+
cli_cfiles=()
54+
while true ; do
55+
case "$1" in
56+
-c|--check)
57+
ACTION=check
58+
shift
59+
;;
60+
-s|--show)
61+
ACTION=show
62+
shift
63+
;;
64+
-f|--file)
65+
cli_cfiles+=("$2")
66+
shift 2
67+
;;
68+
--url)
69+
GOVERSURL="$2"
70+
shift 2
71+
;;
72+
--versionsfile)
73+
GOVERSIONSFILE="$2"
74+
shift 2
75+
;;
76+
--)
77+
shift
78+
break
79+
;;
80+
*)
81+
echo "unexpected option: $1" >&2
82+
exit 2
83+
;;
84+
esac
85+
done
86+
87+
if [ "${#cli_cfiles}" -gt 0 ]; then
88+
CONTAINER_FILES=("${cli_cfiles[@]}")
89+
fi
90+
91+
GV="${GOVERSIONSFILE:-${WORKDIR}/go-versions.json}"
92+
fetch_versions "${GOVERSURL}" "${GV}"
93+
mapfile -t go_major_vers < <(extract_versions "${GV}")
94+
95+
echo "Found Go versions:" "${go_major_vers[@]}" >&2
96+
if [ "${ACTION}" = show ]; then
97+
to_images "${go_major_vers[@]}"
98+
exit 1
99+
fi
100+
101+
mapfile -t valid_images < <(to_images "${go_major_vers[@]}")
102+
errors=0
103+
for cf in "${CONTAINER_FILES[@]}"; do
104+
echo "Checking $cf ..." >&2
105+
if grep -q -e "${valid_images[0]}" -e "${valid_images[1]}" "${cf}" ; then
106+
echo "${cf}: OK" >&2
107+
else
108+
echo "${cf}: no current golang image found" >&2
109+
errors=1
110+
fi
111+
done
112+
113+
exit "${errors}"

0 commit comments

Comments
 (0)