Files
install-sh/install-ctop.sh
T

218 lines
8.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# shellcheck disable=SC2086
# Downloads and installs the latest ctop (container terminal UI) from
# https://github.com/bcicen/ctop/releases
#
# Supported platforms:
# macOS : Intel (x86_64), Apple Silicon via Rosetta 2 (arm64)
# Linux : x86_64 (amd64), aarch64/arm64, arm, ppc64le
# Windows : Not supported — use install-ctop.ps1 (PowerShell)
set -euo pipefail
# ────────────────────────────────────────────
# Constants
# ────────────────────────────────────────────
REPO="bcicen/ctop"
API_URL="https://api.github.com/repos/${REPO}/releases"
FALLBACK_TAG="v0.7.7"
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; }
# ────────────────────────────────────────────
# Check required commands
# ────────────────────────────────────────────
for cmd in curl uname; do
command -v "$cmd" &>/dev/null || error "'$cmd' command is required."
done
# Fallback: use python3 or grep if jq is not available
if command -v jq &>/dev/null; then
_json_latest_tag() {
jq -r '[.[] | select(.prerelease==false 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:
if not r.get('prerelease') and r.get('assets'):
print(r['tag_name'])
break
"
}
else
_json_latest_tag() {
grep -oE '"tag_name":[[:space:]]*"[^"]+"' | head -1 | sed -E 's/.*"([^"]+)"$/\1/'
}
fi
# ────────────────────────────────────────────
# OS / Architecture detection
# ────────────────────────────────────────────
# Returns: "<os>-<arch>" matching ctop's GitHub release asset naming
detect_platform() {
local os arch
os="$(uname -s)"
arch="$(uname -m)"
case "$os" in
# ── macOS ──────────────────────────────────────────────
Darwin)
case "$arch" in
x86_64)
echo "darwin-amd64"
;;
arm64)
# ctop does not provide a native darwin-arm64 binary.
# Use the amd64 binary via Rosetta 2 (works on Apple Silicon).
if [[ "$(sysctl -n sysctl.proc_translated 2>/dev/null || echo 0)" == "1" ]] \
|| command -v arch &>/dev/null; then
warn "ctop has no native Apple Silicon build."
warn "Using amd64 binary via Rosetta 2."
echo "darwin-amd64"
else
error "No native Apple Silicon build available and Rosetta 2 is not installed." \
"Install Rosetta 2: softwareupdate --install-rosetta"
fi
;;
*)
error "Unsupported macOS architecture: $arch"
;;
esac
;;
# ── Linux ──────────────────────────────────────────────
Linux)
case "$arch" in
x86_64|amd64) echo "linux-amd64" ;;
aarch64|arm64) echo "linux-arm64" ;;
armv7l|armhf) echo "linux-arm" ;;
ppc64le) echo "linux-ppc64le" ;;
*)
error "Unsupported Linux architecture: $arch"
;;
esac
;;
# ── Windows (Git Bash / MSYS2 / Cygwin) ───────────────
MINGW*|MSYS*|CYGWIN*)
error "Windows is not supported by this script. Use install-ctop.ps1 instead."
;;
*)
error "Unsupported OS: $os (uname -a: $(uname -a))"
;;
esac
}
# ────────────────────────────────────────────
# Get latest stable release tag
# ────────────────────────────────────────────
get_latest_tag() {
info "Fetching latest release from GitHub API..."
local releases="" tag=""
releases="$(curl -fsSL -H "User-Agent: install-ctop-sh" --connect-timeout 10 "${API_URL}" 2>/dev/null)" || true
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"
}
# ────────────────────────────────────────────
# Build download URL
# ────────────────────────────────────────────
# ctop release assets follow the pattern: ctop-{version}-{os}-{arch}
# where {version} has the 'v' prefix stripped from the git tag.
build_download_url() {
local tag="$1"
local platform="$2" # e.g. darwin-amd64, linux-amd64
local version="${tag#v}" # Remove 'v' prefix (v0.7.7 → 0.7.7)
local filename="ctop-${version}-${platform}"
echo "https://github.com/${REPO}/releases/download/${tag}/${filename}"
}
# ────────────────────────────────────────────
# Download & install (raw binary — no archive extraction needed)
# ────────────────────────────────────────────
download_and_install() {
local url="$1"
local fallback_url="${2:-}"
local tmpdir
tmpdir="$(mktemp -d)"
trap '[[ -d "${tmpdir:-}" ]] && rm -rf "$tmpdir"' EXIT
local filename
filename="${url##*/}"
info "Downloading: $url"
if ! curl -fsSL --progress-bar -o "${tmpdir}/${filename}" "$url"; then
if [[ -n "$fallback_url" && "$fallback_url" != "$url" ]]; then
warn "Primary download failed. Retrying with fallback URL: $fallback_url"
curl -fsSL --progress-bar -o "${tmpdir}/${filename}" "$fallback_url" \
|| error "Download failed: $fallback_url"
else
error "Download failed: $url"
fi
fi
# ctop provides raw binaries (not archives), so skip extraction
local bin_src="${tmpdir}/${filename}"
# Create install directory
mkdir -p "$INSTALL_DIR"
local install_path="${INSTALL_DIR}/ctop"
info "Installing: ${install_path}"
cp "$bin_src" "$install_path"
chmod +x "$install_path"
success "Installation complete!"
info "Version check:"
"$install_path" -v || true
# 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
}
# ────────────────────────────────────────────
# Main
# ────────────────────────────────────────────
main() {
local platform tag url
platform="$(detect_platform)"
info "Detected platform: $platform"
tag="$(get_latest_tag)"
info "Latest release: $tag"
url="$(build_download_url "$tag" "$platform")"
local fallback_url
fallback_url="$(build_download_url "$FALLBACK_TAG" "$platform")"
download_and_install "$url" "$fallback_url"
}
main "$@"