Skip to content

Commit a3837c2

Browse files
committed
feat: add install.sh for curl-based installation
Add shell script for easy installation via: curl -fsSL https://raw.githubusercontent.com/GareArc/opencode-sync/main/install.sh | bash Features: - Auto-detect OS (Linux, macOS, Windows/WSL) - Auto-detect architecture (amd64, arm64) - Download from GitHub releases - Install to /usr/local/bin or ~/.local/bin - Version override with VERSION env var - Colorized output and error handling
1 parent 3e9279a commit a3837c2

File tree

2 files changed

+223
-2
lines changed

2 files changed

+223
-2
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,34 @@ Sync your OpenCode configurations across machines via Git, with optional encrypt
1212

1313
## Installation
1414

15-
### From source
15+
### Quick Install (Recommended)
16+
17+
```bash
18+
curl -fsSL https://raw.githubusercontent.com/GareArc/opencode-sync/main/install.sh | bash
19+
```
20+
21+
Or with a specific version:
22+
23+
```bash
24+
VERSION=v0.1.0 curl -fsSL https://raw.githubusercontent.com/GareArc/opencode-sync/main/install.sh | bash
25+
```
26+
27+
### From Source
1628

1729
```bash
1830
go install github.com/GareArc/opencode-sync@latest
1931
```
2032

21-
### From binary
33+
### From Binary
2234

2335
Download the latest release from the [releases page](https://github.com/GareArc/opencode-sync/releases).
2436

37+
### Homebrew (coming soon)
38+
39+
```bash
40+
brew install GareArc/tap/opencode-sync
41+
```
42+
2543
## Quick Start
2644

2745
```bash

install.sh

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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

Comments
 (0)