#!/usr/bin/env bash # Installs sbxctl, the `sbx` command: # # curl -fsSL https://sbxctl.dev/install | bash # # It downloads the release for this machine from GitHub (github.com/alexjamesgodfrey/sbxctl), # checks it against the release's SHA256SUMS, puts it at ~/.sbx/bin/sbx (with `sbxctl` beside it) # and adds that directory to PATH in your shell's startup file. Then, at a terminal and with no # configuration yet, it starts `sbx config setup`, which asks for a teammate's configuration and # your sign-ins. It writes nothing into any coding agent. Running it again updates sbx. # # SBX_VERSION=v0.3.0 a given release instead of the latest # SBX_INSTALL=~/somewhere install into somewhere/bin instead of ~/.sbx/bin # SBX_NO_MODIFY_PATH=1 leave the shell's startup files alone # SBX_NO_SETUP=1 do not start `sbx config setup` set -euo pipefail repo=alexjamesgodfrey/sbxctl version=${SBX_VERSION:-latest} install_dir=${SBX_INSTALL:-$HOME/.sbx} bin_dir=$install_dir/bin fail() { printf 'sbx install: %s\n' "$*" >&2; exit 1; } say() { printf '%s\n' "$*" >&2; } case "$(uname -s)" in Darwin) os=darwin ;; Linux) os=linux ;; *) fail "sbxctl runs on macOS and Linux, not on $(uname -s)" ;; esac case "$(uname -m)" in arm64 | aarch64) arch=arm64 ;; x86_64 | amd64) arch=x64 ;; *) fail "no sbxctl for $(uname -m) processors" ;; esac # A shell under Rosetta says x86_64 on an Apple chip: the native one is the one to have. if [ "$os" = darwin ] && [ "$arch" = x64 ] && [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || true)" = 1 ]; then arch=arm64 fi command -v curl >/dev/null || fail "curl is needed" if command -v sha256sum >/dev/null; then sha() { sha256sum "$1" | cut -d' ' -f1; } elif command -v shasum >/dev/null; then sha() { shasum -a 256 "$1" | cut -d' ' -f1; } else fail "sha256sum or shasum is needed to check the download"; fi file=sbx-$os-$arch if [ "$version" = latest ]; then base=https://github.com/$repo/releases/latest/download else case "$version" in v*) ;; *) version=v$version ;; esac base=https://github.com/$repo/releases/download/$version fi tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT say "Downloading sbxctl ($version, $os-$arch)" curl -fL --progress-bar -o "$tmp/$file" "$base/$file" || fail "could not download $base/$file" curl -fsSL -o "$tmp/SHA256SUMS" "$base/SHA256SUMS" || fail "could not download $base/SHA256SUMS" want=$(awk -v f="$file" '$2 == f { print $1 }' "$tmp/SHA256SUMS") [ -n "$want" ] || fail "SHA256SUMS of $version does not list $file" [ "$(sha "$tmp/$file")" = "$want" ] || fail "$file does not match its checksum in SHA256SUMS: not installed" chmod 755 "$tmp/$file" SBX_NO_PIN=1 "$tmp/$file" version >/dev/null 2>&1 || fail "the downloaded sbx does not run on this machine" mkdir -p "$bin_dir" mv -f "$tmp/$file" "$bin_dir/sbx" ln -sf sbx "$bin_dir/sbxctl" say "Installed $(SBX_NO_PIN=1 "$bin_dir/sbx" version) at $bin_dir/sbx" # PATH: the startup file of the shell this person uses. on_path() { case ":$PATH:" in *":$bin_dir:"*) return 0 ;; *) return 1 ;; esac; } display_dir=${bin_dir/#$HOME/\$HOME} if ! on_path && [ "${SBX_NO_MODIFY_PATH:-}" != 1 ]; then shell_name=$(basename "${SHELL:-sh}") case "$shell_name" in zsh) rc=${ZDOTDIR:-$HOME}/.zshrc; line="export PATH=\"$display_dir:\$PATH\"" ;; bash) if [ "$os" = darwin ]; then rc=$HOME/.bash_profile; else rc=$HOME/.bashrc; fi; line="export PATH=\"$display_dir:\$PATH\"" ;; fish) rc=${XDG_CONFIG_HOME:-$HOME/.config}/fish/conf.d/sbx.fish; line="fish_add_path \"$bin_dir\"" ;; *) rc= ;; esac if [ -n "$rc" ]; then if ! grep -qsF "$line" "$rc"; then mkdir -p "$(dirname "$rc")" printf '\n# sbxctl\n%s\n' "$line" >>"$rc" say "Added $display_dir to PATH in $rc" fi say "Open a new terminal, or run: $line" else say "Add $bin_dir to your PATH" fi fi # Another sbx earlier on PATH would be the one that runs. other=$(command -v sbx 2>/dev/null || true) if on_path && [ -n "$other" ] && [ "$other" != "$bin_dir/sbx" ]; then say "Note: $other comes before $bin_dir on PATH, so \`sbx\` runs that one" fi # The configuration: asked for when there is none, of a person at a terminal. `curl | bash` reads # the script from stdin, so the questions are read from the terminal itself. config_dir=${SBX_CONFIG_DIR:-$HOME/.config/sbx} if [ ! -e "$config_dir/config.toml" ] && [ ! -e "$config_dir/sbxctl.config.ts" ] && [ "${SBX_NO_SETUP:-}" != 1 ] \ && [ -t 2 ] && { : /dev/null; then SBX_NO_PIN=1 "$bin_dir/sbx" config setup