|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# This script verifies that the value of the toolchain directive in the |
| 4 | +# go.mod files always match that of the .go-version file to ensure that |
| 5 | +# we accidentally don't test and release with differing versions of Go. |
| 6 | + |
| 7 | +set -euo pipefail |
| 8 | + |
| 9 | +source ./scripts/test_lib.sh |
| 10 | + |
| 11 | +target_go_version="${target_go_version:-"$(cat "${ETCD_ROOT_DIR}/.go-version")"}" |
| 12 | +log_info "expected go toolchain directive: go${target_go_version}" |
| 13 | +log_info |
| 14 | + |
| 15 | +toolchain_out_of_sync="false" |
| 16 | +go_line_violation="false" |
| 17 | + |
| 18 | +# verify_go_versions takes a go.mod filepath as an argument |
| 19 | +# and checks if: |
| 20 | +# (1) go directive <= version in .go-version |
| 21 | +# (2) toolchain directive == version in .go-version |
| 22 | +function verify_go_versions() { |
| 23 | + # shellcheck disable=SC2086 |
| 24 | + toolchain_version="$(go mod edit -json $1 | jq -r .Toolchain)" |
| 25 | + # shellcheck disable=SC2086 |
| 26 | + go_line_version="$(go mod edit -json $1 | jq -r .Go)" |
| 27 | + if [[ "go${target_go_version}" != "${toolchain_version}" ]]; then |
| 28 | + log_error "go toolchain directive out of sync for $1, got: ${toolchain_version}" |
| 29 | + toolchain_out_of_sync="true" |
| 30 | + fi |
| 31 | + if ! printf '%s\n' "${go_line_version}" "${target_go_version}" | sort --check=silent --version-sort; then |
| 32 | + log_error "go directive in $1 is greater than maximum allowed: go${target_go_version}" |
| 33 | + go_line_violation="true" |
| 34 | + fi |
| 35 | +} |
| 36 | + |
| 37 | +while read -r mod; do |
| 38 | + verify_go_versions "${mod}"; |
| 39 | +done < <(find . -name 'go.mod') |
| 40 | + |
| 41 | +if [[ "${toolchain_out_of_sync}" == "true" ]]; then |
| 42 | + log_error |
| 43 | + log_error "Please run scripts/sync_go_toolchain_directive.sh or update .go-version to rectify this error" |
| 44 | +fi |
| 45 | + |
| 46 | +if [[ "${go_line_violation}" == "true" ]]; then |
| 47 | + log_error |
| 48 | + log_error "Please update .go-version to rectify this error, any go directive should be <= .go-version" |
| 49 | +fi |
| 50 | + |
| 51 | +if [[ "${go_line_violation}" == "true" ]] || [[ "${toolchain_out_of_sync}" == "true" ]]; then |
| 52 | + exit 1 |
| 53 | +fi |
0 commit comments