#!/usr/bin/env bash # shellcheck disable=SC2086 # Installs dua (Disk Usage Analyzer) — https://github.com/byron/dua-cli # # Supported platforms: # macOS : Apple Silicon (arm64), Intel (x86_64) — via Homebrew # Linux : x86_64, aarch64 — via package manager or direct download # # Prerequisites: # - macOS: Homebrew (brew) — auto-installed if missing # - Linux: curl — for direct download fallback set -euo pipefail # ──────────────────────────────────────────── # Constants # ──────────────────────────────────────────── REPO="Byron/dua-cli" API_URL="https://api.github.com/repos/${REPO}/releases" FALLBACK_TAG="v2.46.0" INSTALL_DIR="${HOME}/.local/bin" # ──────────────────────────────────────────── # Utility functions (all stderr output) # ──────────────────────────────────────────── info() { echo "[INFO] $*" >&2; } warn() { echo "[WARN] $*" >&2; } error() { echo "[ERROR] $*" >&2 exit 1 } success() { echo "[OK] $*" >&2; } # ──────────────────────────────────────────── # Ensure Homebrew is available on macOS # ──────────────────────────────────────────── ensure_brew() { if command -v brew &>/dev/null; then return 0 fi info "Homebrew is not installed. Installing Homebrew..." /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" if [ -d /opt/homebrew/bin ]; then export PATH="/opt/homebrew/bin:$PATH" fi if [ -d /usr/local/bin ]; then export PATH="/usr/local/bin:$PATH" fi command -v brew &>/dev/null || error "Homebrew installation failed. Install it manually: https://brew.sh/" success "Homebrew installed" } # ──────────────────────────────────────────── # Detect OS / Architecture # ──────────────────────────────────────────── OS="$(uname -s)" ARCH="$(uname -m)" case "$OS" in Darwin | Linux) ;; *) error "Unsupported OS: $OS. This script only supports macOS and Linux." ;; esac # ──────────────────────────────────────────── # Check required commands # ──────────────────────────────────────────── for cmd in curl; do command -v "$cmd" &>/dev/null || error "'$cmd' command is required." done # Fallback: use python3 if jq is not available # Note: Byron/dua-cli publishes library tags like 'dua-core-v5.0.1' without binary assets. # Filter only CLI releases whose tag starts with 'v' and contain downloadable assets. if command -v jq &>/dev/null; then _json_latest_tag() { jq -r '[.[] | select(.prerelease==false and (.tag_name | startswith("v")) and (.assets | length > 0))] | .[0].tag_name' } elif command -v python3 &>/dev/null; then _json_latest_tag() { python3 -c " import sys, json data = json.load(sys.stdin) for r in data: tag = r.get('tag_name', '') if not r.get('prerelease') and tag.startswith('v') and r.get('assets'): print(tag) break " } else _json_latest_tag() { grep -oE '"tag_name":[[:space:]]*"v[0-9]+\.[0-9]+\.[0-9]+"' | head -1 | sed -E 's/.*"v/v/; s/"$//' } fi # ──────────────────────────────────────────── # Get latest stable release tag # ──────────────────────────────────────────── get_latest_tag() { info "Fetching latest release from GitHub API..." local releases="" releases="$(curl -fsSL -H "User-Agent: install-dua-sh" "${API_URL}" 2>/dev/null)" || true local tag="" if [[ -n "$releases" ]]; then tag="$(echo "$releases" | _json_latest_tag 2>/dev/null || true)" fi if [[ -z "$tag" || "$tag" == "null" ]]; then warn "Could not fetch latest release tag from GitHub API. Using fallback tag: ${FALLBACK_TAG}" tag="${FALLBACK_TAG}" fi echo "$tag" } # ──────────────────────────────────────────── # Install binary from GitHub releases # ──────────────────────────────────────────── install_from_github() { local target="" case "$OS" in Darwin) case "$ARCH" in x86_64 | amd64) target="x86_64-apple-darwin" ;; aarch64 | arm64) target="aarch64-apple-darwin" ;; *) error "Unsupported macOS architecture: $ARCH" ;; esac ;; Linux) case "$ARCH" in x86_64 | amd64) target="x86_64-unknown-linux-musl" ;; aarch64 | arm64) target="aarch64-unknown-linux-musl" ;; armv7l | armhf) target="arm-unknown-linux-gnueabihf" ;; riscv64) target="riscv64gc-unknown-linux-gnu" ;; *) error "Unsupported Linux architecture: $ARCH" ;; esac ;; esac local tag tag="$(get_latest_tag)" local filename="dua-${tag}-${target}.tar.gz" local 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 local dua_bin 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" export PATH="${INSTALL_DIR}:${PATH}" success "dua ${tag} installed to ${INSTALL_DIR}/dua" # PATH instructions if ! grep -qs "${INSTALL_DIR}" "${HOME}/.bashrc" "${HOME}/.zshrc" 2>/dev/null; then if [[ ":$PATH:" != *":${INSTALL_DIR}:"* ]]; then warn "'${INSTALL_DIR}' is not in your PATH." fi info "Ensure '${INSTALL_DIR}' is in your PATH (e.g. ~/.bashrc or ~/.zshrc):" info " export PATH=\"\$HOME/.local/bin:\$PATH\"" fi } # ──────────────────────────────────────────── # Step 1: Install dua # ──────────────────────────────────────────── info "Installing dua (Disk Usage Analyzer)..." if command -v dua &>/dev/null; then CURRENT_VER="$(dua --version 2>/dev/null || echo 'unknown')" info "dua is already installed ($CURRENT_VER). Updating/checking latest version..." fi case "$OS" in Darwin) if command -v brew &>/dev/null && brew install dua-cli 2>/dev/null; then success "dua installed via Homebrew" else info "Installing dua from GitHub releases..." install_from_github fi ;; Linux) info "Installing dua from GitHub releases..." install_from_github ;; esac # Verify dua is available if command -v dua &>/dev/null; then success "dua is available: $(command -v dua)" else warn "dua was not found in PATH after installation. You may need to open a new terminal." fi # ──────────────────────────────────────────── # Step 2: Post-install notes # ──────────────────────────────────────────── info "" info "==================================================" success "dua installation complete!" info "==================================================" info "" info "Getting started:" info " dua - Show disk usage of current directory" info " dua /path/to/dir - Show disk usage of specific directory" info " dua i - Interactive mode (explore & delete)" info " dua --help - Show help" info "" info "Interactive mode shortcuts:" info " Arrow keys / h/j/k/l - Navigate" info " ? - Show help" info " d - Delete selected entry" info " q / Esc - Quit"