-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_go_dependency.sh
More file actions
executable file
·37 lines (37 loc) · 1.17 KB
/
update_go_dependency.sh
File metadata and controls
executable file
·37 lines (37 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/bash
set -e
if [ -z "$1" ]; then
echo "Usage: $0 <go_module_path>"
exit 1
fi
MODULE_PATH=$1
echo "Updating $MODULE_PATH to the latest major version..."
# 1. Find the current major version from go.mod
ALL_VERSIONS=$(go list -m -f '{{.Path}}' all | grep "$MODULE_PATH/v[0-9]*" || true)
HIGHEST_MAJOR_VERSION=0
for path in $ALL_VERSIONS; do
VERSION_NUMBER=$(echo "$path" | sed -E 's|.*v([0-9]+)|\1|')
if [ "$VERSION_NUMBER" -gt "$HIGHEST_MAJOR_VERSION" ]; then
HIGHEST_MAJOR_VERSION=$VERSION_NUMBER
fi
done
# 2. Find the latest major version
LATEST_VERSION_PATH="$MODULE_PATH/v$HIGHEST_MAJOR_VERSION"
for i in $(seq $(($HIGHEST_MAJOR_VERSION + 1)) 100); do
NEXT_VERSION_PATH="$MODULE_PATH/v$i"
if go list -m "$NEXT_VERSION_PATH@latest" > /dev/null 2>&1; then
LATEST_VERSION_PATH=$NEXT_VERSION_PATH
else
break
fi
done
# 3. Replace all occurrences in the code
for path in $ALL_VERSIONS; do
if [ "$path" != "$LATEST_VERSION_PATH" ]; then
echo "Replacing all occurrences of '$path' with '$LATEST_VERSION_PATH'..."
grep -rl "$path" . | env LC_ALL=C xargs sed -i '' "s|$path|$LATEST_VERSION_PATH|g"
fi
done
echo "Running go mod tidy..."
go mod tidy
echo "Done."