#!/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 curl installer # # Prerequisites: # - macOS: Homebrew (brew) — auto-installed if missing # - Linux: curl — for curl-based installer set -euo pipefail # ──────────────────────────────────────────── # Constants # ──────────────────────────────────────────── REPO="Byron/dua-cli" # ──────────────────────────────────────────── # 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 # ──────────────────────────────────────────── 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 # ──────────────────────────────────────────── # 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). Skipping installation." else case "$OS" in Darwin) ensure_brew brew install dua-cli success "dua installed via Homebrew" ;; Linux) # Try package managers first, fall back to curl installer if command -v apt-get &>/dev/null; then # dua-cli is available in newer Ubuntu/Debian repos info "Installing via apt-get..." sudo apt-get update -y 2>/dev/null || true if sudo apt-get install -y dua-cli 2>/dev/null; then 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..." # 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" fi ;; esac fi # 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"