Skip to content

Commit aa57c27

Browse files
Rewrite script to use go mod edit instead of grep/sed
Replace manual parsing and editing with go mod edit: - Use 'go mod edit -json' to read current go and toolchain directives - Use 'go mod edit -go' to update go directive - Use 'go mod edit -toolchain' to update toolchain directive - Remove manual sed/grep parsing and .bak file handling - More reliable and maintainable than custom text manipulation Co-authored-by: williammartin <1611510+williammartin@users.noreply.github.com>
1 parent a251bb7 commit aa57c27

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

.github/workflows/scripts/bump-go.sh

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,14 @@ LATEST_MAJOR_MINOR="$(cut -d. -f1-2 <<< "$TOOLCHAIN_VERSION")"
4747

4848
echo " → latest toolchain : $TOOLCHAIN_VERSION"
4949

50-
# ---- Prepare Git branch ---------------------------------------------------
51-
CURRENT_GO_DIRECTIVE=$(grep -E '^go ' "$GO_MOD" | cut -d ' ' -f2)
52-
CURRENT_TOOLCHAIN_DIRECTIVE=$(grep -E '^toolchain ' "$GO_MOD" | cut -d ' ' -f2 || true)
50+
# ---- Read current go.mod state using go mod edit ----------------------------
51+
GO_MOD_JSON=$(go mod edit -json "$GO_MOD")
52+
CURRENT_GO_DIRECTIVE=$(jq -r '.Go // ""' <<< "$GO_MOD_JSON")
53+
CURRENT_TOOLCHAIN_DIRECTIVE=$(jq -r '.Toolchain // ""' <<< "$GO_MOD_JSON")
54+
55+
# Handle empty values from jq
56+
[[ "$CURRENT_GO_DIRECTIVE" == "null" || -z "$CURRENT_GO_DIRECTIVE" ]] && CURRENT_GO_DIRECTIVE=""
57+
[[ "$CURRENT_TOOLCHAIN_DIRECTIVE" == "null" || -z "$CURRENT_TOOLCHAIN_DIRECTIVE" ]] && CURRENT_TOOLCHAIN_DIRECTIVE=""
5358

5459
CURRENT_MAJOR_MINOR="$(cut -d. -f1-2 <<< "$CURRENT_GO_DIRECTIVE")"
5560

@@ -87,12 +92,12 @@ echo "Creating branch $BRANCH"
8792
git switch -c "$BRANCH" >/dev/null 2>&1
8893
BRANCH_CREATED=1
8994

90-
# ---- Patch go.mod -----------------------------------------------------------
95+
# ---- Patch go.mod using go mod edit -----------------------------------------
9196
# Only update go directive if we're not already at the latest major.minor version
9297
if [[ "$CURRENT_MAJOR_MINOR" != "$LATEST_MAJOR_MINOR" ]]; then
9398
# Bump to the latest major.minor.0 (preserves the convention of X.Y.0 for go directive)
9499
NEW_GO_DIRECTIVE="$LATEST_MAJOR_MINOR.0"
95-
sed -Ei.bak "s/^go [0-9]+\.[0-9]+.*$/go $NEW_GO_DIRECTIVE/" "$GO_MOD"
100+
go mod edit -go="$NEW_GO_DIRECTIVE" "$GO_MOD"
96101
echo " • go directive $CURRENT_GO_DIRECTIVE$NEW_GO_DIRECTIVE"
97102
# After updating, the current go directive is now the new one for toolchain logic
98103
CURRENT_GO_DIRECTIVE="$NEW_GO_DIRECTIVE"
@@ -106,19 +111,16 @@ if [[ -z "$CURRENT_TOOLCHAIN_DIRECTIVE" ]]; then
106111
# go directive is at latest major.minor - toolchain line is redundant
107112
echo " • toolchain directive not needed (go version matches latest toolchain)"
108113
else
109-
# go directive is older than latest toolchain - add toolchain directive after go line
110-
sed -Ei.bak "/^go [0-9]+\.[0-9]+/a\\
111-
toolchain go$TOOLCHAIN_VERSION" "$GO_MOD"
114+
# go directive is older than latest toolchain - add toolchain directive
115+
go mod edit -toolchain="go$TOOLCHAIN_VERSION" "$GO_MOD"
112116
echo " • toolchain directive added: go$TOOLCHAIN_VERSION"
113117
fi
114118
elif [[ "$CURRENT_TOOLCHAIN_DIRECTIVE" != "go$TOOLCHAIN_VERSION" ]]; then
115119
# Toolchain directive exists but needs updating
116-
sed -Ei.bak "s/^toolchain go[0-9]+\.[0-9]+\.[0-9]+.*$/toolchain go$TOOLCHAIN_VERSION/" "$GO_MOD"
120+
go mod edit -toolchain="go$TOOLCHAIN_VERSION" "$GO_MOD"
117121
echo " • toolchain $CURRENT_TOOLCHAIN_DIRECTIVE → go$TOOLCHAIN_VERSION"
118122
fi
119123

120-
rm -f "$GO_MOD.bak"
121-
122124
git add "$GO_MOD"
123125

124126
# ---- Commit -----------------------------------------------------------------
@@ -149,9 +151,13 @@ if [[ $APPLY -eq 0 ]]; then
149151
fi
150152

151153
# ---- Push & PR --------------------------------------------------------------
152-
# Get the actual go directive from the updated go.mod
153-
FINAL_GO_DIRECTIVE=$(grep -E '^go ' "$GO_MOD" | cut -d ' ' -f2)
154-
FINAL_TOOLCHAIN_DIRECTIVE=$(grep -E '^toolchain ' "$GO_MOD" | cut -d ' ' -f2 || true)
154+
# Get the actual go directive from the updated go.mod using go mod edit
155+
FINAL_GO_MOD_JSON=$(go mod edit -json "$GO_MOD")
156+
FINAL_GO_DIRECTIVE=$(jq -r '.Go // ""' <<< "$FINAL_GO_MOD_JSON")
157+
FINAL_TOOLCHAIN_DIRECTIVE=$(jq -r '.Toolchain // ""' <<< "$FINAL_GO_MOD_JSON")
158+
159+
# Handle empty/null values
160+
[[ "$FINAL_TOOLCHAIN_DIRECTIVE" == "null" || -z "$FINAL_TOOLCHAIN_DIRECTIVE" ]] && FINAL_TOOLCHAIN_DIRECTIVE=""
155161

156162
if [[ -n "$FINAL_TOOLCHAIN_DIRECTIVE" ]]; then
157163
PR_BODY=$(cat <<EOF

0 commit comments

Comments
 (0)