|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# Installation script for fetch |
| 5 | +# Usage: curl -fsSL https://raw.githubusercontent.com/ryanfowler/fetch/main/install.sh | bash |
| 6 | + |
| 7 | +LATEST_RELEASE_URL="https://api.github.com/repos/ryanfowler/fetch/releases/latest" |
| 8 | + |
| 9 | +RESET="" |
| 10 | +BOLD="" |
| 11 | +DIM="" |
| 12 | +RED="" |
| 13 | +GREEN="" |
| 14 | +YELLOW="" |
| 15 | + |
| 16 | +# Set escape sequences if stderr is a terminal. |
| 17 | +if [ -t 2 ]; then |
| 18 | + RESET="\033[0m" |
| 19 | + BOLD="\033[1m" |
| 20 | + DIM="\033[2m" |
| 21 | + RED="\033[31m" |
| 22 | + GREEN="\033[32m" |
| 23 | + YELLOW="\033[33m" |
| 24 | +fi |
| 25 | + |
| 26 | +# Print info message. |
| 27 | +info() { |
| 28 | + echo -e "${BOLD}${GREEN}info${RESET}: $1" |
| 29 | +} |
| 30 | + |
| 31 | +# Print warning message. |
| 32 | +warning() { |
| 33 | + echo -e "${BOLD}${YELLOW}warning${RESET}: $1" |
| 34 | +} |
| 35 | + |
| 36 | +# Print error message. |
| 37 | +error() { |
| 38 | + echo -e "${BOLD}${RED}error${RESET}: $1" |
| 39 | +} |
| 40 | + |
| 41 | +# Print compile from source message. |
| 42 | +compile_msg() { |
| 43 | + echo -e "\nTry compiling from source by running: '${DIM}go install github.com/ryanfowler/fetch@latest${RESET}'" |
| 44 | +} |
| 45 | + |
| 46 | +# Determine OS and architecture. |
| 47 | +OS=$(uname -s) |
| 48 | +ARCH=$(uname -m) |
| 49 | + |
| 50 | +case "$OS" in |
| 51 | + Darwin) OS="darwin" ;; |
| 52 | + Linux) OS="linux" ;; |
| 53 | + *) |
| 54 | + error "no release artifact found for operating system: $OS" |
| 55 | + compile_msg |
| 56 | + exit 1 |
| 57 | + ;; |
| 58 | +esac |
| 59 | + |
| 60 | +case "$ARCH" in |
| 61 | + x86_64|amd64) ARCH="amd64" ;; |
| 62 | + aarch64|arm64) ARCH="arm64" ;; |
| 63 | + *) |
| 64 | + error "no release artifact found for architecture: $ARCH" |
| 65 | + compile_msg |
| 66 | + exit 1 |
| 67 | + ;; |
| 68 | +esac |
| 69 | + |
| 70 | +PLATFORM="${OS}-${ARCH}" |
| 71 | + |
| 72 | +# Fetch the latest release asset |
| 73 | +info "fetching latest release tag" |
| 74 | + |
| 75 | +if ! command -v curl &> /dev/null; then |
| 76 | + error "curl is required but not installed" |
| 77 | + exit 1 |
| 78 | +fi |
| 79 | + |
| 80 | +HAS_JQ=false |
| 81 | +if command -v jq &> /dev/null; then |
| 82 | + HAS_JQ=true |
| 83 | +fi |
| 84 | + |
| 85 | +RELEASE_JSON=$(curl -s "$LATEST_RELEASE_URL") |
| 86 | + |
| 87 | +VERSION="" |
| 88 | +if $HAS_JQ; then |
| 89 | + VERSION=$(echo "$RELEASE_JSON" | jq -r .tag_name) |
| 90 | +else |
| 91 | + VERSION=$(echo "$RELEASE_JSON" | grep -o '"tag_name": *"[^"]*"' | sed 's/"tag_name": *"//;s/"//') |
| 92 | +fi |
| 93 | +if [ -z "$VERSION" ]; then |
| 94 | + error "unable to determine the latest version" |
| 95 | + exit 1 |
| 96 | +fi |
| 97 | + |
| 98 | +# Download the artifact. |
| 99 | +DOWNLOAD_URL="" |
| 100 | +if $HAS_JQ; then |
| 101 | + DOWNLOAD_URL=$(echo "$RELEASE_JSON" | jq -r ".assets.[] | select(.name == \"fetch-v0.7.5-${PLATFORM}.tar.gz\") | .browser_download_url") |
| 102 | +else |
| 103 | + DOWNLOAD_URL=$(echo "$RELEASE_JSON" | grep -o "\"browser_download_url\": *\"[^\"]*${PLATFORM}[^\"]*\"" | sed 's/"browser_download_url": *"//;s/"//') |
| 104 | +fi |
| 105 | +if [ -z "$DOWNLOAD_URL" ]; then |
| 106 | + error "no release artifact found for ${OS}/${ARCH}" |
| 107 | + exit 1 |
| 108 | +fi |
| 109 | + |
| 110 | +# Create temporary directory. |
| 111 | +TMP_DIR=$(mktemp -d) |
| 112 | +BINARY_PATH="${TMP_DIR}/fetch" |
| 113 | + |
| 114 | +info "downloading latest version (${VERSION})" |
| 115 | +if ! curl -fsSL "$DOWNLOAD_URL" -o "$BINARY_PATH.tar.gz"; then |
| 116 | + error "unable to download artifact" |
| 117 | + exit 1 |
| 118 | +fi |
| 119 | + |
| 120 | +tar -xzf "$BINARY_PATH.tar.gz" -C "$TMP_DIR" |
| 121 | +chmod +x "$BINARY_PATH" |
| 122 | + |
| 123 | +# Determine installation directory. |
| 124 | +if [ -w "/usr/local/bin" ]; then |
| 125 | + # Can write to /usr/local/bin. |
| 126 | + INSTALL_DIR="/usr/local/bin" |
| 127 | +else |
| 128 | + # Use home directory. |
| 129 | + INSTALL_DIR="$HOME/.local/bin" |
| 130 | + mkdir -p "$INSTALL_DIR" |
| 131 | +fi |
| 132 | + |
| 133 | +mv "$BINARY_PATH" "$INSTALL_DIR/fetch" |
| 134 | +info "fetch successfully installed to '${DIM}${INSTALL_DIR}/fetch${RESET}'" |
| 135 | + |
| 136 | +# Clean up. |
| 137 | +rm -rf "$TMP_DIR" |
| 138 | + |
| 139 | +# Verify installation. |
| 140 | +if ! command -v fetch &> /dev/null; then |
| 141 | + echo "" |
| 142 | + warning "you may need to add '${DIM}${INSTALL_DIR}${RESET}' to your PATH" |
| 143 | +fi |
| 144 | + |
0 commit comments