-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall
More file actions
384 lines (323 loc) · 11 KB
/
install
File metadata and controls
384 lines (323 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/bash
# Offworld CLI Installer
# Usage: curl -fsSL https://offworld.sh/install | bash
#
# This script detects your OS and architecture, downloads the correct binary,
# verifies the checksum, and installs it to ~/.local/bin/ow
#
# Environment variables:
# OW_VERSION - Pin to specific version (e.g., v0.1.0)
# OW_CHANNEL - Release channel: stable or beta (default: stable)
# OW_BIN_DIR - Override bin directory for installation
set -euo pipefail
GITHUB_REPO="oscabriel/offworld"
BINARY_NAME="ow"
# Detect TTY for colors
if [ -t 1 ]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
else
RED='' GREEN='' YELLOW='' BLUE='' NC=''
fi
log() { echo -e "${GREEN}==>${NC} $1" >&2; }
info() { echo -e "${BLUE}::${NC} $1" >&2; }
warn() { echo -e "${YELLOW}Warning:${NC} $1" >&2; }
error() { echo -e "${RED}Error:${NC} $1" >&2; exit 1; }
# Check for required commands
check_deps() {
command -v curl >/dev/null 2>&1 || error "curl is required but not installed"
}
# Fetch with retry and timeout
fetch_with_retry() {
local url="$1"
local output="$2"
local attempts=3
local timeout_connect=10
local timeout_max=120
for i in $(seq 1 $attempts); do
if curl -fsSL --connect-timeout "$timeout_connect" --max-time "$timeout_max" "$url" -o "$output" 2>/dev/null; then
return 0
fi
if [ "$i" -lt "$attempts" ]; then
warn "Download failed, retrying ($i/$attempts)..."
sleep 2
fi
done
return 1
}
# Compute SHA256 checksum with fallbacks
compute_sha256() {
local file="$1"
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$file" | cut -d' ' -f1
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$file" | cut -d' ' -f1
elif command -v openssl >/dev/null 2>&1; then
openssl dgst -sha256 "$file" | awk '{print $NF}'
else
error "No SHA256 tool found (need sha256sum, shasum, or openssl)"
fi
}
# Detect OS
detect_os() {
local os
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "$os" in
darwin) echo "darwin" ;;
linux) echo "linux" ;;
freebsd) error "FreeBSD is not yet supported. See: https://github.com/${GITHUB_REPO}/issues" ;;
mingw*|msys*|cygwin*) error "Windows is not supported via this installer. Use npm instead:\n npm install -g offworld" ;;
*) error "Unsupported OS: $os" ;;
esac
}
# Detect architecture
detect_arch() {
local arch
arch="$(uname -m)"
case "$arch" in
x86_64|amd64) echo "x64" ;;
arm64|aarch64) echo "arm64" ;;
*) error "Unsupported architecture: $arch" ;;
esac
}
# Detect platform
detect_platform() {
local os arch
os=$(detect_os)
arch=$(detect_arch)
# Check for musl (Alpine, etc.) - not yet supported
if [ "$os" = "linux" ]; then
if ls /lib/ld-musl-*.so.1 >/dev/null 2>&1 || [ -f /etc/alpine-release ]; then
error "musl libc (Alpine) is not yet supported.\n Use npm instead:\n npm install -g offworld"
fi
fi
echo "${os}-${arch}"
}
# Find a writable bin directory that's in PATH
find_bin_dir() {
# If user explicitly set bin dir, use it
if [ -n "${OW_BIN_DIR:-}" ]; then
mkdir -p "$OW_BIN_DIR" 2>/dev/null || true
if [ -d "$OW_BIN_DIR" ] && [ -w "$OW_BIN_DIR" ]; then
echo "$OW_BIN_DIR"
return 0
else
warn "OW_BIN_DIR=$OW_BIN_DIR is not writable, searching PATH..."
fi
fi
# Priority order of common bin directories
local common_dirs=(
"$HOME/.local/bin"
"$HOME/bin"
"/usr/local/bin"
"/opt/homebrew/bin"
)
# Parse PATH into array
local path_dirs
IFS=':' read -ra path_dirs <<< "$PATH"
# Check if any common dir is already in PATH and writable
for dir in "${common_dirs[@]}"; do
for path_dir in "${path_dirs[@]}"; do
if [ "$dir" = "$path_dir" ]; then
if [ -d "$dir" ] && [ -w "$dir" ]; then
echo "$dir"
return 0
elif [ ! -e "$dir" ]; then
local parent
parent=$(dirname "$dir")
if [ -w "$parent" ]; then
mkdir -p "$dir" 2>/dev/null && echo "$dir" && return 0
fi
fi
fi
done
done
# Fallback: create ~/.local/bin (will need PATH modification)
mkdir -p "$HOME/.local/bin" 2>/dev/null
echo "$HOME/.local/bin"
return 1
}
# Detect user's shell and config file
detect_shell_config() {
local shell="${SHELL:-}"
if [ -n "$shell" ]; then
case "$shell" in
*/zsh) echo "zsh:$HOME/.zshrc" ;;
*/bash)
if [ -f "$HOME/.bashrc" ]; then
echo "bash:$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
echo "bash:$HOME/.bash_profile"
else
echo "bash:$HOME/.bashrc"
fi
;;
*/fish) echo "fish:$HOME/.config/fish/config.fish" ;;
*/sh) echo "sh:$HOME/.profile" ;;
*) echo "unknown:$HOME/.profile" ;;
esac
return
fi
# Fallback: check which shell configs exist
if [ -f "$HOME/.zshrc" ]; then
echo "zsh:$HOME/.zshrc"
elif [ -f "$HOME/.bashrc" ]; then
echo "bash:$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
echo "bash:$HOME/.bash_profile"
else
echo "unknown:$HOME/.profile"
fi
}
# Get latest release version
get_latest_version() {
local channel="${1:-stable}"
local api_url="https://api.github.com/repos/${GITHUB_REPO}/releases"
local tmp_file
tmp_file=$(mktemp)
info "Fetching releases (channel: $channel)..."
if ! fetch_with_retry "$api_url" "$tmp_file"; then
rm -f "$tmp_file"
error "Failed to fetch releases\n URL: $api_url"
fi
local response
response=$(cat "$tmp_file")
rm -f "$tmp_file"
if [ -z "$response" ] || [ "$response" = "[]" ]; then
error "No releases found\n Check: https://github.com/${GITHUB_REPO}/releases"
fi
local version=""
if [ "$channel" = "stable" ]; then
# Get first non-prerelease (skip alpha/beta/rc/dev tags)
version=$(echo "$response" | grep -o '"tag_name": *"[^"]*"' | cut -d'"' -f4 | grep -vE '(alpha|beta|rc|dev)' | head -1)
fi
# Fallback to first tag if no stable found or beta channel
if [ -z "$version" ]; then
version=$(echo "$response" | grep -o '"tag_name": *"[^"]*"' | head -1 | cut -d'"' -f4)
fi
echo "$version"
}
# Download and install binary
download_and_install() {
local platform="$1"
local version="$2"
local bin_dir="$3"
local release_url="https://github.com/${GITHUB_REPO}/releases/download/${version}"
local binary_url="${release_url}/ow-${platform}.tar.gz"
local checksums_url="${release_url}/checksums.txt"
local tmp_dir
tmp_dir=$(mktemp -d)
trap "rm -rf '$tmp_dir'" EXIT
log "Downloading offworld ${version} for ${platform}..."
if ! fetch_with_retry "$binary_url" "$tmp_dir/ow.tar.gz"; then
error "Failed to download binary\n URL: $binary_url"
fi
log "Downloading checksums..."
if ! fetch_with_retry "$checksums_url" "$tmp_dir/checksums.txt"; then
error "Failed to download checksums\n URL: $checksums_url"
fi
log "Verifying checksum..."
local expected_sha256
expected_sha256=$(grep "ow-${platform}.tar.gz" "$tmp_dir/checksums.txt" | cut -d' ' -f1)
if [ -z "$expected_sha256" ]; then
error "No checksum for platform: $platform\n See: https://github.com/${GITHUB_REPO}/releases/tag/${version}"
fi
local actual_sha256
actual_sha256=$(compute_sha256 "$tmp_dir/ow.tar.gz")
if [ "$actual_sha256" != "$expected_sha256" ]; then
error "Checksum mismatch!\n Expected: ${expected_sha256}\n Actual: ${actual_sha256}"
fi
info "Checksum verified: ${expected_sha256:0:16}..."
log "Extracting binary..."
tar -xzf "$tmp_dir/ow.tar.gz" -C "$tmp_dir" || error "Failed to extract archive"
mkdir -p "$bin_dir" || error "Failed to create install directory: $bin_dir"
mv "$tmp_dir/ow" "$bin_dir/$BINARY_NAME" || error "Failed to move binary"
chmod +x "$bin_dir/$BINARY_NAME" || error "Failed to make binary executable"
info "Installed to $bin_dir/$BINARY_NAME"
}
# Print shell profile instructions
print_path_instructions() {
local bin_dir="$1"
local shell_info rc_file shell_name
shell_info=$(detect_shell_config)
shell_name="${shell_info%%:*}"
rc_file="${shell_info#*:}"
warn "$bin_dir is not in your PATH"
echo ""
case "$shell_name" in
fish)
echo "Add to $rc_file:"
echo ""
echo " fish_add_path $bin_dir"
echo ""
echo "Or run now:"
echo ""
echo " echo 'fish_add_path $bin_dir' >> $rc_file && source $rc_file"
;;
zsh|bash|sh|unknown)
echo "Add to $rc_file:"
echo ""
echo " export PATH=\"$bin_dir:\$PATH\""
echo ""
echo "Or run now:"
echo ""
echo " echo 'export PATH=\"$bin_dir:\$PATH\"' >> $rc_file && source $rc_file"
;;
esac
}
main() {
local channel="${OW_CHANNEL:-stable}"
echo ""
echo " ██████╗ ███████╗███████╗██╗ ██╗ ██████╗ ██████╗ ██╗ ██████╗ "
echo "██╔═══██╗██╔════╝██╔════╝██║ ██║██╔═══██╗██╔══██╗██║ ██╔══██╗"
echo "██║ ██║█████╗ █████╗ ██║ █╗ ██║██║ ██║██████╔╝██║ ██║ ██║"
echo "██║ ██║██╔══╝ ██╔══╝ ██║███╗██║██║ ██║██╔══██╗██║ ██║ ██║"
echo "╚██████╔╝██║ ██║ ╚███╔███╔╝╚██████╔╝██║ ██║███████╗██████╔╝"
echo " ╚═════╝ ╚═╝ ╚═╝ ╚══╝╚══╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝╚═════╝ "
echo ""
log "Installing Offworld CLI..."
check_deps
local platform version bin_dir needs_path_update=false
platform=$(detect_platform)
log "Detected platform: $platform"
# Find writable bin directory in PATH
if ! bin_dir=$(find_bin_dir); then
needs_path_update=true
fi
info "Bin directory: $bin_dir"
# Version: pinned or latest
if [ -n "${OW_VERSION:-}" ]; then
version="$OW_VERSION"
log "Installing pinned version: $version"
else
version=$(get_latest_version "$channel")
if [ -z "$version" ]; then
if [ "$channel" = "stable" ]; then
error "No stable releases found. Try:\n OW_CHANNEL=beta curl -fsSL https://offworld.sh/install | bash"
else
error "No releases found for channel '$channel'\n Check: https://github.com/${GITHUB_REPO}/releases"
fi
fi
log "Latest version: $version (channel: $channel)"
fi
download_and_install "$platform" "$version" "$bin_dir"
echo ""
log "Installation complete!"
# Print PATH instructions if needed
if [ "$needs_path_update" = true ]; then
echo ""
print_path_instructions "$bin_dir"
fi
echo ""
echo "Get started with:"
echo ""
echo " ow --help # Show help"
echo " ow pull owner/repo # Clone and analyze a repo"
echo ""
echo "Documentation: https://offworld.sh/docs"
echo ""
}
main "$@"