#!/bin/sh # Installs the saboura command line tool on macOS and Linux. # # curl -fsSL https://saboura.site/install.sh | sh # # Settings, all optional: # SABOURA_INSTALL_DIR where to put the tool (default: $HOME/.local/bin) # SABOURA_VERSION install this version, such as 1.2.3, instead of the latest # SABOURA_BASE_URL where releases live (default: https://app.saboura.site) # # The tool is checked against its published SHA-256 before it is installed. This script never # uses sudo. Everything sits in main(), called on the last line, so a cut off download runs # nothing. main() { set -u base_url="${SABOURA_BASE_URL:-https://app.saboura.site}" base_url="${base_url%/}" install_dir="${SABOURA_INSTALL_DIR:-${HOME:-}/.local/bin}" pinned="${SABOURA_VERSION:-}" pinned="${pinned#v}" if [ -z "${SABOURA_INSTALL_DIR:-}" ] && [ -z "${HOME:-}" ]; then fail "HOME is not set. Set SABOURA_INSTALL_DIR to the folder to install into." fi # fail inside $(...) only ends the subshell, so each one is followed by "|| exit 1". os=$(detect_os) || exit 1 arch=$(detect_arch "$os") || exit 1 need_downloader need_hasher tmp_dir=$(mktemp -d 2>/dev/null || mktemp -d -t saboura) [ -n "$tmp_dir" ] && [ -d "$tmp_dir" ] || fail "Could not make a temporary folder." trap 'rm -rf "$tmp_dir"' EXIT trap 'rm -rf "$tmp_dir"; exit 130' INT TERM if [ -n "$pinned" ]; then valid_version "$pinned" || fail "SABOURA_VERSION must look like 1.2.3, not '$pinned'." version="$pinned" sums="$tmp_dir/SHA256SUMS" download "$base_url/cli/$version/SHA256SUMS" "$sums" || fail "Version $version was not found at $base_url." line=$(sums_entry "$sums" "$os" "$arch") [ -n "$line" ] || fail "Version $version has no build for $os $arch." expected=${line%% *} name=${line##* } name=${name#\*} else latest="$tmp_dir/latest.json" download "$base_url/cli/latest.json" "$latest" || fail "Could not read the latest version from $base_url/cli/latest.json." flat=$(flatten_json "$latest") version=$(json_field "$flat" version) valid_version "$version" || fail "The release list from $base_url is not valid." entry=$(latest_entry "$flat" "$os" "$arch") [ -n "$entry" ] || fail "There is no saboura build for $os $arch yet." name=$(json_field "$entry" name) expected=$(json_field "$entry" sha256) fi valid_name "$name" || fail "The release list names a file that is not valid: '$name'." valid_sha256 "$expected" || fail "The release list has no valid SHA-256 for $name." expected=$(printf '%s' "$expected" | tr 'A-F' 'a-f') say "Downloading saboura $version for $os $arch" binary="$tmp_dir/$name" download "$base_url/cli/$version/$name" "$binary" || fail "Could not download $name." actual=$(sha256_of "$binary") if [ "$actual" != "$expected" ]; then fail "The download does not match its SHA-256, so it was not installed. expected $expected got $actual" fi mkdir -p "$install_dir" 2>/dev/null || fail "Could not make $install_dir. Set SABOURA_INSTALL_DIR to a folder you can write to." [ -w "$install_dir" ] || fail "Cannot write to $install_dir. Set SABOURA_INSTALL_DIR to a folder you can write to." # Copy next to the target first, then rename, so a working tool is never half replaced. staged="$install_dir/.saboura.$$" if ! { cp "$binary" "$staged" && chmod 755 "$staged" && mv -f "$staged" "$install_dir/saboura"; }; then rm -f "$staged" fail "Could not install into $install_dir." fi say "Installed saboura $version to $install_dir/saboura" case ":${PATH:-}:" in *":$install_dir:"*) ;; *) path_hint "$install_dir" ;; esac say "Next: saboura login" } say() { printf '%s\n' "$*" } fail() { printf 'saboura install: %s\n' "$*" >&2 exit 1 } # Prints macos or linux, or stops on any other system. detect_os() { kernel=$(uname -s 2>/dev/null || echo unknown) case "$kernel" in Darwin) echo macos ;; Linux) echo linux ;; MINGW* | MSYS* | CYGWIN* | Windows_NT) fail "On Windows, run this in PowerShell: irm https://saboura.site/install.ps1 | iex" ;; *) fail "saboura does not support $kernel yet. It runs on macOS, Linux and Windows." ;; esac } # Prints x64 or arm64, or stops on any other processor. detect_arch() { machine=$(uname -m 2>/dev/null || echo unknown) case "$machine" in x86_64 | amd64) # A shell running under Rosetta on Apple silicon still gets the native build. if [ "$1" = macos ] && [ "$(sysctl -n hw.optional.arm64 2>/dev/null)" = 1 ]; then echo arm64 else echo x64 fi ;; arm64 | aarch64) echo arm64 ;; *) fail "saboura does not support the $machine processor yet. It needs x64 or arm64." ;; esac } need_downloader() { if command -v curl >/dev/null 2>&1; then downloader=curl elif command -v wget >/dev/null 2>&1; then downloader=wget else fail "Needs curl or wget to download." fi } need_hasher() { if command -v sha256sum >/dev/null 2>&1; then hasher=sha256sum elif command -v shasum >/dev/null 2>&1; then hasher=shasum else fail "Needs sha256sum or shasum to check the download." fi } # download URL FILE: saves URL to FILE, failing on any HTTP error. download() { if [ "$downloader" = curl ]; then curl -fsSL --retry 2 -o "$2" "$1" else wget -q -O "$2" "$1" fi } sha256_of() { if [ "$hasher" = sha256sum ]; then sha256sum "$1" | cut -d ' ' -f 1 else shasum -a 256 "$1" | cut -d ' ' -f 1 fi } # The JSON with whitespace removed and one object per line, so each file entry is one line. flatten_json() { tr -d ' \t\r\n' <"$1" | tr '}' '\n' } # json_field TEXT KEY: the string value of KEY in TEXT, or the number for "size". json_field() { printf '%s\n' "$1" | sed -n "s/.*\"$2\":\"\{0,1\}\([^\",]*\)\"\{0,1\}.*/\1/p" | head -n 1 } # The flattened latest.json line for this system. latest_entry() { printf '%s\n' "$1" | grep "\"os\":\"$2\"" | grep "\"arch\":\"$3\"" | head -n 1 } # sums_entry FILE OS ARCH: "HASH NAME" for the file whose name carries the os and arch words. sums_entry() { tr -s ' \t' ' ' <"$1" | grep -i -E "^[0-9a-f]{64} \*?([^ ]*[-_.])?$2[-_.]([^ ]*[-_.])?$3([-_.][^ ]*)?$" | head -n 1 } valid_version() { printf '%s\n' "$1" | grep -E -q '^[0-9]+\.[0-9]+\.[0-9]+([-+][0-9A-Za-z.-]+)?$' } valid_name() { printf '%s\n' "$1" | grep -E -q '^[A-Za-z0-9][A-Za-z0-9._-]*$' } valid_sha256() { printf '%s\n' "$1" | grep -E -q '^[0-9A-Fa-f]{64}$' } path_hint() { # The ~ is text for the person to read, not a path this script opens. # shellcheck disable=SC2088 case "${SHELL:-}" in */zsh) profile="~/.zshrc" ;; */bash) profile="~/.bashrc" ;; */fish) profile="" ;; *) profile="~/.profile" ;; esac say "" say "$1 is not on your PATH yet." if [ -z "$profile" ]; then say "Run this once to add it: fish_add_path $1" else say "Add this line to $profile, then open a new terminal:" say " export PATH=\"$1:\$PATH\"" fi say "" } main "$@"