111 lines
3.4 KiB
Bash
111 lines
3.4 KiB
Bash
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# ── Config ──────────────────────────────────────────────
|
|
REPO="TimothyYe/skm"
|
|
VERSION="${VERSION:-latest}" # set VERSION=v0.8.7 to pin
|
|
PREFIX="$HOME/.local/bin" # non-sudo install path
|
|
|
|
# ── Helpers ─────────────────────────────────────────────
|
|
need() { command -v "$1" >/dev/null 2>&1 || { echo "Missing dependency: $1"; exit 1; }; }
|
|
tmpdir() { mktemp -d 2>/dev/null || mktemp -d -t skm; }
|
|
cleanup() { [[ -n "${WORKDIR:-}" && -d "$WORKDIR" ]] && rm -rf "$WORKDIR"; }
|
|
trap cleanup EXIT
|
|
|
|
detect_platform() {
|
|
local uos uarch
|
|
uos="$(uname -s | tr '[:upper:]' '[:lower:]')"
|
|
uarch="$(uname -m)"
|
|
|
|
case "$uos" in
|
|
linux) OS="linux" ;;
|
|
darwin) OS="darwin" ;;
|
|
*) echo "Unsupported OS: $uos"; exit 1 ;;
|
|
esac
|
|
|
|
case "$uarch" in
|
|
x86_64|amd64) ARCH="amd64" ;;
|
|
aarch64|arm64) ARCH="arm64" ;;
|
|
i386|i686) ARCH="386" ;;
|
|
armv7l|armv7|armv6l|armv6|armv5l|arm) ARCH="arm" ;;
|
|
*) echo "Unsupported architecture: $uarch"; exit 1 ;;
|
|
esac
|
|
}
|
|
|
|
# ── Fixed function ──────────────────────────────────────
|
|
get_latest_tag() {
|
|
need curl
|
|
local json tag
|
|
json="$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest")"
|
|
tag="$(printf '%s\n' "$json" | sed -nE 's/.*"tag_name": *"([^"]+)".*/\1/p' | head -n1)"
|
|
if [ -z "$tag" ]; then
|
|
echo "❌ Failed to determine latest release tag from GitHub API" >&2
|
|
exit 1
|
|
fi
|
|
printf '%s\n' "$tag"
|
|
}
|
|
|
|
build_url() {
|
|
local tag="$1" os="$2" arch="$3" ver="${tag#v}"
|
|
echo "https://github.com/${REPO}/releases/download/${tag}/skm_${ver}_${os}_${arch}.tar.gz"
|
|
}
|
|
|
|
resolve_url() {
|
|
local tag="$1" os="$2" arch="$3"
|
|
local url alt
|
|
url="$(build_url "$tag" "$os" "$arch")"
|
|
if curl -fsI "$url" >/dev/null 2>&1; then
|
|
echo "$url"; return 0
|
|
fi
|
|
if [[ "$os" == "linux" && "$arch" == "arm64" ]]; then
|
|
alt="$(build_url "$tag" "$os" "arm")"
|
|
if curl -fsI "$alt" >/dev/null 2>&1; then
|
|
echo "$alt"; return 0
|
|
fi
|
|
fi
|
|
echo "No release asset found for ${os}/${arch} (tag ${tag})." >&2
|
|
exit 1
|
|
}
|
|
|
|
# ── Main ────────────────────────────────────────────────
|
|
echo "→ Detecting platform..."
|
|
detect_platform
|
|
echo " OS=${OS} ARCH=${ARCH}"
|
|
|
|
if [[ "$VERSION" == "latest" ]]; then
|
|
echo "→ Fetching latest SKM release..."
|
|
TAG="$(get_latest_tag)"
|
|
else
|
|
TAG="$VERSION"
|
|
fi
|
|
echo " Using release: $TAG"
|
|
|
|
URL="$(resolve_url "$TAG" "$OS" "$ARCH")"
|
|
echo "→ Downloading: $URL"
|
|
|
|
WORKDIR="$(tmpdir)"
|
|
cd "$WORKDIR"
|
|
FILENAME="${URL##*/}"
|
|
curl -fL --progress-bar -o "$FILENAME" "$URL"
|
|
|
|
echo "→ Extracting..."
|
|
tar -xzf "$FILENAME"
|
|
|
|
BIN="$(find . -maxdepth 2 -type f -name skm | head -n1 || true)"
|
|
[[ -n "$BIN" && -f "$BIN" ]] || { echo "Could not find skm binary"; exit 1; }
|
|
|
|
mkdir -p "$PREFIX"
|
|
mv "$BIN" "$PREFIX/skm"
|
|
chmod +x "$PREFIX/skm"
|
|
|
|
echo "→ Installed at $PREFIX/skm"
|
|
|
|
# Add to PATH if not already
|
|
if ! echo "$PATH" | grep -q "$HOME/.local/bin"; then
|
|
echo "⚠️ $HOME/.local/bin is not in your PATH."
|
|
echo " Add this to your shell config (e.g. ~/.bashrc or ~/.zshrc):"
|
|
echo " export PATH=\"\$HOME/.local/bin:\$PATH\""
|
|
fi
|
|
|
|
echo "✅ Done. Version: $($PREFIX/skm --version)"
|