From 8e5f8b549b50262eb52d90c324881f7081018e0d Mon Sep 17 00:00:00 2001 From: Jerry Chae Date: Wed, 13 May 2026 22:36:03 +0900 Subject: [PATCH] =?UTF-8?q?gzip=20=EC=98=A4=EB=A5=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install-dua.sh | 131 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 106 insertions(+), 25 deletions(-) diff --git a/install-dua.sh b/install-dua.sh index dfa421b..398256a 100755 --- a/install-dua.sh +++ b/install-dua.sh @@ -5,11 +5,11 @@ # # Supported platforms: # macOS : Apple Silicon (arm64), Intel (x86_64) — via Homebrew -# Linux : x86_64, aarch64 — via package manager or curl installer +# Linux : x86_64, aarch64 — via package manager or direct download # # Prerequisites: # - macOS: Homebrew (brew) — auto-installed if missing -# - Linux: curl — for curl-based installer +# - Linux: curl — for direct download fallback set -euo pipefail @@ -17,6 +17,8 @@ set -euo pipefail # Constants # ──────────────────────────────────────────── REPO="Byron/dua-cli" +API_URL="https://api.github.com/repos/${REPO}/releases" +INSTALL_DIR="${HOME}/.local/bin" # ──────────────────────────────────────────── # Utility functions (all stderr output) @@ -49,7 +51,7 @@ ensure_brew() { } # ──────────────────────────────────────────── -# Detect OS +# Detect OS / Architecture # ──────────────────────────────────────────── OS="$(uname -s)" ARCH="$(uname -m)" @@ -68,6 +70,41 @@ for cmd in curl; do command -v "$cmd" &>/dev/null || error "'$cmd' command is required." done +# Fallback: use python3 if jq is not available +if command -v jq &>/dev/null; then + _json_latest_tag() { + jq -r '[.[] | select(.prerelease==false)] | .[0].tag_name' + } +else + command -v python3 &>/dev/null || error "'jq' or 'python3' is required." + _json_latest_tag() { + python3 -c " +import sys, json +data = json.load(sys.stdin) +for r in data: + if not r.get('prerelease'): + print(r['tag_name']) + break +" + } +fi + +# ──────────────────────────────────────────── +# Get latest stable release tag +# ──────────────────────────────────────────── +get_latest_tag() { + info "Fetching latest release from GitHub API..." + + local releases + releases="$(curl -fsSL "${API_URL}")" || error "Failed to call GitHub API." + + local tag + tag="$(echo "$releases" | _json_latest_tag)" + + [[ -z "$tag" || "$tag" == "null" ]] && error "Could not find latest release tag." + echo "$tag" +} + # ──────────────────────────────────────────── # Step 1: Install dua # ──────────────────────────────────────────── @@ -84,39 +121,83 @@ else success "dua installed via Homebrew" ;; Linux) - # Try package managers first, fall back to curl installer + # Try package managers first + DUA_INSTALLED=false + if command -v apt-get &>/dev/null; then - # dua-cli is available in newer Ubuntu/Debian repos - info "Installing via apt-get..." + info "Trying apt-get..." sudo apt-get update -y 2>/dev/null || true if sudo apt-get install -y dua-cli 2>/dev/null; then + DUA_INSTALLED=true success "dua installed via apt-get" - else - info "dua-cli not in apt repos. Using curl installer..." - curl -LSfs https://raw.githubusercontent.com/Byron/dua-cli/master/ci/install.sh | - sh -s -- --git Byron/dua-cli --crate dua --target x86_64-unknown-linux-musl - success "dua installed via curl installer" fi - elif command -v dnf &>/dev/null; then - sudo dnf install -y dua-cli - success "dua installed via dnf" - elif command -v pacman &>/dev/null; then - sudo pacman -S --noconfirm dua-cli - success "dua installed via pacman" - elif command -v zypper &>/dev/null; then - sudo zypper -n install dua-cli - success "dua installed via zypper" - else - info "No supported package manager found. Using curl installer..." + fi + + if [ "$DUA_INSTALLED" = "false" ] && command -v dnf &>/dev/null; then + info "Trying dnf..." + if sudo dnf install -y dua-cli 2>/dev/null; then + DUA_INSTALLED=true + success "dua installed via dnf" + fi + fi + + if [ "$DUA_INSTALLED" = "false" ] && command -v pacman &>/dev/null; then + info "Trying pacman..." + if sudo pacman -S --noconfirm dua-cli 2>/dev/null; then + DUA_INSTALLED=true + success "dua installed via pacman" + fi + fi + + if [ "$DUA_INSTALLED" = "false" ] && command -v zypper &>/dev/null; then + info "Trying zypper..." + if sudo zypper -n install dua-cli 2>/dev/null; then + DUA_INSTALLED=true + success "dua installed via zypper" + fi + fi + + # Fallback: download binary from GitHub releases + if [ "$DUA_INSTALLED" = "false" ]; then + info "No package manager had dua-cli. Downloading from GitHub releases..." + # Determine target case "$ARCH" in x86_64 | amd64) TARGET="x86_64-unknown-linux-musl" ;; aarch64 | arm64) TARGET="aarch64-unknown-linux-musl" ;; *) error "Unsupported Linux architecture: $ARCH" ;; esac - curl -LSfs https://raw.githubusercontent.com/Byron/dua-cli/master/ci/install.sh | - sh -s -- --git Byron/dua-cli --crate dua --target "$TARGET" - success "dua installed via curl installer" + + TAG="$(get_latest_tag)" + VERSION="${TAG#v}" + FILENAME="dua-${VERSION}-${TARGET}.tar.gz" + DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${TAG}/${FILENAME}" + + info "Downloading: $DOWNLOAD_URL" + TMPDIR="$(mktemp -d)" + trap '[[ -d "${TMPDIR:-}" ]] && rm -rf "$TMPDIR"' EXIT + + curl -fsSL --progress-bar -o "${TMPDIR}/${FILENAME}" "$DOWNLOAD_URL" || + error "Download failed: $DOWNLOAD_URL" + + info "Extracting..." + tar -xzf "${TMPDIR}/${FILENAME}" -C "$TMPDIR" + + # Find the dua binary + DUA_BIN="$(find "$TMPDIR" -maxdepth 3 -type f -name "dua" | head -1)" + [[ -z "$DUA_BIN" ]] && error "Cannot find 'dua' binary in the archive." + + mkdir -p "$INSTALL_DIR" + cp "$DUA_BIN" "${INSTALL_DIR}/dua" + chmod +x "${INSTALL_DIR}/dua" + success "dua installed to ${INSTALL_DIR}/dua" + + # PATH instructions + if ! echo ":${PATH}:" | grep -q ":${INSTALL_DIR}:"; then + warn "'${INSTALL_DIR}' is not in your PATH." + warn "Add this line to your ~/.bashrc or ~/.zshrc:" + warn " export PATH=\"\$HOME/.local/bin:\$PATH\"" + fi fi ;; esac