|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Utils |
| 4 | +RED='\033[0;31m' |
| 5 | +GREEN='\033[0;32m' |
| 6 | +NC='\033[0m' |
| 7 | + |
| 8 | +error() { |
| 9 | + echo -e "${RED}Error: $1${NC}" >&2 |
| 10 | + exit 1 |
| 11 | +} |
| 12 | + |
| 13 | +success() { |
| 14 | + echo -e "${GREEN}$1${NC}" |
| 15 | +} |
| 16 | + |
| 17 | +# 1. Verify requirements more efficiently |
| 18 | +check_requirements() { |
| 19 | + local missing=() |
| 20 | + for cmd in jq curl; do |
| 21 | + if ! command -v "$cmd" >/dev/null; then |
| 22 | + missing+=("$cmd") |
| 23 | + fi |
| 24 | + done |
| 25 | + |
| 26 | + if [ ${#missing[@]} -eq 0 ]; then |
| 27 | + success "\u2705 jq and curl exist" |
| 28 | + else |
| 29 | + error "Missing required tools: ${missing[*]}" |
| 30 | + fi |
| 31 | +} |
| 32 | + |
| 33 | +# 2. JSON download and parsing |
| 34 | +ZIX_JSON_URL="https://raw.githubusercontent.com/zix-rs/zix/refs/heads/main/zix.json" |
| 35 | +download_and_parse() { |
| 36 | + ZIX_JSON=$(curl -s --connect-timeout 10 --max-time 30 "$ZIX_JSON_URL") |
| 37 | + |
| 38 | + if [ -z "$ZIX_JSON" ]; then |
| 39 | + error "Could not download JSON file from $ZIX_JSON_URL" |
| 40 | + fi |
| 41 | + |
| 42 | + read -r LTS_VERSION INSTALLER_URL INSTALLER_NAME << EOF |
| 43 | + $(echo "$ZIX_JSON" | jq -r '[.dist.latest, .versions[.dist.latest].url, .bin] | @tsv') |
| 44 | +EOF |
| 45 | + |
| 46 | + if [ -z "$LTS_VERSION" ] || [ -z "$INSTALLER_URL" ] || [ -z "$INSTALLER_NAME" ]; then |
| 47 | + error "Error parsing JSON. Please verify the structure at $ZIX_JSON_URL" |
| 48 | + fi |
| 49 | +} |
| 50 | + |
| 51 | +# 3. installation |
| 52 | +ZIX_DIR="$HOME/.zix" |
| 53 | +install_binary() { |
| 54 | + local ZIX_BIN="$ZIX_DIR/$INSTALLER_NAME" |
| 55 | + |
| 56 | + mkdir -p "$ZIX_DIR" || error "Could not create directory $ZIX_DIR" |
| 57 | + |
| 58 | + echo "Downloading $INSTALLER_NAME version $LTS_VERSION..." |
| 59 | + if ! curl -L --connect-timeout 10 --max-time 300 --progress-bar -o "$ZIX_BIN" "$INSTALLER_URL"; then |
| 60 | + error "Error downloading $INSTALLER_NAME from $INSTALLER_URL" |
| 61 | + fi |
| 62 | + |
| 63 | + chmod +x "$ZIX_BIN" || error "Could not make $ZIX_BIN executable" |
| 64 | +} |
| 65 | + |
| 66 | +# 4. PATH configuration |
| 67 | +configure_path() { |
| 68 | + if ! echo "$PATH" | grep -q "$ZIX_DIR"; then |
| 69 | + echo "Adding $ZIX_DIR to PATH..." |
| 70 | + export PATH="$ZIX_DIR:$PATH" |
| 71 | + |
| 72 | + # Use a single loop for RC files |
| 73 | + for rc in "$HOME/.bashrc" "$HOME/.zshrc"; do |
| 74 | + if [ -f "$rc" ] && ! grep -q "$ZIX_DIR" "$rc"; then |
| 75 | + echo "export PATH=\"$ZIX_DIR:\$PATH\"" >> "$rc" |
| 76 | + fi |
| 77 | + done |
| 78 | + fi |
| 79 | +} |
| 80 | + |
| 81 | +# 5. verification |
| 82 | +verify_installation() { |
| 83 | + if command -v "$INSTALLER_NAME" >/dev/null; then |
| 84 | + # Try to execute the command with --version or -v to verify it works |
| 85 | + if "$INSTALLER_NAME" --version >/dev/null 2>&1 || "$INSTALLER_NAME" -v >/dev/null 2>&1; then |
| 86 | + success "$INSTALLER_NAME is installed and working correctly" |
| 87 | + else |
| 88 | + error "$INSTALLER_NAME is installed but might have issues" |
| 89 | + fi |
| 90 | + else |
| 91 | + error "$INSTALLER_NAME could not be verified" |
| 92 | + fi |
| 93 | +} |
| 94 | + |
| 95 | +# main |
| 96 | +main() { |
| 97 | + check_requirements |
| 98 | + download_and_parse |
| 99 | + install_binary |
| 100 | + configure_path |
| 101 | + verify_installation |
| 102 | +} |
| 103 | + |
| 104 | +main |
0 commit comments