|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +REPO="GareArc/opencode-sync" |
| 5 | +BINARY_NAME="opencode-sync" |
| 6 | +INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}" |
| 7 | + |
| 8 | +# Colors for output |
| 9 | +RED='\033[0;31m' |
| 10 | +GREEN='\033[0;32m' |
| 11 | +YELLOW='\033[1;33m' |
| 12 | +BLUE='\033[0;34m' |
| 13 | +NC='\033[0m' # No Color |
| 14 | + |
| 15 | +info() { |
| 16 | + echo -e "${BLUE}==>${NC} $1" |
| 17 | +} |
| 18 | + |
| 19 | +success() { |
| 20 | + echo -e "${GREEN}==>${NC} $1" |
| 21 | +} |
| 22 | + |
| 23 | +warn() { |
| 24 | + echo -e "${YELLOW}==>${NC} $1" |
| 25 | +} |
| 26 | + |
| 27 | +error() { |
| 28 | + echo -e "${RED}==>${NC} $1" >&2 |
| 29 | + exit 1 |
| 30 | +} |
| 31 | + |
| 32 | +detect_os() { |
| 33 | + local os |
| 34 | + os="$(uname -s)" |
| 35 | + case "$os" in |
| 36 | + Linux*) echo "linux" ;; |
| 37 | + Darwin*) echo "darwin" ;; |
| 38 | + MINGW*|MSYS*|CYGWIN*) echo "windows" ;; |
| 39 | + *) error "Unsupported operating system: $os" ;; |
| 40 | + esac |
| 41 | +} |
| 42 | + |
| 43 | +detect_arch() { |
| 44 | + local arch |
| 45 | + arch="$(uname -m)" |
| 46 | + case "$arch" in |
| 47 | + x86_64|amd64) echo "amd64" ;; |
| 48 | + arm64|aarch64) echo "arm64" ;; |
| 49 | + *) error "Unsupported architecture: $arch" ;; |
| 50 | + esac |
| 51 | +} |
| 52 | + |
| 53 | +get_latest_version() { |
| 54 | + local version |
| 55 | + version=$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') |
| 56 | + if [ -z "$version" ]; then |
| 57 | + error "Failed to fetch latest version" |
| 58 | + fi |
| 59 | + echo "$version" |
| 60 | +} |
| 61 | + |
| 62 | +download_binary() { |
| 63 | + local os="$1" |
| 64 | + local arch="$2" |
| 65 | + local version="$3" |
| 66 | + local ext="" |
| 67 | + local archive_ext="tar.gz" |
| 68 | + |
| 69 | + if [ "$os" = "windows" ]; then |
| 70 | + ext=".exe" |
| 71 | + archive_ext="zip" |
| 72 | + fi |
| 73 | + |
| 74 | + local filename="${BINARY_NAME}_${version#v}_${os}_${arch}.${archive_ext}" |
| 75 | + local url="https://github.com/${REPO}/releases/download/${version}/${filename}" |
| 76 | + local tmp_dir |
| 77 | + tmp_dir=$(mktemp -d) |
| 78 | + |
| 79 | + info "Downloading ${BINARY_NAME} ${version} for ${os}/${arch}..." |
| 80 | + |
| 81 | + if ! curl -fsSL "$url" -o "${tmp_dir}/${filename}"; then |
| 82 | + rm -rf "$tmp_dir" |
| 83 | + error "Failed to download from ${url}" |
| 84 | + fi |
| 85 | + |
| 86 | + info "Extracting..." |
| 87 | + cd "$tmp_dir" |
| 88 | + |
| 89 | + if [ "$archive_ext" = "zip" ]; then |
| 90 | + if command -v unzip &> /dev/null; then |
| 91 | + unzip -q "$filename" |
| 92 | + else |
| 93 | + error "unzip is required to extract Windows binaries" |
| 94 | + fi |
| 95 | + else |
| 96 | + tar -xzf "$filename" |
| 97 | + fi |
| 98 | + |
| 99 | + echo "${tmp_dir}/${BINARY_NAME}${ext}" |
| 100 | +} |
| 101 | + |
| 102 | +install_binary() { |
| 103 | + local binary_path="$1" |
| 104 | + local install_path="${INSTALL_DIR}/${BINARY_NAME}" |
| 105 | + |
| 106 | + # Check if we need sudo |
| 107 | + if [ -w "$INSTALL_DIR" ]; then |
| 108 | + mv "$binary_path" "$install_path" |
| 109 | + chmod +x "$install_path" |
| 110 | + else |
| 111 | + info "Requesting sudo to install to ${INSTALL_DIR}..." |
| 112 | + sudo mv "$binary_path" "$install_path" |
| 113 | + sudo chmod +x "$install_path" |
| 114 | + fi |
| 115 | + |
| 116 | + success "Installed ${BINARY_NAME} to ${install_path}" |
| 117 | +} |
| 118 | + |
| 119 | +verify_installation() { |
| 120 | + if command -v "$BINARY_NAME" &> /dev/null; then |
| 121 | + success "Installation verified!" |
| 122 | + echo "" |
| 123 | + "$BINARY_NAME" version |
| 124 | + else |
| 125 | + warn "Installation complete, but ${BINARY_NAME} is not in PATH" |
| 126 | + warn "Add ${INSTALL_DIR} to your PATH or run: ${INSTALL_DIR}/${BINARY_NAME}" |
| 127 | + fi |
| 128 | +} |
| 129 | + |
| 130 | +check_dependencies() { |
| 131 | + if ! command -v curl &> /dev/null; then |
| 132 | + error "curl is required but not installed" |
| 133 | + fi |
| 134 | + |
| 135 | + if ! command -v tar &> /dev/null; then |
| 136 | + error "tar is required but not installed" |
| 137 | + fi |
| 138 | +} |
| 139 | + |
| 140 | +create_install_dir() { |
| 141 | + if [ ! -d "$INSTALL_DIR" ]; then |
| 142 | + info "Creating install directory: ${INSTALL_DIR}" |
| 143 | + if [ -w "$(dirname "$INSTALL_DIR")" ]; then |
| 144 | + mkdir -p "$INSTALL_DIR" |
| 145 | + else |
| 146 | + sudo mkdir -p "$INSTALL_DIR" |
| 147 | + fi |
| 148 | + fi |
| 149 | +} |
| 150 | + |
| 151 | +main() { |
| 152 | + echo "" |
| 153 | + echo " ┌─────────────────────────────────────┐" |
| 154 | + echo " │ opencode-sync installer │" |
| 155 | + echo " └─────────────────────────────────────┘" |
| 156 | + echo "" |
| 157 | + |
| 158 | + check_dependencies |
| 159 | + |
| 160 | + local os arch version binary_path |
| 161 | + os=$(detect_os) |
| 162 | + arch=$(detect_arch) |
| 163 | + |
| 164 | + info "Detected: ${os}/${arch}" |
| 165 | + |
| 166 | + # Allow version override |
| 167 | + version="${VERSION:-$(get_latest_version)}" |
| 168 | + |
| 169 | + info "Version: ${version}" |
| 170 | + |
| 171 | + # Handle user-local installation if /usr/local/bin is not writable |
| 172 | + if [ ! -w "$INSTALL_DIR" ] && [ ! -w "$(dirname "$INSTALL_DIR")" ]; then |
| 173 | + if [ -z "$SUDO_USER" ] && [ "$(id -u)" -ne 0 ]; then |
| 174 | + # Try user-local installation first |
| 175 | + local user_bin="$HOME/.local/bin" |
| 176 | + warn "${INSTALL_DIR} is not writable" |
| 177 | + info "Attempting to install to ${user_bin} instead..." |
| 178 | + INSTALL_DIR="$user_bin" |
| 179 | + fi |
| 180 | + fi |
| 181 | + |
| 182 | + create_install_dir |
| 183 | + |
| 184 | + binary_path=$(download_binary "$os" "$arch" "$version") |
| 185 | + install_binary "$binary_path" |
| 186 | + |
| 187 | + # Cleanup |
| 188 | + rm -rf "$(dirname "$binary_path")" |
| 189 | + |
| 190 | + echo "" |
| 191 | + verify_installation |
| 192 | + |
| 193 | + echo "" |
| 194 | + success "Done! Run 'opencode-sync' to get started." |
| 195 | + echo "" |
| 196 | + echo " Quick start:" |
| 197 | + echo " opencode-sync setup # First-time setup" |
| 198 | + echo " opencode-sync sync # Sync your configs" |
| 199 | + echo " opencode-sync --help # Show all commands" |
| 200 | + echo "" |
| 201 | +} |
| 202 | + |
| 203 | +main "$@" |
0 commit comments