#!/usr/bin/env bash # niotio CLI installer (served by the Niotio app). # # curl -fsSL https://niotio.dk/cli/install.sh | sh # # Downloads the prebuilt `niotio` binary from the Niotio app (the same host that # serves this script), verifies its sha256 against the app's manifest, and # installs it. No GitHub token and no Go toolchain are required — the # stigsen/nps repo is private, so a direct GitHub install would need auth. # # Options (env): # NPS_CLI_BASE_URL app base URL (default: https://niotio.dk) # NPS_CLI_BINDIR install dir (default: /usr/local/bin, else ~/.local/bin) # NPS_CLI_NO_CONFIG set to 1 to skip pre-setting the API URL in the CLI config set -eu # Enable pipefail when the shell supports it (bash/ksh); dash (sh) does not. if (set -o pipefail) 2>/dev/null; then set -o pipefail; fi BASE_URL="${NPS_CLI_BASE_URL:-https://niotio.dk}" BASE_URL="${BASE_URL%/}" BIN="niotio" os="$(uname -s | tr '[:upper:]' '[:lower:]')" arch="$(uname -m)" case "$arch" in x86_64|amd64) arch="amd64" ;; aarch64|arm64) arch="arm64" ;; *) echo "unsupported architecture: $arch" >&2; exit 1 ;; esac case "$os" in linux|darwin) ;; *) echo "unsupported OS: $os" >&2; exit 1 ;; esac if [ -n "${NPS_CLI_BINDIR:-}" ]; then bindir="$NPS_CLI_BINDIR" elif [ -w /usr/local/bin ]; then bindir="/usr/local/bin" else bindir="${HOME}/.local/bin" fi mkdir -p "$bindir" tmp="$(mktemp -d)" trap 'rm -rf "$tmp"' EXIT asset="niotio_${os}_${arch}.tar.gz" legacy_asset="nps_${os}_${arch}.tar.gz" echo "niotio installer — ${os}/${arch}, source ${BASE_URL}" if ! manifest="$(curl -fsSL "${BASE_URL}/cli/manifest.json" 2>/dev/null)"; then echo "Could not fetch ${BASE_URL}/cli/manifest.json" >&2 echo "If the app is not at that URL, set NPS_CLI_BASE_URL and retry." >&2 exit 1 fi # Pull this artifact's sha256 (and the version) out of the manifest. if command -v python3 >/dev/null 2>&1; then want="$(printf '%s' "$manifest" | python3 -c 'import json,sys d=json.load(sys.stdin) print(next((a.get("sha256","") for a in d.get("artifacts",[]) if a.get("name")=="'"$asset"'"), ""))' 2>/dev/null)" ver="$(printf '%s' "$manifest" | python3 -c 'import json,sys; print(json.load(sys.stdin).get("version",""))' 2>/dev/null)" else want="$(printf '%s' "$manifest" | tr '{' '\n' | grep "\"name\":[[:space:]]*\"${asset}\"" | sed -n 's/.*"sha256"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -1)" ver="$(printf '%s' "$manifest" | sed -n 's/.*"version"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -1)" fi if [ -z "$want" ]; then if command -v python3 >/dev/null 2>&1; then want="$(printf '%s' "$manifest" | python3 -c 'import json,sys d=json.load(sys.stdin) print(next((a.get(\"sha256\",\"\") for a in d.get(\"artifacts\",[]) if a.get(\"name\")==\"'"$legacy_asset"'\"), \"\"))' 2>/dev/null)" else want="$(printf '%s' "$manifest" | tr '{' '\n' | grep "\"name\":[[:space:]]*\"${legacy_asset}\"" | sed -n 's/.*"sha256"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -1)" fi if [ -n "$want" ]; then asset="$legacy_asset" else echo "No prebuilt ${asset} in the manifest (version ${ver:-unknown})." >&2 exit 1 fi fi echo "Downloading ${asset} (${ver:-unknown})…" if ! curl -fsSL -o "$tmp/${asset}" "${BASE_URL}/cli/download/${asset}"; then echo "Download failed: ${BASE_URL}/cli/download/${asset}" >&2 exit 1 fi if command -v sha256sum >/dev/null 2>&1; then got="$(sha256sum "$tmp/${asset}" | awk '{print $1}')"; else got="$(shasum -a 256 "$tmp/${asset}" | awk '{print $1}')"; fi if [ "$want" != "$got" ]; then echo "Checksum mismatch for ${asset} (expected ${want}, got ${got})" >&2 exit 1 fi echo "Checksum verified." tar -xzf "$tmp/${asset}" -C "$tmp" install -m 0755 "$tmp/${BIN}" "${bindir}/${BIN}" if [ "${NPS_CLI_NO_CONFIG:-0}" != "1" ]; then "${bindir}/${BIN}" config set-url "$BASE_URL" >/dev/null 2>&1 || true fi echo "Installed ${BIN} -> ${bindir}/${BIN}" case ":$PATH:" in *":${bindir}:"*) ;; *) echo "Note: add ${bindir} to your PATH." ;; esac "${bindir}/${BIN}" version || true