Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/mrgnw/scripts.
Something went wrong. Try again.
scripts gh-install
2.0 kB · 71 lines
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172#!/usr/bin/env bashset -euo pipefail
DEST="${DEST:-$HOME/.local/bin}"
usage() { echo "Install a binary from GitHub releases" echo "" echo "Usage: gh-install <owner/repo> <asset-pattern> [binary-name]" echo "" echo " asset-pattern Glob for the release asset filename (use * for version)" echo " binary-name Name of the binary inside the archive (default: repo name)" echo "" echo "Examples:" echo " gh-install orf/gping 'gping-*-aarch64-apple-darwin.tar.gz'" echo " gh-install helix-editor/helix 'helix-*-aarch64-macos.tar.xz' hx" echo " gh-install Schniz/fnm 'fnm-macos.zip' fnm" exit 1}
[[ $# -lt 2 ]] && usage
REPO="$1"PATTERN="$2"BINARY="${3:-$(basename "$REPO")}"
VER=$(curl -sI "https://github.com/${REPO}/releases/latest" | grep -i ^location | sed 's|.*/tag/||;s/\r//;s/^v//')[[ -z "$VER" ]] && echo "Could not detect latest version for ${REPO}" && exit 1echo "Latest: ${REPO} ${VER}"
FILENAME="${PATTERN//\*/$VER}"URL="https://github.com/${REPO}/releases/download/${VER}/${FILENAME}"
# some repos tag with v prefixif ! curl -sfIo /dev/null "$URL"; then URL="https://github.com/${REPO}/releases/download/v${VER}/${FILENAME}" curl -sfIo /dev/null "$URL" || { echo "Asset not found: ${FILENAME}"; exit 1; }fi
echo "Downloading ${FILENAME}"
TMPDIR=$(mktemp -d)trap 'rm -rf "$TMPDIR"' EXITcurl -sL -o "${TMPDIR}/${FILENAME}" "$URL"
mkdir -p "$DEST"
case "$FILENAME" in *.tar.gz|*.tgz) tar xzf "${TMPDIR}/${FILENAME}" -C "$TMPDIR" ;; *.tar.xz) tar xJf "${TMPDIR}/${FILENAME}" -C "$TMPDIR" ;; *.zip) unzip -o "${TMPDIR}/${FILENAME}" -d "$TMPDIR" >/dev/null ;; *) chmod +x "${TMPDIR}/${FILENAME}" mv "${TMPDIR}/${FILENAME}" "${DEST}/${BINARY}" echo "Installed ${BINARY} to ${DEST}/${BINARY}" exit 0 ;;esac
BIN=$(find "$TMPDIR" -name "$BINARY" -type f | head -1)[[ -z "$BIN" ]] && echo "Could not find '${BINARY}' in archive" && ls -R "$TMPDIR" && exit 1
chmod +x "$BIN"mv "$BIN" "${DEST}/${BINARY}"echo "Installed ${BINARY} to ${DEST}/${BINARY}"