Skip to content

Instantly share code, notes, and snippets.

@popey
Last active Apr 30, 2021
Embed
What would you like to do?
#!/bin/bash
# Check for updates to a bunch of snaps
DATESTAMP=$(date +%Y-%m-%d)
TIMESTAMP=$(date +%H%M%S)
LOGFILE=./reports/$DATESTAMP-$TIMESTAMP.txt
SNAPSFILE="./snaps.csv"
USERNAME="popey"
TMPDIR=$(mktemp -d)
RELEASESFILE=$TMPDIR/releases.json
LATESTFILE=$TMPDIR/latest.json
TAGSFILE=$TMPDIR/tags.json
# Set GH API token
# shellcheck source=/home/alan/.config/ghtoken
source "/home/alan/.config/ghtoken"
# Whether to display edge version
EDGE=true
count_snaps () {
NUMSNAPS=$(wc -l $SNAPSFILE |cut -d ' ' -f1)
}
start_logfile () {
echo -e "Snap" "\t" "Application" "\t\t" "Publisher" "\t\t" "Stable" "\t\t" "Upstream" "\t" "OK?(✔/✖)" $(if [ "$EDGE" == "true" ]; then echo "\t\t" "Edge"; fi ) > $LOGFILE
}
clear_warnings () {
snap warnings > /dev/null
snap okay > /dev/null
}
check_github_api () {
curl -s -u $USERNAME:$GHTOKEN https://api.github.com/repos/SimonLarsen/mrrescue/releases/latest -o $RELEASESFILE
if [ "$?" == "8" ]; then
echo "Possible github api failure"
exit
fi
}
dot () {
echo -n "."
}
get_latest () {
curl -s -u $USERNAME:$GHTOKEN https://api.github.com/repos/"$1"/"$2"/releases/latest -o $LATESTFILE
if [ "$?" == "8" ]; then
echo "Possible github api failure"
exit
fi
}
get_releases () {
curl -s -u $USERNAME:$GHTOKEN https://api.github.com/repos/"$1"/"$2"/releases -o $RELEASESFILE
if [ "$?" == "8" ]; then
echo "Possible github api failure"
exit
fi
}
get_tags () {
curl -s -u $USERNAME:$GHTOKEN https://api.github.com/repos/"$1"/"$2"/tags -o $TAGSFILE
if [ "$?" == "8" ]; then
echo "Possible github api failure"
exit
fi
}
get_stable_snap () {
snap info "$1" | awk '$1 == "latest/stable:" { print $2 }'
}
get_edge_snap () {
snap info "$1" | awk '$1 == "latest/edge:" { print $2 }'
}
get_snap_publisher () {
snap info "$1" | grep "^publisher" | cut -d ' ' -f2- | tr ' ' '.'
}
get_snap_details () {
snap_stable="$(get_stable_snap $SnapName)"
snap_edge="$(get_edge_snap $SnapName)"
snap_publisher="$(get_snap_publisher $SnapName)"
}
cleanup () {
for file in "$RELEASESFILE" "$LATESTFILE" "$TAGSFILE";
do
if [ -f "$file" ]; then
rm "$file"
fi
done
}
print_output () {
url=""
if [ "$snap_edge" == "$upstream_version" ] && [ "$snap_stable" != "$upstream_version" ]; then
url="https://snapcraft.io/$SnapName/releases"
elif [ "$snap_edge" != "$upstream_version" ] && [ "$snap_stable" != "$upstream_version" ]; then
url="https://snapcraft.io/$SnapName/builds"
fi
echo -e "$SnapName" "\t" "$FriendlyName" "\t" "$snap_publisher" "\t" "$snap_stable" "\t" "$upstream_version" "\t" $(if [ "$snap_stable" == "$upstream_version" ]; then echo ""; else echo ""; fi ) $(if [ "$EDGE" == "true" ]; then echo -e "\t\t" "$snap_edge"; fi ) "\t" $url >> $LOGFILE
}
atom () {
DEB_API="https://atom.io/download/deb"
DEB_URL=$(curl -w "%{url_effective}\n" -I -L -s -S "${DEB_API}" -o /dev/null)
upstream_version=$(echo "${DEB_URL}" | cut -d'/' -f4 | tr -d 'v')
}
android_studio () {
upstream_version=$(python3 -u get_latest_android_studio.py | awk -F " " '{ print $1 }')
}
android_studio_canary () {
upstream_version=$(python3 -u get_latest_android_studio_canary.py | awk -F " " '{ print $1 }')
}
discord () {
deb_api="https://discordapp.com/api/download?platform=linux&format=deb"
deb_url=$(curl -w "%{url_effective}\n" -I -L -s -S "${deb_api}" -o /dev/null)
upstream_version=$(echo "${deb_url}" | cut -d'/' -f6)
}
jenkins () {
curl -sLO http://mirrors.jenkins.io/war/latest/jenkins.war
unzip -q jenkins.war META-INF/MANIFEST.MF
upstream_version=$(grep Jenkins-Version META-INF/MANIFEST.MF | cut -d' ' -f2 | tr -d '\r')
rm -rf ./META-INF/
rm jenkins.war
}
process_csv () {
while IFS=, read -r FriendlyName SnapName Type API Owner Repo
do
dot
get_snap_details
# Special cases
case $SnapName in
android-studio)
android_studio
;;
android-studio-canary)
android_studio_canary
;;
atom)
atom
;;
discord)
discord
;;
jenkins)
jenkins
;;
*)
#echo "$FriendlyName $SnapName $Type $API $Owner $Repo"
if [ "$Type" == "github" ]; then
if [ "$API" == "releases" ]; then
get_releases "$Owner" "$Repo"
# Special cases
case $SnapName in
dosbox-x)
upstream_version=$(jq . $RELEASESFILE | grep tag_name | cut -d'"' -f4 | sed -e 's/dosbox-x-//' -e 's/wip-//' -e 's/windows-//' -e 's/v//' | head -n 1)
;;
b2)
upstream_version=$(jq . $RELEASESFILE | grep tag_name | cut -d'"' -f4 | cut -d- -f2-4 | head -n 1)
;;
wire)
upstream_version=$(jq . $RELEASESFILE | grep tag_name | cut -d'"' -f4 | grep linux | head -n 1 | cut -d'/' -f2)
;;
signal-desktop)
upstream_version=$(jq . $RELEASESFILE | grep tag_name | grep -v beta | head -n 1 | cut -d'"' -f4 | tr -d 'v')
;;
opentoonz-morevna)
upstream_version=$(jq . $RELEASESFILE | grep tag_name | cut -d'"' -f4 | tr -d "v" | head -n 1 | sed s'/-morena//')
;;
*)
upstream_version=$(jq . $RELEASESFILE | grep tag_name | cut -d'"' -f4 | tr -d "v" | head -n 1)
;;
esac
elif [ "$API" == "latest" ]; then
get_latest "$Owner" "$Repo"
# Special cases
case $SnapName in
brackets)
upstream_version=$(jq . $LATESTFILE | grep tag_name | cut -d'"' -f4 | sed s'/release-//')
;;
*)
upstream_version=$(jq . $LATESTFILE | grep tag_name | cut -d'"' -f4 | tr -d "v" )
;;
esac
elif [ "$API" == "tags" ]; then
get_tags "$Owner" "$Repo"
# Special cases
case $SnapName in
mutt)
upstream_version=$(jq . $TAGSFILE | grep name | cut -d'"' -f4 | grep -v patch | sed -e 's/mutt-//' -e 's/.rel//' | sort -rn | head -n 1)
;;
flightgear)
upstream_version=$(jq . $TAGSFILE | grep name | cut -d'"' -f4 | sed -e 's/version\///' | head -n 1)
;;
xonotic)
upstream_version=$(jq . $TAGSFILE | grep name | cut -d'"' -f4 | head -n 1 | tr -d "v" | sed 's/xonotic-//')
;;
*)
upstream_version=$(jq . $TAGSFILE | grep name | cut -d'"' -f4 | head -n 1 | tr -d "v")
;;
esac
fi
fi
esac
# Echo results
print_output
cleanup
done < "$SNAPSFILE"
}
clear_warnings
check_github_api
start_logfile
count_snaps
process_csv
echo " "
clear
echo "Processed $NUMSNAPS snaps"
cat $LOGFILE | column -t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment