chore(scripts): improve install script readability and error handling

This commit is contained in:
2025-12-13 16:43:19 +01:00
parent ec51d271e3
commit 2a2ad79bb4

View File

@@ -54,15 +54,34 @@ show_help() {
POSITIONAL=() POSITIONAL=()
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case "$1" in case "$1" in
-h|--help) show_help; exit 0 ;; -h | --help)
-n|--dry-run) DRY_RUN=true; shift ;; show_help
exit 0
;;
-n | --dry-run)
DRY_RUN=true
shift
;;
-a | --arch) -a | --arch)
[[ $# -lt 2 ]] && { echo "❌ Option $1 requires an argument"; exit 1; } [[ $# -lt 2 ]] && {
ARCH_OS_INPUT="$2"; shift 2 ;; echo "❌ Option $1 requires an argument"
exit 1
}
ARCH_OS_INPUT="$2"
shift 2
;;
-d | --dir) -d | --dir)
[[ $# -lt 2 ]] && { echo "❌ Option $1 requires an argument"; exit 1; } [[ $# -lt 2 ]] && {
INSTALL_DIR="$2"; shift 2 ;; echo "❌ Option $1 requires an argument"
*) POSITIONAL+=("$1"); shift ;; exit 1
}
INSTALL_DIR="$2"
shift 2
;;
*)
POSITIONAL+=("$1")
shift
;;
esac esac
done done
if [[ ${#POSITIONAL[@]} -gt 0 ]]; then if [[ ${#POSITIONAL[@]} -gt 0 ]]; then
@@ -98,8 +117,16 @@ if command -v git-cliff >/dev/null; then
fi fi
# 4 Check for required tools # 4 Check for required tools
need() { command -v "$1" >/dev/null || { echo "$1 is missing"; exit 1; }; } need() { command -v "$1" >/dev/null || {
need curl; need tar; need grep; need sed; need awk; need jq echo "$1 is missing"
exit 1
}; }
need curl
need tar
need grep
need sed
need awk
need jq
# 5 Determine version → Fetch release JSON # 5 Determine version → Fetch release JSON
if [[ "$VERSION" == "latest" ]]; then if [[ "$VERSION" == "latest" ]]; then
@@ -109,14 +136,23 @@ else
fi fi
echo "🔍 Fetching release info ($API_URL)…" 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" | ASSET_URL=$(jq -r '.assets[]?.browser_download_url' <<<"$JSON" |
grep -E "${ARCH_OS}\.(tar\.(gz|xz)|zip)$" | head -n1) grep -E "${ARCH_OS}\.(tar\.(gz|xz)|zip)$" | head -n1)
[[ -z "$ASSET_URL" ]] && { echo "❌ Matching asset not found for architecture ${ARCH_OS}"; exit 1; } [[ -z "$ASSET_URL" ]] && {
echo "❌ Matching asset not found for architecture ${ARCH_OS}"
exit 1
}
ASSET_FILE=$(basename "$ASSET_URL") ASSET_FILE=$(basename "$ASSET_URL")
echo "📦 Downloading git-cliff ${VERSION} (${ASSET_FILE}) …" echo "📦 Downloading git-cliff ${VERSION} (${ASSET_FILE}) …"
@@ -127,19 +163,30 @@ curl -#L -o "${TMP}/${ASSET_FILE}" "$ASSET_URL"
case "$ASSET_FILE" in case "$ASSET_FILE" in
*.tar.gz | *.tgz) tar -C "$TMP" -xzf "${TMP}/${ASSET_FILE}" ;; *.tar.gz | *.tgz) tar -C "$TMP" -xzf "${TMP}/${ASSET_FILE}" ;;
*.tar.xz) tar -C "$TMP" -xJf "${TMP}/${ASSET_FILE}" ;; *.tar.xz) tar -C "$TMP" -xJf "${TMP}/${ASSET_FILE}" ;;
*.zip) need unzip; unzip -q "${TMP}/${ASSET_FILE}" -d "$TMP" ;; *.zip)
*) echo "❌ Unknown archive format: $ASSET_FILE"; exit 1 ;; need unzip
unzip -q "${TMP}/${ASSET_FILE}" -d "$TMP"
;;
*)
echo "❌ Unknown archive format: $ASSET_FILE"
exit 1
;;
esac esac
# 7 Locate & (maybe) install binary # 7 Locate & (maybe) install binary
BIN_PATH=$(find "$TMP" -type f -name git-cliff -perm -u+x | head -n1) BIN_PATH=$(find "$TMP" -type f -name git-cliff -perm -u+x | head -n1)
[[ -z "$BIN_PATH" ]] && { echo "❌ Binary not found"; exit 1; } [[ -z "$BIN_PATH" ]] && {
echo "❌ Binary not found"
exit 1
}
if $DRY_RUN; then if $DRY_RUN; then
echo "🧪 Dry-run: verified git-cliff binary at ${BIN_PATH}" echo "🧪 Dry-run: verified git-cliff binary at ${BIN_PATH}"
echo "ℹ️ Would install to: ${INSTALL_DIR}/git-cliff (sudo install -m755 …)" echo "ℹ️ Would install to: ${INSTALL_DIR}/git-cliff (sudo install -m755 …)"
echo "✅ git-cliff $("$BIN_PATH" --version) would be installed successfully" echo "✅ git-cliff $("$BIN_PATH" --version) would be installed successfully"
else else
sudo install -m755 "$BIN_PATH" "${INSTALL_DIR}/git-cliff" SUDO=""
[[ $EUID -eq 0 ]] || SUDO="sudo"
$SUDO install -m755 "$BIN_PATH" "${INSTALL_DIR}/git-cliff"
echo "✅ git-cliff $(git-cliff --version) installed in ${INSTALL_DIR}" echo "✅ git-cliff $(git-cliff --version) installed in ${INSTALL_DIR}"
fi fi