|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +########################################### |
| 4 | +# Check Go version in go.mod files |
| 5 | +# and ensure it is not greater than the |
| 6 | +# version in the main go.mod file. |
| 7 | +# Also check if the version in the main |
| 8 | +# go.mod file is updated in the |
| 9 | +# submodules. |
| 10 | +# This script is intended to be run |
| 11 | +# as part of the CI pipeline to ensure |
| 12 | +# that the version of Go that we can use |
| 13 | +# is not accidentally upgraded. |
| 14 | +# Source: https://github.com/operator-framework/operator-controller/blob/main/hack/tools/check-go-version.sh |
| 15 | +# |
| 16 | +# PS: We have the intention to centralize |
| 17 | +# this implementation in the future. |
| 18 | +########################################### |
| 19 | + |
| 20 | + |
| 21 | +BASE_REF=${1:-main} |
| 22 | +GO_VER=$(sed -En 's/^go (.*)$/\1/p' "go.mod") |
| 23 | +OLDIFS="${IFS}" |
| 24 | +IFS='.' MAX_VER=(${GO_VER}) |
| 25 | +IFS="${OLDIFS}" |
| 26 | + |
| 27 | +if [ ${#MAX_VER[*]} -ne 3 -a ${#MAX_VER[*]} -ne 2 ]; then |
| 28 | + echo "Invalid go version: ${GO_VER}" |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +GO_MAJOR=${MAX_VER[0]} |
| 33 | +GO_MINOR=${MAX_VER[1]} |
| 34 | +GO_PATCH=${MAX_VER[2]} |
| 35 | + |
| 36 | +RETCODE=0 |
| 37 | + |
| 38 | +check_version () { |
| 39 | + local whole=$1 |
| 40 | + local file=$2 |
| 41 | + OLDIFS="${IFS}" |
| 42 | + IFS='.' ver=(${whole}) |
| 43 | + IFS="${OLDIFS}" |
| 44 | + |
| 45 | + if [ ${ver[0]} -gt ${GO_MAJOR} ]; then |
| 46 | + echo "${file}: ${whole}: Bad golang version (expected ${GO_VER} or less)" |
| 47 | + return 1 |
| 48 | + fi |
| 49 | + if [ ${ver[1]} -gt ${GO_MINOR} ]; then |
| 50 | + echo "${file}: ${whole}: Bad golang version (expected ${GO_VER} or less)" |
| 51 | + return 1 |
| 52 | + fi |
| 53 | + |
| 54 | + if [ ${#ver[*]} -eq 2 ] ; then |
| 55 | + return 0 |
| 56 | + fi |
| 57 | + if [ ${#ver[*]} -ne 3 ] ; then |
| 58 | + echo "${file}: ${whole}: Badly formatted golang version" |
| 59 | + return 1 |
| 60 | + fi |
| 61 | + |
| 62 | + if [ ${ver[1]} -eq ${GO_MINOR} -a ${ver[2]} -gt ${GO_PATCH} ]; then |
| 63 | + echo "${file}: ${whole}: Bad golang version (expected ${GO_VER} or less)" |
| 64 | + return 1 |
| 65 | + fi |
| 66 | + return 0 |
| 67 | +} |
| 68 | + |
| 69 | +echo "Found golang version: ${GO_VER}" |
| 70 | + |
| 71 | +for f in $(find . -name "*.mod"); do |
| 72 | + v=$(sed -En 's/^go (.*)$/\1/p' ${f}) |
| 73 | + if [ -z ${v} ]; then |
| 74 | + echo "${f}: Skipping, no version found" |
| 75 | + continue |
| 76 | + fi |
| 77 | + if ! check_version ${v} ${f}; then |
| 78 | + RETCODE=1 |
| 79 | + fi |
| 80 | + old=$(git grep -ohP '^go .*$' "${BASE_REF}" -- "${f}") |
| 81 | + old=${old#go } |
| 82 | + new=$(git grep -ohP '^go .*$' "${f}") |
| 83 | + new=${new#go } |
| 84 | + # If ${old} is empty, it means this is a new .mod file |
| 85 | + if [ -z "${old}" ]; then |
| 86 | + continue |
| 87 | + fi |
| 88 | + # Check if patch version remains 0: X.x.0 <-> X.x |
| 89 | + if [ "${new}.0" == "${old}" -o "${new}" == "${old}.0" ]; then |
| 90 | + continue |
| 91 | + fi |
| 92 | + if [ "${new}" != "${old}" ]; then |
| 93 | + echo "${f}: ${v}: Updated golang version from ${old}" |
| 94 | + RETCODE=1 |
| 95 | + fi |
| 96 | +done |
| 97 | + |
| 98 | +exit ${RETCODE} |
0 commit comments