Skip to content

Commit 2cc0b44

Browse files
Merge pull request #84 from bparees/golang
better golang version checking
2 parents d5a3219 + 1342378 commit 2cc0b44

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

hack/lib/build/binaries.sh

+39-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,39 @@ function os::build::platform_arch() {
6161
}
6262
readonly -f os::build::platform_arch
6363

64+
65+
# compare semantic versions, from https://stackoverflow.com/a/4025065/6788026
66+
function vercomp() {
67+
if [[ $1 == $2 ]]
68+
then
69+
return 0
70+
fi
71+
local IFS=.
72+
local i ver1=($1) ver2=($2)
73+
# fill empty fields in ver1 with zeros
74+
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
75+
do
76+
ver1[i]=0
77+
done
78+
for ((i=0; i<${#ver1[@]}; i++))
79+
do
80+
if [[ -z ${ver2[i]} ]]
81+
then
82+
# fill empty fields in ver2 with zeros
83+
ver2[i]=0
84+
fi
85+
if ((10#${ver1[i]} > 10#${ver2[i]}))
86+
then
87+
return 1
88+
fi
89+
if ((10#${ver1[i]} < 10#${ver2[i]}))
90+
then
91+
return 2
92+
fi
93+
done
94+
return 0
95+
}
96+
6497
# os::build::setup_env will check that the `go` commands is available in
6598
# ${PATH}. If not running on Travis, it will also check that the Go version is
6699
# good enough for the Kubernetes build.
@@ -85,7 +118,12 @@ function os::build::setup_env() {
85118
if [[ "${TRAVIS:-}" != "true" ]]; then
86119
local go_version
87120
go_version=($(go version))
88-
if [[ "${go_version[2]}" < "${OS_REQUIRED_GO_VERSION}" ]]; then
121+
go_version=${go_version[2]#go}
122+
set +ue
123+
vercomp ${go_version} ${OS_REQUIRED_GO_VERSION#go}
124+
rc=$?
125+
set -eu
126+
if [[ $rc -eq 2 ]]; then
89127
os::log::fatal "Detected Go version: ${go_version[*]}.
90128
Builds require Go version ${OS_REQUIRED_GO_VERSION} or greater."
91129
fi

0 commit comments

Comments
 (0)