#!/bin/sh # # Installs the saasie CLI. # # curl -fsSL https://saasie.io/install.sh | sh # # Options, as environment variables: # # SAASIE_INSTALL_DIR where to put it (default: the first writable of # /usr/local/bin, ~/.local/bin) # SAASIE_BASE_URL where to fetch it from (default: https://saasie.io) # # POSIX sh, no dependencies beyond curl (or wget) and a sha256 tool, because # this runs on machines that have nothing else on them yet. # set -eu BASE_URL="${SAASIE_BASE_URL:-https://saasie.io}" DL_URL="$BASE_URL/dl" say() { printf '%s\n' "$*" >&2; } die() { printf 'install.sh: %s\n' "$*" >&2; exit 1; } # --- which build ------------------------------------------------------------ os="$(uname -s)" case "$os" in Linux) os=linux ;; Darwin) os=darwin ;; *) die "unsupported operating system: $os Windows: download $DL_URL/saasie-windows-x64.exe by hand and put it on your PATH." ;; esac arch="$(uname -m)" case "$arch" in x86_64 | amd64) arch=x64 ;; arm64 | aarch64) arch=arm64 ;; *) die "unsupported architecture: $arch" ;; esac asset="saasie-$os-$arch" # --- where it goes ---------------------------------------------------------- # Chosen before the download, so a permissions problem is reported before # spending a minute on 40 MB. if [ -n "${SAASIE_INSTALL_DIR:-}" ]; then install_dir="$SAASIE_INSTALL_DIR" mkdir -p "$install_dir" || die "cannot create $install_dir" elif [ -w /usr/local/bin ]; then install_dir=/usr/local/bin else install_dir="$HOME/.local/bin" mkdir -p "$install_dir" || die "cannot create $install_dir" fi [ -w "$install_dir" ] || die "cannot write to $install_dir Pick somewhere else with: SAASIE_INSTALL_DIR=~/.local/bin" # --- fetch ------------------------------------------------------------------ # The server stores the binaries gzipped and serves them under their plain # names. `curl --compressed` unwraps that here; plain wget does not advertise # gzip, so nginx expands them at its end instead. Either way what lands on # disk is the executable. if command -v curl >/dev/null 2>&1; then fetch() { curl -fsSL --compressed "$1" -o "$2"; } elif command -v wget >/dev/null 2>&1; then fetch() { wget -q -O "$2" "$1"; } else die "needs curl or wget" fi tmp="$(mktemp -d)" trap 'rm -rf "$tmp"' EXIT INT TERM fetch "$DL_URL/VERSION" "$tmp/VERSION" || die "cannot reach $DL_URL — is $BASE_URL up?" version="$(cat "$tmp/VERSION")" say "downloading saasie $version for $os-$arch" fetch "$DL_URL/$asset" "$tmp/saasie" || die "download failed: $DL_URL/$asset" # --- verify ----------------------------------------------------------------- if command -v sha256sum >/dev/null 2>&1; then sum="$(sha256sum "$tmp/saasie" | cut -d' ' -f1)" elif command -v shasum >/dev/null 2>&1; then sum="$(shasum -a 256 "$tmp/saasie" | cut -d' ' -f1)" else sum="" fi if [ -n "$sum" ]; then fetch "$DL_URL/SHA256SUMS" "$tmp/SHA256SUMS" || die "cannot fetch checksums" expected="$(grep " $asset\$" "$tmp/SHA256SUMS" | cut -d' ' -f1)" [ -n "$expected" ] || die "no checksum published for $asset" [ "$sum" = "$expected" ] || die "checksum mismatch for $asset — refusing to install expected $expected got $sum" else say "! no sha256 tool found; skipping checksum verification" fi # --- install ---------------------------------------------------------------- chmod +x "$tmp/saasie" # Move into place in one step: replacing a binary that is currently running # fails on some systems if it is written to in place. mv -f "$tmp/saasie" "$install_dir/saasie" 2>/dev/null || die "cannot install into $install_dir" say "" say "saasie $version installed to $install_dir/saasie" case ":$PATH:" in *":$install_dir:"*) say "" say "Next: saasie docs — what the platform asks of your repo" say " saasie otp " ;; *) say "" say "! $install_dir is not on your PATH. Add it:" say " export PATH=\"$install_dir:\$PATH\"" ;; esac