etcd/scripts/sync_go_toolchain_directive.sh
Madhav Jivrajani 90666624f3 .*: sync go toolchain version and add ability to verify versions
This commit adds a script to sync the version present in .go-version
across all go.mod files as the toolchain directive. As part of that,
this commit also modifies go.mod files that did not have synced toolchain
directives.

Additionally, this also adds a script to verify all toolchain and go
directives against the version present in .go-version as follows:
(1) The go directive <= version in .go-version
(2) The toolchain directive == version in .go-version

This script runs as part of the `make verify` target, making it run
as a presbumit by default.

Signed-off-by: Madhav Jivrajani <madhav.jiv@gmail.com>
2024-05-15 15:35:21 +05:30

22 lines
739 B
Bash
Executable File

#!/usr/bin/env bash
# This script looks at the version present in the .go-version file and treats
# that to be the value of the toolchain directive that go should use. It then
# updates the toolchain directives of all go.mod files to reflect this version.
#
# We do this to ensure that .go-version acts as the source of truth for go versions.
set -euo pipefail
ROOT_MODULE="go.etcd.io/etcd"
if [[ "$(go list)" != "${ROOT_MODULE}" ]]; then
echo "must be run from '${ROOT_MODULE}' module directory"
exit 255
fi
ETCD_ROOT_DIR=$(go list -f '{{.Dir}}' "${ROOT_MODULE}")
TARGET_GO_VERSION="${TARGET_GO_VERSION:-"$(cat "${ETCD_ROOT_DIR}/.go-version")"}"
find . -name 'go.mod' -exec go mod edit -toolchain=go"${TARGET_GO_VERSION}" {} \;