#!/bin/sh # Installs the Clair agent (`clair`) on macOS or Linux and runs it as a login service. # # curl -fsSL https://claircode.app/install.sh | sh # curl -fsSL https://claircode.app/install.sh | sh -s -- v0.1.0 # a specific version # # Downloads the binary for this system from https://claircode.app/releases, checks it # against that release's SHA256SUMS.txt, puts it in ~/.local/bin, and runs # `clair service install`: a systemd user unit on Linux, a LaunchAgent on macOS. Nothing # runs as root. Running it again upgrades in place and restarts the service. # # Environment: # CLAIR_INSTALL_DIR where the binary goes (default ~/.local/bin) # CLAIR_NO_SERVICE=1 install the binary only; run `clair service install` yourself # CLAIR_RELEASES_URL where releases are read from (default https://claircode.app/releases) # # Windows: irm https://claircode.app/install.ps1 | iex # # Everything is inside main, called on the last line, so a download cut short runs nothing. set -eu RELEASES_URL="${CLAIR_RELEASES_URL:-https://claircode.app/releases}" INSTALL_DIR="${CLAIR_INSTALL_DIR:-$HOME/.local/bin}" tmp="" say() { printf '%s\n' "$*"; } fail() { printf 'clair install: %s\n' "$*" >&2 exit 1 } cleanup() { if [ -n "$tmp" ]; then rm -rf "$tmp"; fi } # fetch URL [FILE]: the body of URL into FILE, or to stdout. HTTPS only, except for a test # server on this machine. fetch() { case "$1" in http://127.0.0.1[:/]* | http://localhost[:/]*) proto='=http,https' ;; *) proto='=https' ;; esac if command -v curl >/dev/null 2>&1; then curl -fsSL --proto "$proto" --tlsv1.2 -o "${2:--}" "$1" elif command -v wget >/dev/null 2>&1; then # GNU wget can refuse a redirect to plain HTTP; BusyBox's has no such option. https_only= if [ "$proto" = '=https' ] && wget --help 2>&1 | grep -q -- --https-only; then https_only=--https-only fi # shellcheck disable=SC2086 # empty, or one option wget -q $https_only -O "${2:--}" "$1" else fail "needs curl or wget to download the agent." fi } sha256() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d ' ' -f 1 else shasum -a 256 "$1" | cut -d ' ' -f 1 fi } main() { trap cleanup EXIT trap 'exit 130' INT TERM case "$(uname -s)" in Linux) os=linux ;; Darwin) os=darwin ;; MINGW* | MSYS* | CYGWIN*) fail "on Windows, run this in PowerShell instead: irm https://claircode.app/install.ps1 | iex" ;; *) fail "there is no build for $(uname -s). The agent runs on macOS, Linux and Windows." ;; esac case "$(uname -m)" in x86_64 | amd64) arch=amd64 ;; arm64 | aarch64) arch=arm64 ;; *) fail "there is no build for $(uname -m). The agent is built for amd64 and arm64." ;; esac # A shell under Rosetta reports x86_64 on Apple silicon; install the native build. if [ "$os" = darwin ] && [ "$arch" = amd64 ] && [ "$(sysctl -n hw.optional.arm64 2>/dev/null || echo 0)" = 1 ]; then arch=arm64 fi command -v sha256sum >/dev/null 2>&1 || command -v shasum >/dev/null 2>&1 || fail "needs sha256sum or shasum to check the download." requested="${1:-}" version="$requested" if [ -z "$version" ]; then version="$(fetch "$RELEASES_URL/latest.txt")" || fail "couldn't read the latest version from $RELEASES_URL/latest.txt. Check your connection and try again." fi version="$(printf '%s' "$version" | tr -d '[:space:]')" case "$version" in v*) ;; *) version="v$version" ;; esac case "$version" in v[0-9]*) ;; *) fail "\"$version\" is not a version." ;; esac asset="clair-$version-$os-$arch" tmp="$(mktemp -d)" say "Downloading clair $version for $os/$arch" fetch "$RELEASES_URL/$version/$asset" "$tmp/clair" || fail "couldn't download $RELEASES_URL/$version/$asset." fetch "$RELEASES_URL/$version/SHA256SUMS.txt" "$tmp/SHA256SUMS" || fail "couldn't download $RELEASES_URL/$version/SHA256SUMS.txt." want="$(awk -v f="$asset" '$2 == f || $2 == "*" f { print $1 }' "$tmp/SHA256SUMS")" [ -n "$want" ] || fail "the checksums for $version have no line for $asset." got="$(sha256 "$tmp/clair")" [ "$got" = "$want" ] || fail "the download doesn't match its checksum (expected $want, got $got). Nothing was installed." chmod 755 "$tmp/clair" dest="$INSTALL_DIR/clair" mkdir -p "$INSTALL_DIR" # An installed service keeps running the old binary, so stop it before the swap; # `clair service install` below starts it again on the new one. if [ "$os" = linux ]; then unit="${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user/clair.service" else unit="$HOME/Library/LaunchAgents/app.claircode.clair.plist" fi if [ -f "$unit" ] && [ -x "$dest" ]; then "$dest" service stop /dev/null 2>&1 || true fi # Copied beside the target first, so the rename is atomic. cp "$tmp/clair" "$dest.new" mv -f "$dest.new" "$dest" say "Installed $("$dest" version /dev/null || echo "clair $version") at $dest" if [ "${CLAIR_NO_SERVICE:-0}" = 1 ]; then say "Skipped the service (CLAIR_NO_SERVICE=1). To start it: $dest service install" say "To pair your phone: clair pair" elif ! "$dest" service install