Files
install-sh/install-dua.sh
T

231 lines
8.1 KiB
Bash
Raw Normal View History

2026-05-13 22:29:18 +09:00
#!/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
2026-05-13 22:36:03 +09:00
# Linux : x86_64, aarch64 — via package manager or direct download
2026-05-13 22:29:18 +09:00
#
# Prerequisites:
# - macOS: Homebrew (brew) — auto-installed if missing
2026-05-13 22:36:03 +09:00
# - Linux: curl — for direct download fallback
2026-05-13 22:29:18 +09:00
set -euo pipefail
# ────────────────────────────────────────────
# Constants
# ────────────────────────────────────────────
REPO="Byron/dua-cli"
2026-05-13 22:36:03 +09:00
API_URL="https://api.github.com/repos/${REPO}/releases"
INSTALL_DIR="${HOME}/.local/bin"
2026-05-13 22:29:18 +09:00
# ────────────────────────────────────────────
# 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"
}
# ────────────────────────────────────────────
2026-05-13 22:36:03 +09:00
# Detect OS / Architecture
2026-05-13 22:29:18 +09:00
# ────────────────────────────────────────────
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
2026-05-13 22:36:03 +09:00
# 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"
}
2026-05-13 22:29:18 +09:00
# ────────────────────────────────────────────
# 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)
2026-05-13 22:36:03 +09:00
# Try package managers first
DUA_INSTALLED=false
2026-05-13 22:29:18 +09:00
if command -v apt-get &>/dev/null; then
2026-05-13 22:36:03 +09:00
info "Trying apt-get..."
2026-05-13 22:29:18 +09:00
sudo apt-get update -y 2>/dev/null || true
if sudo apt-get install -y dua-cli 2>/dev/null; then
2026-05-13 22:36:03 +09:00
DUA_INSTALLED=true
2026-05-13 22:29:18 +09:00
success "dua installed via apt-get"
fi
2026-05-13 22:36:03 +09:00
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..."
2026-05-13 22:29:18 +09:00
# 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
2026-05-13 22:36:03 +09:00
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
2026-05-13 22:29:18 +09:00
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"