dotfiles/.local/bin/ghup

48 lines
1.5 KiB
Bash
Executable file

#! /usr/bin/bash
set -euo pipefail
IFS=$'\n\t'
STATE_DIR="$(systemd-path user-state-private)"/github-versions
mkdir --parents "${STATE_DIR}"
github_update() {
package="$1"
repo="$2"
resource="$3"
post_fetch="$4"
response=$(curl --silent "https://api.github.com/repos/${repo}/releases/latest")
latest_version=$(printf '%s' "${response}" | jq --raw-output .name)
VERSION_FILE="${STATE_DIR}/${package}"
install="false"
if [ -f "${VERSION_FILE}" ]; then
installed_version=$(cat "${VERSION_FILE}")
if [ "${installed_version}" = "${latest_version}" ]; then
echo "Latest version \"${package} ${installed_version}\" is already installed"
else
echo "Upgrading \"${package} ${installed_version}\" -> \"${package} ${latest_version}\"..."
install="true"
fi
else
echo "Installing \"${package} ${latest_version}\"..."
install="true"
fi
if [ "${install}" = "true" ]; then
asset=$(printf '%s' "${response}" | jq --raw-output ".assets[] | select(.name == \"${resource}\").browser_download_url")
curl --location "${asset}" | "${post_fetch}" && \
echo "${latest_version}" > "${VERSION_FILE}" && \
echo "Successfully installed \"${package} ${latest_version}\""
fi
}
PKG_DIR="$(systemd-path user-shared)"/github-versions
if [ -d "${PKG_DIR}" ]; then
echo "Starting iteration"
for package in "${PKG_DIR}"/*; do
# shellcheck source=/dev/null
. "${package}"
done
fi