Skip to content

Commit 2a67f6f

Browse files
committed
Add golang version check to verify
This makes sure that we are not exceeding our golang version Supported golang version is 1.22.5 Signed-off-by: Todd Short <tshort@redhat.com>
1 parent 9491b59 commit 2a67f6f

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ SHELL := /usr/bin/env bash -o pipefail
77
.SHELLFLAGS := -ec
88
export ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
99

10+
GOLANG_VERSION := 1.22.5
1011
# Image URL to use all building/pushing image targets
1112
ifeq ($(origin IMAGE_REPO), undefined)
1213
IMAGE_REPO := quay.io/operator-framework/operator-controller
@@ -95,7 +96,7 @@ lint: $(GOLANGCI_LINT) #HELP Run golangci linter.
9596

9697
.PHONY: tidy
9798
tidy: #HELP Update dependencies.
98-
$(Q)go mod tidy
99+
$(Q)go mod tidy -go=$(GOLANG_VERSION)
99100

100101
.PHONY: manifests
101102
manifests: $(CONTROLLER_GEN) #EXHELP Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
@@ -105,8 +106,12 @@ manifests: $(CONTROLLER_GEN) #EXHELP Generate WebhookConfiguration, ClusterRole
105106
generate: $(CONTROLLER_GEN) #EXHELP Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
106107
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
107108

109+
.PHONY: go-verify
110+
go-verify:
111+
hack/tools/check-go-version.sh $(GOLANG_VERSION)
112+
108113
.PHONY: verify
109-
verify: tidy fmt vet generate manifests crd-ref-docs #HELP Verify all generated code is up-to-date.
114+
verify: tidy fmt vet generate manifests crd-ref-docs go-verify #HELP Verify all generated code is up-to-date.
110115
git diff --exit-code
111116

112117
.PHONY: fix-lint

hack/tools/check-go-version.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
3+
GO_VER=$1
4+
OLDIFS="${IFS}"
5+
IFS='.' vers=(${GO_VER})
6+
IFS="${OLDIFS}"
7+
8+
if [ ${#vers[*]} -ne 3 -a ${#vers[*]} -ne 2 ]; then
9+
echo "Invalid go version: ${GO_VER}"
10+
exit 1
11+
fi
12+
13+
GO_MAJOR=${vers[0]}
14+
GO_MINOR=${vers[1]}
15+
GO_PATCH=${vers[2]}
16+
17+
if [ ${#vers[*]} -eq 2 ]; then
18+
CHECK_FOR_2=1
19+
else
20+
CHECK_FOR_2=0
21+
fi
22+
23+
24+
check_version () {
25+
whole=$1
26+
file=$2
27+
OLDIFS="${IFS}"
28+
IFS='.' ver=(${whole})
29+
IFS="${OLDIFS}"
30+
31+
if [ ${#ver[*]} -eq 2 ] ; then
32+
if [ ${ver[0]} -gt ${GO_MAJOR} ] ; then
33+
echo "Bad golang version ${whole} in ${file} (expected ${GO_VER} or less)"
34+
exit 1
35+
fi
36+
if [ ${ver[1]} -gt ${GO_MINOR} ] ; then
37+
echo "Bad golang version ${whole} in ${file} (expected ${GO_VER} or less)"
38+
exit 1
39+
fi
40+
echo "Version ${whole} in ${file} is good"
41+
return
42+
fi
43+
if [ ${CHECK_FOR_2} -eq 1 ]; then
44+
echo "Bad golang version ${whole} in ${file} (expecting only major.minor version)"
45+
exit 1
46+
fi
47+
if [ ${#ver[*]} -ne 3 ] ; then
48+
echo "Badly formatted golang version ${whole} in ${file}"
49+
exit 1
50+
fi
51+
52+
if [ ${ver[0]} -gt ${GO_MAJOR} ]; then
53+
echo "Bad golang version ${whole} in ${file} (expected ${GO_VER} or less)"
54+
exit 1
55+
fi
56+
if [ ${ver[1]} -gt ${GO_MINOR} ]; then
57+
echo "Bad golang version ${whole} in ${file} (expected ${GO_VER} or less)"
58+
exit 1
59+
fi
60+
if [ ${ver[1]} -eq ${GO_MINOR} -a ${ver[2]} -gt ${GO_PATCH} ]; then
61+
echo "Bad golang version ${whole} in ${file} (expected ${GO_VER} or less)"
62+
exit 1
63+
fi
64+
echo "Version ${whole} in ${file} is good"
65+
}
66+
67+
for f in $(find . -name "*.mod"); do
68+
v=$(sed -En 's/^go (.*)$/\1/p' ${f})
69+
if [ -z ${v} ]; then
70+
echo "Skipping ${f}: no version found"
71+
else
72+
check_version ${v} ${f}
73+
fi
74+
done

0 commit comments

Comments
 (0)