#!/bin/sh # Installs ezgh, the EZGH Cloud CLI, on macOS or Linux: # # curl -fsSL https://get.ezghcloud.com | sh # # (it's also at https://get.ezghcloud.com/install.sh and /cli.sh). # # It downloads the release archive for this machine from get.ezghcloud.com, checks it against the # release's checksums.txt, and puts the ezgh binary in EZGH_INSTALL_DIR: by default # /usr/local/bin when that's writable, otherwise ~/.local/bin. It never uses sudo. # # EZGH_VERSION a version to install (0.1.0 or v0.1.0) instead of the latest # EZGH_INSTALL_DIR where to put ezgh # EZGH_DOWNLOAD_URL where releases are (https://get.ezghcloud.com), for testing # # Windows: irm https://get.ezghcloud.com/cli.ps1 | iex # # Everything is in main, which runs on the last line, so a partly downloaded script does nothing. set -eu say() { printf '%s\n' "$*" >&2; } fail() { say "ezgh install: $*"; exit 1; } # A transfer that stalls (under 1 KiB/s for 30 seconds) fails as a timeout, which is retried like # a dropped connection, rather than hanging. fetch() { # fetch if command -v curl >/dev/null 2>&1; then curl -fsSL --retry 3 --connect-timeout 15 --speed-time 30 --speed-limit 1024 -o "$2" "$1" || fail "couldn't download $1" elif command -v wget >/dev/null 2>&1; then wget -q --tries=3 -T 30 -O "$2" "$1" || fail "couldn't download $1" else fail "curl or wget is needed" fi } sha256() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d ' ' -f 1 elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d ' ' -f 1 else fail "sha256sum or shasum is needed to check the download" fi } platform() { case "$(uname -s)" in Linux) os=linux ;; Darwin) os=darwin ;; MINGW* | MSYS* | CYGWIN*) fail "on Windows, run in PowerShell: irm https://get.ezghcloud.com/cli.ps1 | iex" ;; *) fail "there's no ezgh for $(uname -s); it runs on macOS, Linux and Windows" ;; esac case "$(uname -m)" in x86_64 | amd64) arch=amd64 ;; aarch64 | arm64) arch=arm64 ;; *) fail "there's no ezgh for $(uname -m); it runs on amd64 and arm64" ;; esac # A shell running under Rosetta says x86_64 on Apple silicon; install the native build. if [ "$os" = darwin ] && [ "$arch" = amd64 ] && [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || true)" = 1 ]; then arch=arm64 fi } main() { base="${EZGH_DOWNLOAD_URL:-https://get.ezghcloud.com}" base="${base%/}/ezgh" platform tmp="$(mktemp -d 2>/dev/null || mktemp -d -t ezgh)" trap 'rm -rf "$tmp"' EXIT INT TERM version="${EZGH_VERSION:-}" if [ -z "$version" ]; then fetch "$base/latest" "$tmp/latest" version="$(tr -d ' \r\n' <"$tmp/latest")" fi version="${version#v}" case "$version" in '' | *[!0-9A-Za-z.+-]*) fail "not a version: '$version'" ;; esac archive="ezgh_${version}_${os}_${arch}.tar.gz" say "Downloading ezgh $version for $os/$arch" fetch "$base/v$version/checksums.txt" "$tmp/checksums.txt" fetch "$base/v$version/$archive" "$tmp/$archive" want="$(awk -v f="$archive" '$2 == f { print $1 }' "$tmp/checksums.txt")" [ -n "$want" ] || fail "$archive isn't in the release's checksums.txt" [ "$(sha256 "$tmp/$archive")" = "$want" ] || fail "$archive doesn't match its checksum; nothing was installed" tar -xzf "$tmp/$archive" -C "$tmp" ezgh || fail "couldn't unpack $archive" dir="${EZGH_INSTALL_DIR:-}" if [ -z "$dir" ]; then if [ -d /usr/local/bin ] && [ -w /usr/local/bin ]; then dir=/usr/local/bin else [ -n "${HOME:-}" ] || fail "set EZGH_INSTALL_DIR to where ezgh should go" dir="$HOME/.local/bin" fi fi mkdir -p "$dir" || fail "couldn't create $dir" [ -w "$dir" ] || fail "can't write to $dir; set EZGH_INSTALL_DIR to a directory you can write" # Replaced in one step, so a running ezgh or a failed copy never leaves half a binary. if ! { cp "$tmp/ezgh" "$dir/.ezgh.$$" && chmod 755 "$dir/.ezgh.$$" && mv -f "$dir/.ezgh.$$" "$dir/ezgh"; }; then rm -f "$dir/.ezgh.$$" fail "couldn't install to $dir" fi say "Installed ezgh $version to $dir/ezgh" case ":${PATH:-}:" in *":$dir:"*) ;; *) say "$dir isn't on your PATH. Add it, e.g.: echo 'export PATH=\"$dir:\$PATH\"' >> ~/.profile" ;; esac found="$(command -v ezgh 2>/dev/null || true)" if [ -n "$found" ] && [ "$found" != "$dir/ezgh" ]; then say "Another ezgh comes first on your PATH: $found" fi say "Next: ezgh login" } main "$@"