Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion hack/go-version.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
#!/bin/bash -e
grep ^go go.mod | awk '{print $2}'
toolchain=$(grep '^toolchain' go.mod | awk '{print $2}' | sed 's/^go//')
if [ -n "$toolchain" ]; then
echo "$toolchain"
else
grep '^go' go.mod | awk '{print $2}'
fi
Comment on lines +2 to +7
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation uses multiple pipes and multiple passes over the go.mod file. It also uses grep patterns that could potentially match non-directive lines (e.g., module paths starting with go or toolchain in a require block).

A more robust and efficient approach is to use a single awk command to parse the file in one pass, ensuring only the intended directives are matched and correctly handling the fallback logic.

Suggested change
toolchain=$(grep '^toolchain' go.mod | awk '{print $2}' | sed 's/^go//')
if [ -n "$toolchain" ]; then
echo "$toolchain"
else
grep '^go' go.mod | awk '{print $2}'
fi
awk '/^toolchain[[:space:]]/ { sub(/^go/, "", $2); print $2; f=1; exit } /^go[[:space:]]/ { v=$2 } END { if (!f && v) print v }' go.mod

Loading