|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# Sync Go version from go.mod to other files in the repo |
| 6 | +# Usage: ./scripts/sync-go-version.sh <check|sync> |
| 7 | + |
| 8 | +usage() { |
| 9 | + echo "Usage: $0 <check|sync>" |
| 10 | + echo " check - Check if Go version is consistent across files" |
| 11 | + echo " sync - Sync Go version from go.mod to other files" |
| 12 | + exit 1 |
| 13 | +} |
| 14 | + |
| 15 | +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 16 | +cd "$REPO_ROOT" |
| 17 | + |
| 18 | +GO_VERSION=$(grep '^go ' go.mod | awk '{print $2}') |
| 19 | +GO_VERSION_MINOR=$(echo "$GO_VERSION" | cut -d. -f1,2) |
| 20 | + |
| 21 | +if [[ -z "$GO_VERSION" ]]; then |
| 22 | + echo "Error: Could not extract Go version from go.mod" >&2 |
| 23 | + exit 1 |
| 24 | +fi |
| 25 | + |
| 26 | +echo "Go version from go.mod: $GO_VERSION" |
| 27 | +echo "" |
| 28 | + |
| 29 | +update_file() { |
| 30 | + local file="$1" |
| 31 | + local pattern="$2" |
| 32 | + local replacement="$3" |
| 33 | + |
| 34 | + if [[ ! -f "$file" ]]; then |
| 35 | + return |
| 36 | + fi |
| 37 | + |
| 38 | + local current |
| 39 | + current=$(grep -E "$pattern" "$file" 2>/dev/null | head -1 || echo "") |
| 40 | + |
| 41 | + if [[ -z "$current" ]]; then |
| 42 | + return |
| 43 | + fi |
| 44 | + |
| 45 | + if [[ "$current" == "$replacement" ]]; then |
| 46 | + return |
| 47 | + fi |
| 48 | + |
| 49 | + if [[ "$(uname)" == "Darwin" ]]; then |
| 50 | + sed -i '' "s|$pattern|$replacement|" "$file" |
| 51 | + else |
| 52 | + sed -i "s|$pattern|$replacement|" "$file" |
| 53 | + fi |
| 54 | + echo "Updated: $file" |
| 55 | + echo " $current -> $replacement" |
| 56 | +} |
| 57 | + |
| 58 | +NEEDS_UPDATE=0 |
| 59 | +check_file() { |
| 60 | + local file="$1" |
| 61 | + local expected_pattern="$2" |
| 62 | + local search_pattern="$3" |
| 63 | + local expected_value="$4" |
| 64 | + |
| 65 | + if [[ ! -f "$file" ]]; then |
| 66 | + return |
| 67 | + fi |
| 68 | + |
| 69 | + if ! grep -q "$expected_pattern" "$file"; then |
| 70 | + local current |
| 71 | + current=$(grep "$search_pattern" "$file" 2>/dev/null | head -1 || echo "(not found)") |
| 72 | + echo "Mismatch: $(realpath "$file")" |
| 73 | + echo " have: $current" |
| 74 | + echo " want: $expected_value" |
| 75 | + NEEDS_UPDATE=1 |
| 76 | + fi |
| 77 | +} |
| 78 | + |
| 79 | +# Files to update (excluding pkg/go-containerregistry) |
| 80 | +GO_FILES=("go.work" "go.mod" "cmd/cli/go.mod") |
| 81 | +MAKEFILE_FILES=() |
| 82 | +while IFS= read -r -d '' file; do |
| 83 | + MAKEFILE_FILES+=("$file") |
| 84 | +done < <(find . -name 'Makefile' -not -path './pkg/go-containerregistry/*' -print0) |
| 85 | +DOCKERFILE_FILES=() |
| 86 | +while IFS= read -r -d '' file; do |
| 87 | + DOCKERFILE_FILES+=("$file") |
| 88 | +done < <(find . -name 'Dockerfile*' -not -path './pkg/go-containerregistry/*' -print0) |
| 89 | + |
| 90 | +case "${1:-}" in |
| 91 | + check) |
| 92 | + echo "Checking Go version consistency..." |
| 93 | + echo "" |
| 94 | + |
| 95 | + for gofile in "${GO_FILES[@]}"; do |
| 96 | + check_file "$gofile" "^go $GO_VERSION" "^go " "go $GO_VERSION" |
| 97 | + done |
| 98 | + |
| 99 | + for makefile in "${MAKEFILE_FILES[@]}"; do |
| 100 | + if grep -q "^GO_VERSION := " "$makefile" 2>/dev/null; then |
| 101 | + check_file "$makefile" "^GO_VERSION := $GO_VERSION" "^GO_VERSION := " "GO_VERSION := $GO_VERSION" |
| 102 | + fi |
| 103 | + done |
| 104 | + |
| 105 | + for dockerfile in "${DOCKERFILE_FILES[@]}"; do |
| 106 | + if grep -q "ARG GO_VERSION=" "$dockerfile" 2>/dev/null; then |
| 107 | + check_file "$dockerfile" "ARG GO_VERSION=$GO_VERSION_MINOR" "ARG GO_VERSION=" "ARG GO_VERSION=$GO_VERSION_MINOR" |
| 108 | + fi |
| 109 | + done |
| 110 | + |
| 111 | + if [[ "$NEEDS_UPDATE" -eq 1 ]]; then |
| 112 | + echo "" |
| 113 | + echo "Files are out of sync. Run: ./scripts/sync-go-version.sh sync" |
| 114 | + exit 1 |
| 115 | + else |
| 116 | + echo "All files are in sync." |
| 117 | + exit 0 |
| 118 | + fi |
| 119 | + ;; |
| 120 | + sync) |
| 121 | + echo "Syncing Go version to $GO_VERSION..." |
| 122 | + echo "" |
| 123 | + |
| 124 | + for gofile in "${GO_FILES[@]}"; do |
| 125 | + update_file "$gofile" "^go .*" "go $GO_VERSION" |
| 126 | + done |
| 127 | + |
| 128 | + for makefile in "${MAKEFILE_FILES[@]}"; do |
| 129 | + update_file "$makefile" "^GO_VERSION := .*" "GO_VERSION := $GO_VERSION" |
| 130 | + done |
| 131 | + |
| 132 | + for dockerfile in "${DOCKERFILE_FILES[@]}"; do |
| 133 | + update_file "$dockerfile" "ARG GO_VERSION=.*" "ARG GO_VERSION=$GO_VERSION_MINOR" |
| 134 | + done |
| 135 | + |
| 136 | + echo "" |
| 137 | + echo "Done. Review changes with: git diff" |
| 138 | + ;; |
| 139 | + *) |
| 140 | + usage |
| 141 | + ;; |
| 142 | +esac |
0 commit comments