From 9e35c1c6b9641f6f64282101c6b066dcda436f3f Mon Sep 17 00:00:00 2001 From: "Max P." Date: Fri, 27 Jun 2025 21:48:33 +0200 Subject: [PATCH] feat(script): enhance git-cliff installer with extended options - Add support for custom architecture, installation directory, and dry-run mode - Improve version comparison and upgrade logic for better usability - Refactor help output and argument parsing for clarity and flexibility - Ensure compatibility with additional architecture triplets --- install-git-cliff.sh | 148 +++++++++++++++++++++++++++++++------------ 1 file changed, 109 insertions(+), 39 deletions(-) mode change 100644 => 100755 install-git-cliff.sh diff --git a/install-git-cliff.sh b/install-git-cliff.sh old mode 100644 new mode 100755 index c00898a..6edf4a9 --- a/install-git-cliff.sh +++ b/install-git-cliff.sh @@ -1,41 +1,114 @@ #!/usr/bin/env bash -# install-git-cliff.sh – fetches the latest or specified git-cliff binary (x86_64) -# Usage: sudo ./install-git-cliff.sh # latest version -# sudo ./install-git-cliff.sh 2.9.0 # specific version -# sudo ./install-git-cliff.sh -h # help +# install-git-cliff.sh – fetches the latest or specified git-cliff binary +# Usage (back-compatible): +# sudo ./install-git-cliff.sh # latest version, default arch/dir +# sudo ./install-git-cliff.sh 2.9.1 # specific version +# Extended options: +# sudo ./install-git-cliff.sh -a x86_64-linux-musl [] # custom arch +# sudo ./install-git-cliff.sh -d /opt/tools [] # custom install dir +# sudo ./install-git-cliff.sh -n [] # dry-run (no install) +# sudo ./install-git-cliff.sh -h | --help # help +# +# Behaviour: +# • If git-cliff is already in \$PATH and **no version** was requested, the script exits early. +# • If a specific version was requested, the installed version is compared; identical → exit, different → upgrade. set -euo pipefail REPO="orhun/git-cliff" -ARCH_OS="x86_64-unknown-linux-gnu" -INSTALL_DIR="/usr/local/bin" -VERSION="${1:-latest}" -# 0 Handle -h/--help option -if [[ "$VERSION" == "-h" || "$VERSION" == "--help" ]]; then +# ─────────────────────────────────────────────── +# Defaults (internal vs. user-facing) +ARCH_OS_DEFAULT="x86_64-unknown-linux-gnu" +DEFAULT_ARCH_DISPLAY="${ARCH_OS_DEFAULT/-unknown-/}" # shown to user → x86_64-linux-gnu +INSTALL_DIR_DEFAULT="/usr/local/bin" +ARCH_OS="$ARCH_OS_DEFAULT" +INSTALL_DIR="$INSTALL_DIR_DEFAULT" +VERSION="latest" +DRY_RUN=false +USER_VERSION_SPECIFIED=false +# ─────────────────────────────────────────────── + +# 0 Help function +show_help() { cat <] - sudo ./install-git-cliff.sh - Install a specific version (e.g. 2.9.0) +Options: + -a, --arch Target architecture (default: ${DEFAULT_ARCH_DISPLAY}) + -d, --dir Installation directory (default: ${INSTALL_DIR_DEFAULT}) + -n, --dry-run Download and verify, but do NOT install + -h, --help Show this help message - sudo ./install-git-cliff.sh -h | --help - Show this help message +Arguments: + git-cliff version to install (default: latest) -This script downloads the appropriate release archive from GitHub, -extracts it, and installs the 'git-cliff' binary to ${INSTALL_DIR}. +Behaviour: + • If git-cliff is already in PATH and no version was requested the script exits. + • If a specific version was requested, the installed version is compared and + only upgraded if it differs. + +Supported architecture triplets (examples): + aarch64-linux-gnu / musl / apple-darwin / pc-windows-msvc + i686-linux-gnu / musl / pc-windows-msvc + x86_64-linux-gnu / musl / apple-darwin / pc-windows-* EOF - exit 0 +} + +# 1 Parse options (keeps old single-arg form) +POSITIONAL=() +while [[ $# -gt 0 ]]; do + case "$1" in + -h|--help) show_help; exit 0 ;; + -n|--dry-run) DRY_RUN=true; shift ;; + -a|--arch) + [[ $# -lt 2 ]] && { echo "❌ Option $1 requires an argument"; exit 1; } + ARCH_OS_INPUT="$2"; shift 2 ;; + -d|--dir) + [[ $# -lt 2 ]] && { echo "❌ Option $1 requires an argument"; exit 1; } + INSTALL_DIR="$2"; shift 2 ;; + *) POSITIONAL+=("$1"); shift ;; + esac +done +if [[ ${#POSITIONAL[@]} -gt 0 ]]; then + VERSION="${POSITIONAL[0]}" + USER_VERSION_SPECIFIED=true fi -# 1 Check for required tools +# 2 Normalize architecture (strip "unknown" for user input → add internally) +if [[ -n "${ARCH_OS_INPUT:-}" ]]; then + ARCH_OS="$ARCH_OS_INPUT" + if [[ "$ARCH_OS" =~ -linux- ]] && [[ ! "$ARCH_OS" =~ -unknown-linux- ]]; then + ARCH_OS="${ARCH_OS/-linux-/-unknown-linux-}" + fi +fi + +# 3 Pre-check existing installation +if command -v git-cliff >/dev/null; then + INSTALLED_PATH=$(command -v git-cliff) + INSTALLED_VER=$(git-cliff -V | awk '{print $2}') + if ! $USER_VERSION_SPECIFIED; then + echo "✅ git-cliff already present at ${INSTALLED_PATH} (version ${INSTALLED_VER}) – nothing to do." + exit 0 + else + # strip leading 'v' for comparison consistency + REQ_VER="${VERSION#v}" + if [[ "$INSTALLED_VER" == "$REQ_VER" ]]; then + echo "✅ git-cliff ${INSTALLED_VER} already installed – requested version matches." + exit 0 + else + echo "ℹ️ git-cliff ${INSTALLED_VER} installed, upgrading to ${REQ_VER}..." + fi + fi +fi + +# 4 Check for required tools need() { command -v "$1" >/dev/null || { echo "$1 is missing"; exit 1; }; } need curl; need tar; need grep; need sed; need awk; need jq -# 2 Determine version → Fetch release JSON +# 5 Determine version → Fetch release JSON if [[ "$VERSION" == "latest" ]]; then API_URL="https://api.github.com/repos/${REPO}/releases/latest" else @@ -43,30 +116,21 @@ else fi echo "🔍 Fetching release info ($API_URL)…" -JSON=$(curl -fsSL "$API_URL") || { - echo "❌ Failed to fetch release info" - exit 1 -} +JSON=$(curl -fsSL "$API_URL") || { echo "❌ Failed to fetch release info"; exit 1; } -VERSION=$(jq -r '.tag_name' <<< "$JSON") || { - echo "❌ Could not extract version" - exit 1 -} +VERSION=$(jq -r '.tag_name' <<< "$JSON") || { echo "❌ Could not extract version"; exit 1; } ASSET_URL=$(jq -r '.assets[]?.browser_download_url' <<< "$JSON" | - grep "${ARCH_OS}\.tar" | head -n1) + grep -E "${ARCH_OS}\.(tar\.(gz|xz)|zip)$" | head -n1) -if [[ -z "$ASSET_URL" ]]; then - echo "❌ Matching asset not found for architecture ${ARCH_OS}" - exit 1 -fi +[[ -z "$ASSET_URL" ]] && { echo "❌ Matching asset not found for architecture ${ARCH_OS}"; exit 1; } ASSET_FILE=$(basename "$ASSET_URL") -echo "📦 Downloading git-cliff v${VERSION} (${ASSET_FILE}) …" +echo "📦 Downloading git-cliff ${VERSION} (${ASSET_FILE}) …" TMP=$(mktemp -d) curl -#L -o "${TMP}/${ASSET_FILE}" "$ASSET_URL" -# 3 Extract based on file extension +# 6 Extract based on file extension case "$ASSET_FILE" in *.tar.gz|*.tgz) tar -C "$TMP" -xzf "${TMP}/${ASSET_FILE}" ;; *.tar.xz) tar -C "$TMP" -xJf "${TMP}/${ASSET_FILE}" ;; @@ -74,9 +138,15 @@ case "$ASSET_FILE" in *) echo "❌ Unknown archive format: $ASSET_FILE"; exit 1 ;; esac -# 4 Locate and install binary +# 7 Locate & (maybe) install binary BIN_PATH=$(find "$TMP" -type f -name git-cliff -perm -u+x | head -n1) [[ -z "$BIN_PATH" ]] && { echo "❌ Binary not found"; exit 1; } -sudo install -m755 "$BIN_PATH" "${INSTALL_DIR}/git-cliff" -echo "✅ git-cliff $(git-cliff --version) installed in ${INSTALL_DIR}" +if $DRY_RUN; then + echo "🧪 Dry-run: verified git-cliff binary at ${BIN_PATH}" + echo "ℹ️ Would install to: ${INSTALL_DIR}/git-cliff (sudo install -m755 …)" + echo "✅ git-cliff $("$BIN_PATH" --version) would be installed successfully" +else + sudo install -m755 "$BIN_PATH" "${INSTALL_DIR}/git-cliff" + echo "✅ git-cliff $(git-cliff --version) installed in ${INSTALL_DIR}" +fi