#!/bin/sh # Install howfar: download a release from GitHub, verify its SHA-256 against # the release's checksums.txt, and put the binary in ~/.local/bin. No sudo. # # curl -fsSL https://raw.githubusercontent.com/JonathanColetti/howfar/main/install.sh | sh # # POSIX sh, not bash: the line above pipes it to whatever sh the machine has. # Everything runs from main() on the last line, so a truncated download runs # nothing. There is no `set -e`: every step that can fail is checked where it # runs. `local` is not POSIX, but every Linux /bin/sh (dash, bash, busybox # ash, mksh) has it. set -u REPO=JonathanColetti/howfar SCRIPT_NAME=install.sh DEPENDENCIES="uname mktemp tar" DOWNLOADERS="curl wget" HASHERS="sha256sum shasum" readonly REPO SCRIPT_NAME DEPENDENCIES DOWNLOADERS HASHERS # The temporary directory, global so the EXIT trap can remove it. TMP_DIR="" RED=$(printf '\033[0;31m') GREEN=$(printf '\033[0;32m') YELLOW=$(printf '\033[1;33m') NC=$(printf '\033[0m') if [ -n "${NO_COLOR:-}" ] || [ "${TERM:-dumb}" = dumb ] || [ ! -t 1 ]; then RED="" GREEN="" YELLOW="" NC="" fi readonly RED GREEN YELLOW NC usage() { cat < ", or " *" in # binary mode. Shell only: some images (openSUSE Tumbleweed) have no awk. checksum_for() { local sum file while read -r sum file || [ -n "$sum" ]; do if [ "$file" = "$1" ] || [ "$file" = "*$1" ]; then echo "$sum" return fi done } # install_binary extracts only the binary, never README or LICENSE, and # renames it into place so a running howfar is never half overwritten. install_binary() { local tarball=$1 dir=$2 tar -xzf "$TMP_DIR/$tarball" -C "$TMP_DIR" howfar || die "could not extract howfar from $tarball" mkdir -p "$dir" || die "could not create $dir" cp "$TMP_DIR/howfar" "$dir/.howfar.new" || die "could not write to $dir" chmod 755 "$dir/.howfar.new" || die "could not make $dir/.howfar.new executable" mv -f "$dir/.howfar.new" "$dir/howfar" || die "could not move the binary to $dir/howfar" } print_next_steps() { local dir=$1 base=$2 tarball=$3 print_success "Installed $("$dir/howfar" version 2>/dev/null || echo howfar) to $dir/howfar" case ":$PATH:" in *":$dir:"*) say "$dir is on your PATH. Run: howfar" ;; *) print_warning "$dir is not on your PATH. Add it, for example:" say " echo 'export PATH=\"$dir:\$PATH\"' >> ~/.profile && . ~/.profile" say "or run it directly: $dir/howfar" ;; esac say "" say "Optional: check the release was built by this repo's GitHub workflow:" say " curl -fsSLO $base/$tarball && gh attestation verify $tarball -R $REPO" say "Uninstall: rm $dir/howfar; rm -rf ~/.cache/howfar" } # fetch URL FILE downloads over HTTPS. curl and GNU wget also refuse a # redirect to plain HTTP. Busybox wget (the only downloader on a stock # Alpine) has no option for that, but it still checks the certificate. fetch() { if command -v curl >/dev/null 2>&1; then curl -fsSL --proto '=https' --tlsv1.2 -o "$2" "$1" return fi if wget_has_https_only; then wget -q --https-only -O "$2" "$1" return fi wget -q -O "$2" "$1" } wget_has_https_only() { case $(wget --help 2>&1) in *--https-only*) return 0 ;; *) return 1 ;; esac } # sha256 file prints the file's SHA-256 checksum. sha256() { local out if command -v sha256sum >/dev/null 2>&1; then out=$(sha256sum "$1") || return 1 else out=$(shasum -a 256 "$1") || return 1 fi echo "${out%% *}" } say() { printf '%s\n' "$*" } print_step() { printf '%s[%s/%s] %s%s\n' "$YELLOW" "$1" "$2" "$3" "$NC" } print_success() { printf '%s%s%s\n' "$GREEN" "$1" "$NC" } print_warning() { printf '%s%s%s\n' "$YELLOW" "$1" "$NC" } print_error() { printf '%s%s: %s%s\n' "$RED" "howfar install" "$1" "$NC" >&2 } die() { print_error "$*" exit 1 } exit_on_missing_tools() { local cmd for cmd in "$@"; do if command -v "$cmd" >/dev/null 2>&1; then continue fi die "required tool '$cmd' is not installed or not in PATH" done } # exit_on_missing_alternatives exits unless at least one of the tools exists. exit_on_missing_alternatives() { local cmd for cmd in "$@"; do if command -v "$cmd" >/dev/null 2>&1; then return 0 fi done die "need one of: $*" } main "$@"