From 1980912ebee0d1111070fc6df106287922c53baf Mon Sep 17 00:00:00 2001 From: Ryan Richard Date: Tue, 3 Dec 2024 12:51:40 -0600 Subject: [PATCH] add hack script to help update major versions of modules --- hack/update-go-mod/update-majors.sh | 65 +++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 hack/update-go-mod/update-majors.sh diff --git a/hack/update-go-mod/update-majors.sh b/hack/update-go-mod/update-majors.sh new file mode 100755 index 000000000..5cfdc37ee --- /dev/null +++ b/hack/update-go-mod/update-majors.sh @@ -0,0 +1,65 @@ +#!/usr/bin/env bash + +# Copyright 2024 the Pinniped contributors. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +set -euo pipefail + +SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd) +ROOT_DIR="$SCRIPT_DIR/../.." + +GO_MOD="${ROOT_DIR}/go.mod" + +modules=$(grep -v ' // indirect' "$GO_MOD" | + grep -v '^//' | + grep -v '^replace ' | + grep '/v[0-9]' | + tr -d '\t' | + cut -d ' ' -f 1) + +# Print to screen for debugging purposes. +echo "Found current modules using major versions above v1..." +echo "$modules" + +pushd "$ROOT_DIR" >/dev/null + +echo +echo "Checking for new major versions..." +for m in $modules; do + if [[ "$m" == "github.com/go-jose/go-jose/v3" ]]; then + # Skip github.com/go-jose/go-jose/v3 because we are using both v3 and v4. + continue + fi + next_version=$(echo "$m" | awk -F '/v' '{ printf("%s/v%d\n", $1, $2 + 1) }') + set +e + go get "${next_version}@latest" + found_new_version=$? + if [[ $found_new_version == 0 ]]; then + echo "Found new version $next_version. Replacing imports..." + find . -name './.*' -prune -o \ + -path ./generated -prune -o \ + -type f -name '*.go' -print0 | + xargs -0 sed -i '' "s#${m}#${next_version}#g" + fi + set -e +done + +go mod tidy + +if git diff --quiet; then + echo + echo "No changes." +else + echo + echo "Showing resulting diffs..." + git --no-pager diff + + echo + echo "Running unit tests..." + ./hack/module.sh units +fi + +popd >/dev/null + +echo +echo "Done!"