Skip to content

🌱 Add golang version check to verify #1257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SHELL := /usr/bin/env bash -o pipefail
.SHELLFLAGS := -ec
export ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

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

.PHONY: tidy
tidy: #HELP Update dependencies.
$(Q)go mod tidy
$(Q)go mod tidy -go=$(GOLANG_VERSION)

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

.PHONY: go-verify
go-verify:
hack/tools/check-go-version.sh $(GOLANG_VERSION)

.PHONY: verify
verify: tidy fmt vet generate manifests crd-ref-docs #HELP Verify all generated code is up-to-date.
verify: tidy fmt vet generate manifests crd-ref-docs go-verify #HELP Verify all generated code is up-to-date.
git diff --exit-code

.PHONY: fix-lint
Expand Down
67 changes: 67 additions & 0 deletions hack/tools/check-go-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash

GO_VER=$1
OLDIFS="${IFS}"
IFS='.' INPUT_VERS=(${GO_VER})
IFS="${OLDIFS}"

if [ ${#INPUT_VERS[*]} -ne 3 -a ${#INPUT_VERS[*]} -ne 2 ]; then
echo "Invalid go version: ${GO_VER}"
exit 1
fi

GO_MAJOR=${INPUT_VERS[0]}
GO_MINOR=${INPUT_VERS[1]}
GO_PATCH=${INPUT_VERS[2]}

check_version () {
whole=$1
file=$2
OLDIFS="${IFS}"
IFS='.' ver=(${whole})
IFS="${OLDIFS}"

if [ ${#ver[*]} -eq 2 ] ; then
if [ ${ver[0]} -gt ${GO_MAJOR} ] ; then
echo "Bad golang version ${whole} in ${file} (expected ${GO_VER} or less)"
exit 1
fi
if [ ${ver[1]} -gt ${GO_MINOR} ] ; then
echo "Bad golang version ${whole} in ${file} (expected ${GO_VER} or less)"
exit 1
fi
echo "Version ${whole} in ${file} is good"
return
fi
if [ ${#INPUT_VERS[*]} -eq 2 ]; then
echo "Bad golang version ${whole} in ${file} (expecting only major.minor version)"
exit 1
fi
if [ ${#ver[*]} -ne 3 ] ; then
echo "Badly formatted golang version ${whole} in ${file}"
exit 1
fi

if [ ${ver[0]} -gt ${GO_MAJOR} ]; then
echo "Bad golang version ${whole} in ${file} (expected ${GO_VER} or less)"
exit 1
fi
if [ ${ver[1]} -gt ${GO_MINOR} ]; then
echo "Bad golang version ${whole} in ${file} (expected ${GO_VER} or less)"
exit 1
fi
if [ ${ver[1]} -eq ${GO_MINOR} -a ${ver[2]} -gt ${GO_PATCH} ]; then
echo "Bad golang version ${whole} in ${file} (expected ${GO_VER} or less)"
exit 1
fi
echo "Version ${whole} in ${file} is good"
}

for f in $(find . -name "*.mod"); do
v=$(sed -En 's/^go (.*)$/\1/p' ${f})
if [ -z ${v} ]; then
echo "Skipping ${f}: no version found"
else
check_version ${v} ${f}
fi
done