refactor: improve error handling in GitHub release download function
This commit is contained in:
parent
73999b2f0a
commit
c4d8698741
1 changed files with 18 additions and 22 deletions
|
@ -20,31 +20,27 @@ cleanup() {
|
|||
# Usage: download_latest_deb "owner/repo" "output_file" ["asset_pattern"]
|
||||
# curtsey of Grok
|
||||
download_latest_deb() {
|
||||
local repo="$1" # GitHub repo (e.g., "TabbyML/tabby")
|
||||
local output="$2" # Output file path (e.g., "tabby.deb")
|
||||
local pattern="$3" # Optional: regex pattern to filter assets (e.g., "tabby_.*_amd64.deb")
|
||||
local api_url="https://api.github.com/repos/$repo/releases/latest"
|
||||
local download_url
|
||||
local repo="$1" # GitHub repo (e.g., "Eugeny/tabby")
|
||||
local output="$2" # Output file path (e.g., "/tmp/tabby_amd64.deb")
|
||||
local pattern="$3" # Optional: regex pattern to filter assets (e.g., "tabby.*amd64.deb")
|
||||
local api_url="https://api.github.com/repos/$repo/releases/latest"
|
||||
local download_url
|
||||
|
||||
# Fetch the latest release JSON and extract the download URL for the .deb
|
||||
download_url=$(curl -s "$api_url" | jq -r --arg pat "${pattern:-.*\.deb}" \
|
||||
'.assets[] | select(.name | test($pat)) | .browser_download_url')
|
||||
# Fetch the latest release JSON and extract the download URL for the .deb
|
||||
download_url=$(curl -s -f "$api_url" | jq -r --arg pat "${pattern:-.*\.deb}" \
|
||||
'.assets[] | select(.name | test($pat)) | .browser_download_url')
|
||||
if [[ -z "$download_url" || $? -ne 0 ]]; then
|
||||
echo "Error: No .deb file found for $repo with pattern '$pattern' or API request failed" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [[ -z "$download_url" ]]; then
|
||||
echo "Error: No .deb file found for $repo with pattern '$pattern'"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Download the .deb file
|
||||
echo "Downloading $download_url to $output..."
|
||||
curl -sL "$download_url" -o "$output"
|
||||
|
||||
if [[ $? -eq 0 ]]; then
|
||||
# Download the .deb file
|
||||
echo "Downloading $download_url to $output..."
|
||||
if ! curl -sL -f "$download_url" -o "$output"; then
|
||||
echo "Error: Failed to download $output" >&2
|
||||
return 1
|
||||
fi
|
||||
echo "Successfully downloaded $output"
|
||||
else
|
||||
echo "Error: Failed to download $output"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# System update and base tools
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue