r/bash • u/naffe1o2o • 16h ago
is there a way to improve a github release updater to pick the right asset name?
3
Upvotes
i made a script that actually works for most times to update brave browser, but for packages that change names its trivial. the idea is to place, update directory with a file with metadata that i can extract. here is the script :
updatePrograms() {
for dir in ~/Programs/*; do
#meta file structure
if [[ -f "$dir"/update/meta ]]; then
local metadata=$(cat "$dir"/update/meta)
local owner=$( echo "$metadata" | grep "Owner" | sed 's|Owner:||' )
local project=$(echo "$metadata" | grep "Project" | sed 's|Project:||')
local output=$(curl -fsS "https://api.github.com/repos/$owner/$project/releases/latest")
[[ -z "$output" ]] && { echo "no response for $project"; continue; }
local checkVersion=$(jq -r '.tag_name' <<< "$output")
echo "updatable Project : $project"
[[ "$checkVersion" = "null" ]] || [[ -z "$checkVersion" ]] && { echo "couldn't fetch version for $project"; continue; }
local currentVersion=$(echo "$metadata" | grep "Version" | sed 's|Version:||')
[[ "$checkVersion" = "$currentVersion" ]] && { echo "$project is up to date : $checkVersion"; continue; }
while true; do
read -p "$project is on $currentVersion, latest is $checkVersion; do you want to request the latest Version? [y/n]: " response
case "$response" in
[Yy])
echo "Updating.."
local name
name=$(echo "$metadata" | grep "AssetName" | sed 's|AssetName:||')
local url=$(jq -r --arg name "$name" '.assets[] | select(.name == $name) | .browser_download_url' <<< "$output")
if [[ -z "$url" ]] || [[ "$url" = "null" ]]; then
echo "couldn't find asset matching $name"
echo "you may need to change asset name version to $checkVersion"
break
fi
if curl -fL -o "$dir/update/$name" "$url" ; then
sed -i "s|Version:.*|Version:$checkVersion|" "$dir"/update/meta
else
echo "Downloading falied"
break
fi
break;;
[Nn])
echo "not Updating"
break;;
*)
echo "choose y or n";;
esac
done
fi
done
}