|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +repo="owainlewis/cortex" |
| 5 | +install_dir="${CORTEX_INSTALL_DIR:-$HOME/.local/bin}" |
| 6 | +version="" |
| 7 | +install_tmp="" |
| 8 | + |
| 9 | +usage() { |
| 10 | + cat <<'USAGE' |
| 11 | +Install or update Cortex from GitHub Releases. |
| 12 | +
|
| 13 | +Usage: |
| 14 | + install.sh [--version vX.Y.Z] |
| 15 | +
|
| 16 | +Environment: |
| 17 | + CORTEX_INSTALL_DIR Install directory. Defaults to ~/.local/bin. |
| 18 | +USAGE |
| 19 | +} |
| 20 | + |
| 21 | +error() { |
| 22 | + printf 'cortex install: %s\n' "$*" >&2 |
| 23 | + exit 1 |
| 24 | +} |
| 25 | + |
| 26 | +need() { |
| 27 | + command -v "$1" >/dev/null 2>&1 || error "missing required command: $1" |
| 28 | +} |
| 29 | + |
| 30 | +while [ "$#" -gt 0 ]; do |
| 31 | + case "$1" in |
| 32 | + --version) |
| 33 | + [ "$#" -ge 2 ] || error "--version requires a tag such as v0.2.0" |
| 34 | + version="$2" |
| 35 | + shift 2 |
| 36 | + ;; |
| 37 | + -h | --help) |
| 38 | + usage |
| 39 | + exit 0 |
| 40 | + ;; |
| 41 | + *) |
| 42 | + error "unknown argument: $1" |
| 43 | + ;; |
| 44 | + esac |
| 45 | +done |
| 46 | + |
| 47 | +need curl |
| 48 | +need head |
| 49 | +need install |
| 50 | +need mkdir |
| 51 | +need mktemp |
| 52 | +need mv |
| 53 | +need rm |
| 54 | +need sed |
| 55 | +need shasum |
| 56 | +need tar |
| 57 | +need uname |
| 58 | + |
| 59 | +os="$(uname -s)" |
| 60 | +arch="$(uname -m)" |
| 61 | + |
| 62 | +if [ "$os" != "Darwin" ]; then |
| 63 | + error "unsupported platform: $os. Cortex currently ships macOS binaries only." |
| 64 | +fi |
| 65 | + |
| 66 | +case "$arch" in |
| 67 | + arm64) |
| 68 | + target_triple="aarch64-apple-darwin" |
| 69 | + ;; |
| 70 | + *) |
| 71 | + error "unsupported macOS architecture: $arch. Cortex currently ships arm64 binaries only." |
| 72 | + ;; |
| 73 | +esac |
| 74 | + |
| 75 | +latest_version() { |
| 76 | + response="$(curl -fsSL "https://api.github.com/repos/${repo}/releases/latest")" \ |
| 77 | + || error "failed to fetch latest release metadata" |
| 78 | + tag="$(printf '%s\n' "$response" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)" |
| 79 | + [ -n "$tag" ] || error "could not find a latest release tag" |
| 80 | + printf '%s\n' "$tag" |
| 81 | +} |
| 82 | + |
| 83 | +if [ -z "$version" ]; then |
| 84 | + version="$(latest_version)" |
| 85 | +fi |
| 86 | + |
| 87 | +archive="cortex-${version}-${target_triple}.tar.gz" |
| 88 | +checksum="${archive}.sha256" |
| 89 | +base_url="https://github.com/${repo}/releases/download/${version}" |
| 90 | +tmp_dir="$(mktemp -d)" |
| 91 | + |
| 92 | +cleanup() { |
| 93 | + rm -rf "$tmp_dir" |
| 94 | + if [ -n "$install_tmp" ]; then |
| 95 | + rm -f "$install_tmp" |
| 96 | + fi |
| 97 | +} |
| 98 | +trap cleanup EXIT |
| 99 | + |
| 100 | +printf 'Installing Cortex %s for %s\n' "$version" "$target_triple" |
| 101 | + |
| 102 | +curl -fsSL "${base_url}/${archive}" -o "${tmp_dir}/${archive}" \ |
| 103 | + || error "failed to download ${archive}" |
| 104 | +curl -fsSL "${base_url}/${checksum}" -o "${tmp_dir}/${checksum}" \ |
| 105 | + || error "failed to download ${checksum}" |
| 106 | + |
| 107 | +( |
| 108 | + cd "$tmp_dir" |
| 109 | + shasum -a 256 -c "$checksum" >/dev/null |
| 110 | +) || error "checksum verification failed" |
| 111 | + |
| 112 | +tar -xzf "${tmp_dir}/${archive}" -C "$tmp_dir" cortex \ |
| 113 | + || error "failed to extract cortex binary" |
| 114 | + |
| 115 | +mkdir -p "$install_dir" |
| 116 | +install_tmp="$(mktemp "${install_dir}/.cortex.XXXXXX")" \ |
| 117 | + || error "failed to create temporary install file in ${install_dir}" |
| 118 | +install -m 0755 "${tmp_dir}/cortex" "$install_tmp" \ |
| 119 | + || error "failed to stage cortex binary in ${install_dir}" |
| 120 | +mv -f "$install_tmp" "${install_dir}/cortex" \ |
| 121 | + || error "failed to replace ${install_dir}/cortex" |
| 122 | +install_tmp="" |
| 123 | + |
| 124 | +printf 'Installed cortex to %s/cortex\n' "$install_dir" |
| 125 | + |
| 126 | +case ":$PATH:" in |
| 127 | + *":$install_dir:"*) ;; |
| 128 | + *) |
| 129 | + printf 'Add %s to PATH to run cortex from any shell.\n' "$install_dir" |
| 130 | + ;; |
| 131 | +esac |
0 commit comments