| 1 | #!/bin/sh |
| 2 | # Installs the g1t CLI on Linux or macOS: |
| 3 | # |
| 4 | # curl -fsSL https://g1t.sh/install | sh |
| 5 | # |
| 6 | # It downloads the build for this system from g1t.sh/downloads/cli, checks it |
| 7 | # against the release's SHA256SUMS, and puts it in ~/.local/bin. |
| 8 | # |
| 9 | # G1T_INSTALL_DIR where to put g1t (default: ~/.local/bin) |
| 10 | # G1T_VERSION a version to install, such as 0.2.0 (default: the latest) |
| 11 | # |
| 12 | # Docs: https://docs.g1t.sh/guides/cli/ |
| 13 | set -eu |
| 14 | |
| 15 | base="${G1T_DOWNLOAD_URL:-https://g1t.sh/downloads/cli}" |
| 16 | version="${G1T_VERSION:-latest}" |
| 17 | version="${version#v}" |
| 18 | dir="${G1T_INSTALL_DIR:-$HOME/.local/bin}" |
| 19 | |
| 20 | say() { printf '%s\n' "$*" >&2; } |
| 21 | fail() { |
| 22 | say "g1t install: $*" |
| 23 | exit 1 |
| 24 | } |
| 25 | |
| 26 | case "$(uname -s)" in |
| 27 | Linux) os=linux ;; |
| 28 | Darwin) os=macos ;; |
| 29 | MINGW* | MSYS* | CYGWIN*) fail "on Windows, run this in PowerShell instead: irm https://g1t.sh/install.ps1 | iex" ;; |
| 30 | *) fail "there is no build for $(uname -s)." ;; |
| 31 | esac |
| 32 | case "$(uname -m)" in |
| 33 | x86_64 | amd64) arch=x64 ;; |
| 34 | arm64 | aarch64) arch=arm64 ;; |
| 35 | *) fail "there is no build for $(uname -m)." ;; |
| 36 | esac |
| 37 | # An Intel shell on Apple silicon (Rosetta) gets the native build. |
| 38 | if [ "$os" = macos ] && [ "$arch" = x64 ] && [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || echo 0)" = 1 ]; then |
| 39 | arch=arm64 |
| 40 | fi |
| 41 | file="g1t-$os-$arch" |
| 42 | |
| 43 | if command -v curl >/dev/null 2>&1; then |
| 44 | get() { curl -fsSL --retry 3 -o "$2" "$1"; } |
| 45 | elif command -v wget >/dev/null 2>&1; then |
| 46 | get() { wget -q -O "$2" "$1"; } |
| 47 | else |
| 48 | fail "this needs curl or wget." |
| 49 | fi |
| 50 | |
| 51 | tmp="$(mktemp -d)" |
| 52 | trap 'rm -rf "$tmp"' EXIT INT TERM |
| 53 | |
| 54 | say "Downloading g1t ($version, $os $arch)..." |
| 55 | get "$base/$version/$file" "$tmp/g1t" || fail "could not download $base/$version/$file." |
| 56 | get "$base/$version/SHA256SUMS" "$tmp/SHA256SUMS" || fail "could not download $base/$version/SHA256SUMS." |
| 57 | |
| 58 | # A name may carry sha256sum's binary-mode `*`. |
| 59 | want="$(awk -v f="$file" '{ sub(/^[*]/, "", $2) } $2 == f { print $1 }' "$tmp/SHA256SUMS")" |
| 60 | [ -n "$want" ] || fail "SHA256SUMS names no $file." |
| 61 | if command -v sha256sum >/dev/null 2>&1; then |
| 62 | have="$(sha256sum "$tmp/g1t" | awk '{ print $1 }')" |
| 63 | elif command -v shasum >/dev/null 2>&1; then |
| 64 | have="$(shasum -a 256 "$tmp/g1t" | awk '{ print $1 }')" |
| 65 | else |
| 66 | fail "this needs sha256sum or shasum to check the download." |
| 67 | fi |
| 68 | [ "$want" = "$have" ] || fail "the download's SHA-256 is $have, not $want. Nothing was installed." |
| 69 | |
| 70 | chmod +x "$tmp/g1t" |
| 71 | mkdir -p "$dir" || fail "could not make $dir. Set G1T_INSTALL_DIR to a directory you can write to." |
| 72 | mv "$tmp/g1t" "$dir/g1t" || fail "could not write $dir/g1t. Set G1T_INSTALL_DIR to a directory you can write to." |
| 73 | |
| 74 | say "Installed $("$dir/g1t" --version) to $dir/g1t" |
| 75 | case ":$PATH:" in |
| 76 | *":$dir:"*) ;; |
| 77 | *) say "$dir is not on your PATH. Add it to your shell's profile: export PATH=\"$dir:\$PATH\"" ;; |
| 78 | esac |
| 79 | say "Next: g1t auth login" |