|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Socket CLI installation script. |
| 3 | +# Downloads and installs the appropriate Socket CLI binary for your platform. |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +# Colors for output. |
| 8 | +RED='\033[0;31m' |
| 9 | +GREEN='\033[0;32m' |
| 10 | +YELLOW='\033[1;33m' |
| 11 | +BLUE='\033[0;34m' |
| 12 | +NC='\033[0m' # No Color |
| 13 | + |
| 14 | +# Print colored messages. |
| 15 | +info() { |
| 16 | + echo -e "${BLUE}ℹ${NC} $1" |
| 17 | +} |
| 18 | + |
| 19 | +success() { |
| 20 | + echo -e "${GREEN}✓${NC} $1" |
| 21 | +} |
| 22 | + |
| 23 | +error() { |
| 24 | + echo -e "${RED}✗${NC} $1" |
| 25 | +} |
| 26 | + |
| 27 | +warning() { |
| 28 | + echo -e "${YELLOW}⚠${NC} $1" |
| 29 | +} |
| 30 | + |
| 31 | +# Detect platform and architecture. |
| 32 | +detect_platform() { |
| 33 | + local os |
| 34 | + local arch |
| 35 | + |
| 36 | + # Detect OS. |
| 37 | + case "$(uname -s)" in |
| 38 | + Linux*) |
| 39 | + os="linux" |
| 40 | + ;; |
| 41 | + Darwin*) |
| 42 | + os="darwin" |
| 43 | + ;; |
| 44 | + MINGW*|MSYS*|CYGWIN*) |
| 45 | + os="win32" |
| 46 | + ;; |
| 47 | + *) |
| 48 | + error "Unsupported operating system: $(uname -s)" |
| 49 | + exit 1 |
| 50 | + ;; |
| 51 | + esac |
| 52 | + |
| 53 | + # Detect architecture. |
| 54 | + case "$(uname -m)" in |
| 55 | + x86_64|amd64) |
| 56 | + arch="x64" |
| 57 | + ;; |
| 58 | + aarch64|arm64) |
| 59 | + arch="arm64" |
| 60 | + ;; |
| 61 | + *) |
| 62 | + error "Unsupported architecture: $(uname -m)" |
| 63 | + exit 1 |
| 64 | + ;; |
| 65 | + esac |
| 66 | + |
| 67 | + echo "${os}-${arch}" |
| 68 | +} |
| 69 | + |
| 70 | +# Get the latest release version from GitHub. |
| 71 | +get_latest_version() { |
| 72 | + local version |
| 73 | + |
| 74 | + # Try using curl with GitHub API. |
| 75 | + if command -v curl &> /dev/null; then |
| 76 | + version=$(curl -fsSL https://api.github.com/repos/SocketDev/socket-cli/releases/latest | grep -o '"tag_name": *"[^"]*"' | sed 's/"tag_name": *"\([^"]*\)"/\1/') |
| 77 | + # Fallback to wget. |
| 78 | + elif command -v wget &> /dev/null; then |
| 79 | + version=$(wget -qO- https://api.github.com/repos/SocketDev/socket-cli/releases/latest | grep -o '"tag_name": *"[^"]*"' | sed 's/"tag_name": *"\([^"]*\)"/\1/') |
| 80 | + else |
| 81 | + error "Neither curl nor wget found. Please install one of them." |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | + |
| 85 | + if [ -z "$version" ]; then |
| 86 | + error "Failed to fetch latest version from GitHub" |
| 87 | + exit 1 |
| 88 | + fi |
| 89 | + |
| 90 | + echo "$version" |
| 91 | +} |
| 92 | + |
| 93 | +# Calculate SHA256 hash of a string. |
| 94 | +calculate_hash() { |
| 95 | + local str="$1" |
| 96 | + |
| 97 | + if command -v sha256sum &> /dev/null; then |
| 98 | + echo -n "$str" | sha256sum | cut -d' ' -f1 |
| 99 | + elif command -v shasum &> /dev/null; then |
| 100 | + echo -n "$str" | shasum -a 256 | cut -d' ' -f1 |
| 101 | + else |
| 102 | + error "Neither sha256sum nor shasum found" |
| 103 | + exit 1 |
| 104 | + fi |
| 105 | +} |
| 106 | + |
| 107 | +# Download and install Socket CLI. |
| 108 | +install_socket_cli() { |
| 109 | + local platform |
| 110 | + local version |
| 111 | + local package_name |
| 112 | + local download_url |
| 113 | + local dlx_dir |
| 114 | + local package_hash |
| 115 | + local install_dir |
| 116 | + local binary_path |
| 117 | + local bin_dir |
| 118 | + local symlink_path |
| 119 | + |
| 120 | + info "Detecting platform..." |
| 121 | + platform=$(detect_platform) |
| 122 | + success "Platform detected: $platform" |
| 123 | + |
| 124 | + info "Fetching latest version..." |
| 125 | + version=$(get_latest_version) |
| 126 | + success "Latest version: $version" |
| 127 | + |
| 128 | + # Construct package name and download URL. |
| 129 | + package_name="@socketbin/cli-${platform}" |
| 130 | + download_url="https://github.com/SocketDev/socket-cli/releases/download/${version}/socketbin-cli-${platform}.tgz" |
| 131 | + |
| 132 | + info "Downloading Socket CLI from $download_url" |
| 133 | + |
| 134 | + # Create DLX directory structure. |
| 135 | + dlx_dir="${HOME}/.socket/_dlx" |
| 136 | + mkdir -p "$dlx_dir" |
| 137 | + |
| 138 | + # Calculate content hash for the package. |
| 139 | + package_hash=$(calculate_hash "${package_name}@${version}") |
| 140 | + install_dir="${dlx_dir}/${package_hash}" |
| 141 | + |
| 142 | + # Create installation directory. |
| 143 | + mkdir -p "$install_dir" |
| 144 | + |
| 145 | + # Download tarball to temporary location. |
| 146 | + local temp_tarball="${install_dir}/socket.tgz" |
| 147 | + |
| 148 | + if command -v curl &> /dev/null; then |
| 149 | + curl -fsSL -o "$temp_tarball" "$download_url" |
| 150 | + elif command -v wget &> /dev/null; then |
| 151 | + wget -qO "$temp_tarball" "$download_url" |
| 152 | + fi |
| 153 | + |
| 154 | + success "Downloaded tarball" |
| 155 | + |
| 156 | + # Extract tarball. |
| 157 | + info "Extracting binary..." |
| 158 | + tar -xzf "$temp_tarball" -C "$install_dir" |
| 159 | + |
| 160 | + # Find the binary (it's in package/bin/socket or package/bin/socket.exe). |
| 161 | + if [ "$platform" = "win32-x64" ] || [ "$platform" = "win32-arm64" ]; then |
| 162 | + binary_path="${install_dir}/package/bin/socket.exe" |
| 163 | + else |
| 164 | + binary_path="${install_dir}/package/bin/socket" |
| 165 | + fi |
| 166 | + |
| 167 | + if [ ! -f "$binary_path" ]; then |
| 168 | + error "Binary not found at expected path: $binary_path" |
| 169 | + exit 1 |
| 170 | + fi |
| 171 | + |
| 172 | + # Make binary executable (Unix-like systems). |
| 173 | + if [ "$platform" != "win32-x64" ] && [ "$platform" != "win32-arm64" ]; then |
| 174 | + chmod +x "$binary_path" |
| 175 | + |
| 176 | + # Clear macOS quarantine attribute. |
| 177 | + if [ "$platform" = "darwin-x64" ] || [ "$platform" = "darwin-arm64" ]; then |
| 178 | + xattr -d com.apple.quarantine "$binary_path" 2>/dev/null || true |
| 179 | + fi |
| 180 | + fi |
| 181 | + |
| 182 | + # Clean up tarball. |
| 183 | + rm "$temp_tarball" |
| 184 | + |
| 185 | + success "Installed to $binary_path" |
| 186 | + |
| 187 | + # Create symlink in user's local bin directory. |
| 188 | + bin_dir="${HOME}/.local/bin" |
| 189 | + mkdir -p "$bin_dir" |
| 190 | + symlink_path="${bin_dir}/socket" |
| 191 | + |
| 192 | + # Remove existing symlink if present. |
| 193 | + if [ -L "$symlink_path" ] || [ -f "$symlink_path" ]; then |
| 194 | + rm "$symlink_path" |
| 195 | + fi |
| 196 | + |
| 197 | + # Create symlink. |
| 198 | + ln -s "$binary_path" "$symlink_path" |
| 199 | + success "Created symlink: $symlink_path -> $binary_path" |
| 200 | + |
| 201 | + # Check if ~/.local/bin is in PATH. |
| 202 | + if [[ ":$PATH:" != *":${bin_dir}:"* ]]; then |
| 203 | + warning "~/.local/bin is not in your PATH" |
| 204 | + echo "" |
| 205 | + echo "Add it to your PATH by adding this to your shell profile (~/.bashrc, ~/.zshrc, etc.):" |
| 206 | + echo "" |
| 207 | + echo " export PATH=\"\$HOME/.local/bin:\$PATH\"" |
| 208 | + echo "" |
| 209 | + else |
| 210 | + success "~/.local/bin is already in your PATH" |
| 211 | + fi |
| 212 | + |
| 213 | + echo "" |
| 214 | + success "Socket CLI installed successfully!" |
| 215 | + echo "" |
| 216 | + info "Run 'socket --help' to get started" |
| 217 | + info "Run 'socket self-update' to update to the latest version" |
| 218 | +} |
| 219 | + |
| 220 | +# Main execution. |
| 221 | +main() { |
| 222 | + echo "" |
| 223 | + echo "Socket CLI Installer" |
| 224 | + echo "====================" |
| 225 | + echo "" |
| 226 | + |
| 227 | + install_socket_cli |
| 228 | +} |
| 229 | + |
| 230 | +main "$@" |
0 commit comments