Skip to content

Instantly share code, notes, and snippets.

@shvchk
Last active June 27, 2023 15:45
Show Gist options
  • Select an option

  • Save shvchk/e2831b92b9243361eceb457e113abc68 to your computer and use it in GitHub Desktop.

Select an option

Save shvchk/e2831b92b9243361eceb457e113abc68 to your computer and use it in GitHub Desktop.
youtubedr updater
#! /usr/bin/env bash
set -euo pipefail
executable="youtubedr"
executable_dir="$(dirname -- "$(which $executable)")"
repo="kkdai/youtube"
release_url="https://api.github.com/repos/$repo/releases/latest"
cur_version="$($executable version | grep '^Version:' | awk '{print $2}')"
new_version_raw="$(curl -sL "$release_url" | jq -r .tag_name)"
new_version="${new_version_raw:1}" # remove 'v'
tmp_dir=$(mktemp -d)
case "$(uname -m)" in
x86_64) arch="amd64" ;;
arm64) arch="arm64" ;;
*) echo "Unsupported architecture: $(uname -m). Aborting"; exit ;;
esac
case "$OSTYPE" in
darwin*) os="darwin" ;;
linux*) os="linux" ;;
bsd*) os="freebsd" ;;
*) echo "Unsupported OS: ${OSTYPE}. Aborting"; exit ;;
esac
_update() {
jq_expr=".assets[].browser_download_url | select(endswith(\"${os}_${arch}.tar.gz\"))"
link="$(curl -sL "$release_url" | jq -r "$jq_expr")"
archive_path="${tmp_dir}/$(basename -- "$link")"
curl -sL -o "$archive_path" "$link"
tar -xf "$archive_path" -C "$executable_dir" "$executable"
}
_is_version_newer() {
! printf '%s\n' "$1" "$2" | sort -CV
}
_cleanup() {
rm -rf "$tmp_dir"
}
trap _cleanup EXIT
if _is_version_newer "$new_version" "$cur_version"; then
echo "New version found, upgrading..."
_update
else
echo "No new version found"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment