|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Syndicate CLI Installer |
| 5 | +# Usage: |
| 6 | +# curl -L https://raw.githubusercontent.com/SyndicateProtocol/syndicate-appchains/main/synd-cli/install.sh | bash |
| 7 | +# curl -L https://raw.githubusercontent.com/SyndicateProtocol/syndicate-appchains/main/synd-cli/install.sh | SYND_VERSION=synd-cli-v1.0.0 bash |
| 8 | + |
| 9 | +REPO="SyndicateProtocol/syndicate-appchains" |
| 10 | +INSTALL_DIR="${SYND_INSTALL_DIR:-$HOME/.synd/bin}" |
| 11 | +BINARY_NAME="synd-cli" |
| 12 | + |
| 13 | +# Colors for output |
| 14 | +RED='\033[0;31m' |
| 15 | +GREEN='\033[0;32m' |
| 16 | +YELLOW='\033[1;33m' |
| 17 | +NC='\033[0m' # No Color |
| 18 | + |
| 19 | +info() { |
| 20 | + printf "${GREEN}info${NC}: %s\n" "$1" |
| 21 | +} |
| 22 | + |
| 23 | +warn() { |
| 24 | + printf "${YELLOW}warn${NC}: %s\n" "$1" |
| 25 | +} |
| 26 | + |
| 27 | +error() { |
| 28 | + printf "${RED}error${NC}: %s\n" "$1" |
| 29 | + exit 1 |
| 30 | +} |
| 31 | + |
| 32 | +detect_platform() { |
| 33 | + local os arch |
| 34 | + |
| 35 | + # Detect OS |
| 36 | + case "$(uname -s)" in |
| 37 | + Linux*) os="linux" ;; |
| 38 | + Darwin*) os="darwin" ;; |
| 39 | + *) error "Unsupported operating system: $(uname -s)" ;; |
| 40 | + esac |
| 41 | + |
| 42 | + # Detect architecture |
| 43 | + case "$(uname -m)" in |
| 44 | + x86_64|amd64) arch="x64" ;; |
| 45 | + arm64|aarch64) arch="arm64" ;; |
| 46 | + *) error "Unsupported architecture: $(uname -m)" ;; |
| 47 | + esac |
| 48 | + |
| 49 | + echo "${os}-${arch}" |
| 50 | +} |
| 51 | + |
| 52 | +get_latest_version() { |
| 53 | + local latest |
| 54 | + # Find the latest synd-cli release (tags starting with "synd-cli-") |
| 55 | + latest=$(curl -fsSL "https://api.github.com/repos/${REPO}/releases" | \ |
| 56 | + grep '"tag_name"' | \ |
| 57 | + sed -E 's/.*"([^"]+)".*/\1/' | \ |
| 58 | + grep '^synd-cli-' | \ |
| 59 | + head -n 1) |
| 60 | + |
| 61 | + if [[ -z "$latest" ]]; then |
| 62 | + error "Failed to fetch latest synd-cli version. Please check your internet connection or specify a version with SYND_VERSION=synd-cli-vX.Y.Z" |
| 63 | + fi |
| 64 | + |
| 65 | + echo "$latest" |
| 66 | +} |
| 67 | + |
| 68 | +download_binary() { |
| 69 | + local version="$1" |
| 70 | + local platform="$2" |
| 71 | + local url="https://github.com/${REPO}/releases/download/${version}/${BINARY_NAME}-${platform}-${version}" |
| 72 | + local tmp_file |
| 73 | + |
| 74 | + tmp_file=$(mktemp) |
| 75 | + |
| 76 | + info "Downloading synd-cli ${version} for ${platform}..." |
| 77 | + |
| 78 | + if ! curl -fsSL "$url" -o "$tmp_file"; then |
| 79 | + rm -f "$tmp_file" |
| 80 | + error "Failed to download from ${url}. Please check that version ${version} exists and has binaries for ${platform}." |
| 81 | + fi |
| 82 | + |
| 83 | + echo "$tmp_file" |
| 84 | +} |
| 85 | + |
| 86 | +install_binary() { |
| 87 | + local tmp_file="$1" |
| 88 | + local install_path="${INSTALL_DIR}/${BINARY_NAME}" |
| 89 | + |
| 90 | + # Create install directory |
| 91 | + mkdir -p "$INSTALL_DIR" |
| 92 | + |
| 93 | + # Move binary to install location |
| 94 | + mv "$tmp_file" "$install_path" |
| 95 | + chmod +x "$install_path" |
| 96 | + |
| 97 | + info "Installed synd-cli to ${install_path}" |
| 98 | +} |
| 99 | + |
| 100 | +setup_path_instructions() { |
| 101 | + local shell_name |
| 102 | + local rc_file |
| 103 | + |
| 104 | + shell_name=$(basename "$SHELL") |
| 105 | + |
| 106 | + case "$shell_name" in |
| 107 | + bash) |
| 108 | + if [[ -f "$HOME/.bash_profile" ]]; then |
| 109 | + rc_file="$HOME/.bash_profile" |
| 110 | + else |
| 111 | + rc_file="$HOME/.bashrc" |
| 112 | + fi |
| 113 | + ;; |
| 114 | + zsh) rc_file="$HOME/.zshrc" ;; |
| 115 | + fish) rc_file="$HOME/.config/fish/config.fish" ;; |
| 116 | + *) rc_file="your shell's config file" ;; |
| 117 | + esac |
| 118 | + |
| 119 | + # Check if already in PATH |
| 120 | + if [[ ":$PATH:" == *":$INSTALL_DIR:"* ]]; then |
| 121 | + info "synd-cli is ready to use!" |
| 122 | + else |
| 123 | + echo "" |
| 124 | + warn "Add synd-cli to your PATH by running:" |
| 125 | + echo "" |
| 126 | + if [[ "$shell_name" == "fish" ]]; then |
| 127 | + printf " fish_add_path %s\n" "$INSTALL_DIR" |
| 128 | + else |
| 129 | + printf " echo 'export PATH=\"%s:\$PATH\"' >> %s\n" "$INSTALL_DIR" "$rc_file" |
| 130 | + fi |
| 131 | + echo "" |
| 132 | + printf "Then restart your shell or run:\n" |
| 133 | + printf " source %s\n" "$rc_file" |
| 134 | + echo "" |
| 135 | + fi |
| 136 | +} |
| 137 | + |
| 138 | +main() { |
| 139 | + local version="${SYND_VERSION:-}" |
| 140 | + local platform |
| 141 | + |
| 142 | + info "Syndicate CLI Installer" |
| 143 | + echo "" |
| 144 | + |
| 145 | + # Detect platform |
| 146 | + platform=$(detect_platform) |
| 147 | + info "Detected platform: ${platform}" |
| 148 | + |
| 149 | + # Get version (use provided or fetch latest) |
| 150 | + if [[ -z "$version" ]]; then |
| 151 | + info "Fetching latest version..." |
| 152 | + version=$(get_latest_version) |
| 153 | + fi |
| 154 | + info "Version: ${version}" |
| 155 | + |
| 156 | + # Download binary |
| 157 | + local tmp_file |
| 158 | + tmp_file=$(download_binary "$version" "$platform") |
| 159 | + |
| 160 | + # Install binary |
| 161 | + install_binary "$tmp_file" |
| 162 | + |
| 163 | + # Show PATH setup instructions |
| 164 | + setup_path_instructions |
| 165 | + |
| 166 | + info "Installation complete!" |
| 167 | +} |
| 168 | + |
| 169 | +main "$@" |
0 commit comments