Files
install-sh/install-dua.sh
T

239 lines
8.9 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"
FALLBACK_TAG="v2.46.0"
2026-05-13 22:36:03 +09:00
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
# 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.
2026-05-13 22:36:03 +09:00
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'
2026-05-13 22:36:03 +09:00
}
elif command -v python3 &>/dev/null; then
2026-05-13 22:36:03 +09:00
_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)
2026-05-13 22:36:03 +09:00
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/"$//'
}
2026-05-13 22:36:03 +09:00
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
2026-05-13 22:36:03 +09:00
local tag
tag="$(get_latest_tag)"
local filename="dua-${tag}-${target}.tar.gz"
local download_url="https://github.com/${REPO}/releases/download/${tag}/${filename}"
local fallback_filename="dua-${FALLBACK_TAG}-${target}.tar.gz"
local fallback_url="https://github.com/${REPO}/releases/download/${FALLBACK_TAG}/${fallback_filename}"
2026-05-13 22:36:03 +09:00
info "Downloading: $download_url"
TMPDIR="$(mktemp -d)"
trap '[[ -d "${TMPDIR:-}" ]] && rm -rf "$TMPDIR"' EXIT
if ! curl -fsSL --progress-bar -o "${TMPDIR}/${filename}" "$download_url"; then
if [[ "$tag" != "$FALLBACK_TAG" ]]; then
warn "Primary download failed. Retrying with fallback tag (${FALLBACK_TAG}): $fallback_url"
curl -fsSL --progress-bar -o "${TMPDIR}/${filename}" "$fallback_url" ||
error "Download failed: $fallback_url"
else
error "Download failed: $download_url"
fi
fi
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
2026-05-13 22:36:03 +09:00
}
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). Updating/checking latest version..."
2026-05-13 22:29:18 +09:00
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
2026-05-13 22:29:18 +09:00
# 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"