Skip to content

Commit 8164e4c

Browse files
committed
Add --update-script option to cvm.sh for automatic script updates
1 parent 354f211 commit 8164e4c

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

cvm.sh

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#H# --remove <version> Removes a locally available version
2424
#H# --install Adds an alias `cursor` and downloads the latest version
2525
#H# --uninstall Removes the Cursor version manager directory and alias
26+
#H# --update-script Updates the (cvm) script to the latest version
2627
#H# -v --version Shows the script version
2728
#H# -h --help Shows this message
2829

@@ -36,7 +37,7 @@ DOWNLOADS_DIR="$CURSOR_DIR/app-images"
3637
CVM_VERSION="1.2.0"
3738
_CACHE_FILE="/tmp/cursor_versions.json"
3839
VERSION_HISTORY_URL="https://raw.githubusercontent.com/oslook/cursor-ai-downloads/refs/heads/main/version-history.json"
39-
40+
GITHUB_API_URL="https://api.github.com/repos/ivstiv/cursor-version-manager/releases/latest"
4041

4142
#
4243
# Functions
@@ -45,6 +46,17 @@ help() {
4546
sed -rn 's/^#H# ?//;T;p' "$0"
4647
}
4748

49+
getLatestScriptVersion() {
50+
# Fetch latest release version from GitHub API
51+
latest_version=$(wget -qO- "$GITHUB_API_URL" | jq -r '.tag_name' 2>/dev/null)
52+
if [ -n "$latest_version" ]; then
53+
echo "$latest_version"
54+
return 0
55+
else
56+
return 1
57+
fi
58+
}
59+
4860
getVersionHistory() {
4961
# Check if cache file exists and is less than 15 min old
5062
if [ -f "$_CACHE_FILE" ] && [ -n "$(find "$_CACHE_FILE" -mmin -15 2>/dev/null)" ]; then
@@ -243,6 +255,44 @@ cleanupAppImages() {
243255
done
244256
}
245257

258+
updateScript() {
259+
version=$(getLatestScriptVersion)
260+
261+
if [ -z "$version" ]; then
262+
echo "Error: Failed to determine version to download" >&2
263+
return 1
264+
fi
265+
266+
# Get the download URL from the release assets
267+
download_url=$(
268+
wget -O- "$GITHUB_API_URL" \
269+
| jq -r '.assets[] | select(.name == "cvm.sh") | .browser_download_url'
270+
)
271+
272+
if [ -z "$download_url" ]; then
273+
echo "Error: Failed to find download URL for cvm.sh" >&2
274+
return 1
275+
fi
276+
277+
echo "Downloading CVM version ${version}..."
278+
279+
# Download to a temporary file in the same directory
280+
script_dir=$(dirname "$0")
281+
temp_file="${script_dir}/cvm.sh.new"
282+
283+
if wget -qO "$temp_file" "$download_url"; then
284+
chmod +x "$temp_file"
285+
mv "$temp_file" "$0"
286+
echo "Successfully updated to version ${version}"
287+
echo "Please run the script again to use the new version"
288+
return 0
289+
else
290+
rm -f "$temp_file"
291+
echo "Error: Failed to download version ${version}" >&2
292+
return 1
293+
fi
294+
}
295+
246296

247297

248298
#
@@ -265,7 +315,17 @@ case "$1" in
265315
help
266316
;;
267317
--version|-v)
268-
echo "$CVM_VERSION"
318+
echo "Current version: $CVM_VERSION"
319+
if latest_version=$(getLatestScriptVersion); then
320+
if [ "$latest_version" != "$CVM_VERSION" ]; then
321+
echo "Latest version available: $latest_version"
322+
echo "You can download the latest version with: $0 --update-script"
323+
else
324+
echo "You are running the latest version"
325+
fi
326+
else
327+
echo "Failed to check for latest version"
328+
fi
269329
;;
270330
--update)
271331
latestVersion=$(getLatestRemoteVersion)
@@ -356,6 +416,9 @@ case "$1" in
356416
--uninstall)
357417
uninstallCVM
358418
;;
419+
--update-script)
420+
updateScript
421+
;;
359422
*)
360423
echo "Unknown command: $1"
361424
help

0 commit comments

Comments
 (0)