-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·417 lines (358 loc) · 12.8 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·417 lines (358 loc) · 12.8 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/bin/sh
# shellcheck disable=SC3043 # 'local' is widely supported in practice (dash, ash, busybox)
# AutoSpec Installer
# Usage: curl -fsSL https://raw.githubusercontent.com/ariel-frischer/autospec/main/install.sh | sh
#
# Environment variables:
# AUTOSPEC_INSTALL_DIR - Installation directory (default: ~/.local/bin)
# AUTOSPEC_VERSION - Specific version to install (default: latest)
set -eu
# Configuration
GITHUB_REPO="ariel-frischer/autospec"
BINARY_NAME="autospec"
DEFAULT_INSTALL_DIR="$HOME/.local/bin"
# Colors (disabled if not a terminal)
# Use \e escapes with printf %b for portable POSIX color support
if [ -t 1 ]; then
RED='\e[0;31m'
GREEN='\e[0;32m'
YELLOW='\e[0;33m'
BLUE='\e[0;34m'
NC='\e[0m'
else
RED=''
GREEN=''
YELLOW=''
BLUE=''
NC=''
fi
# Logging functions (use %b to interpret color escapes, output to stderr)
info() {
printf '%b==>%b %s\n' "${BLUE}" "${NC}" "$1" >&2
}
success() {
printf '%b==>%b %s\n' "${GREEN}" "${NC}" "$1" >&2
}
warn() {
printf '%bWarning:%b %s\n' "${YELLOW}" "${NC}" "$1" >&2
}
error() {
printf '%bError:%b %s\n' "${RED}" "${NC}" "$1" >&2
exit 1
}
# Detect OS
detect_os() {
case "$(uname -s)" in
Linux*) echo "Linux" ;;
Darwin*) echo "Darwin" ;;
MINGW*|MSYS*|CYGWIN*)
printf '%bWindows Detected%b\n\n' "${YELLOW}" "${NC}" >&2
printf 'autospec requires WSL (Windows Subsystem for Linux).\n\n' >&2
printf 'Install WSL and try again:\n\n' >&2
printf ' 1. Open PowerShell as Administrator and run:\n' >&2
printf ' %bwsl --install%b\n\n' "${GREEN}" "${NC}" >&2
printf ' 2. Restart your computer\n\n' >&2
printf ' 3. Open the WSL terminal and re-run this installer\n\n' >&2
exit 1
;;
*) error "Unsupported operating system: $(uname -s)" ;;
esac
}
# Detect architecture
detect_arch() {
case "$(uname -m)" in
x86_64|amd64) echo "x86_64" ;;
aarch64|arm64) echo "arm64" ;;
*) error "Unsupported architecture: $(uname -m)" ;;
esac
}
# Check for required commands
check_dependencies() {
for cmd in curl tar; do
if ! command -v "$cmd" >/dev/null 2>&1; then
error "Required command not found: $cmd"
fi
done
}
# Get latest release version from GitHub
get_latest_version() {
local latest_url="https://api.github.com/repos/${GITHUB_REPO}/releases/latest"
local version
version=$(curl -fsSL "$latest_url" 2>/dev/null | grep '"tag_name"' | sed -E 's/.*"tag_name": *"([^"]+)".*/\1/')
if [ -z "$version" ]; then
error "Failed to fetch latest version from GitHub. Check your internet connection."
fi
echo "$version"
}
# Download and verify checksum
download_and_verify() {
local version="$1"
local os="$2"
local arch="$3"
local tmp_dir="$4"
# Construct archive name (matches goreleaser template)
local archive_name="autospec_${version#v}_${os}_${arch}.tar.gz"
local download_url="https://github.com/${GITHUB_REPO}/releases/download/${version}/${archive_name}"
local checksum_url="https://github.com/${GITHUB_REPO}/releases/download/${version}/checksums.txt"
info "Downloading ${archive_name}..."
# Use progress bar (-#) if terminal, silent (-s) otherwise
local curl_opts="-fSL"
if [ -t 2 ]; then
curl_opts="-f#L"
fi
if ! curl $curl_opts -o "${tmp_dir}/${archive_name}" "$download_url"; then
error "Failed to download ${archive_name}. Check if version ${version} exists."
fi
# Try to verify checksum
if curl -fsSL -o "${tmp_dir}/checksums.txt" "$checksum_url" 2>/dev/null; then
info "Verifying checksum..."
local expected_checksum
expected_checksum=$(grep "${archive_name}" "${tmp_dir}/checksums.txt" | awk '{print $1}')
if [ -n "$expected_checksum" ]; then
local actual_checksum
if command -v sha256sum >/dev/null 2>&1; then
actual_checksum=$(sha256sum "${tmp_dir}/${archive_name}" | awk '{print $1}')
elif command -v shasum >/dev/null 2>&1; then
actual_checksum=$(shasum -a 256 "${tmp_dir}/${archive_name}" | awk '{print $1}')
else
warn "sha256sum/shasum not found, skipping checksum verification"
echo "${tmp_dir}/${archive_name}"
return
fi
if [ "$expected_checksum" != "$actual_checksum" ]; then
error "Checksum verification failed!\nExpected: ${expected_checksum}\nActual: ${actual_checksum}"
fi
success "Checksum verified"
else
warn "Checksum not found for ${archive_name}, skipping verification"
fi
else
warn "Could not download checksums.txt, skipping verification"
fi
echo "${tmp_dir}/${archive_name}"
}
# Extract binary from archive
extract_binary() {
local archive_path="$1"
local tmp_dir="$2"
info "Extracting archive..."
tar -xzf "$archive_path" -C "$tmp_dir"
if [ ! -f "${tmp_dir}/${BINARY_NAME}" ]; then
error "Binary '${BINARY_NAME}' not found in archive"
fi
echo "${tmp_dir}/${BINARY_NAME}"
}
# Install binary to destination
install_binary() {
local binary_path="$1"
local install_dir="$2"
# Create install directory if it doesn't exist
if [ ! -d "$install_dir" ]; then
info "Creating directory ${install_dir}..."
if ! mkdir -p "$install_dir" 2>/dev/null; then
warn "Cannot create ${install_dir} without elevated privileges"
info "Trying with sudo..."
sudo mkdir -p "$install_dir"
fi
fi
# Check if we can write to install directory
if [ -w "$install_dir" ]; then
mv "$binary_path" "${install_dir}/${BINARY_NAME}"
chmod +x "${install_dir}/${BINARY_NAME}"
else
info "Elevated privileges required to install to ${install_dir}"
sudo mv "$binary_path" "${install_dir}/${BINARY_NAME}"
sudo chmod +x "${install_dir}/${BINARY_NAME}"
fi
}
# Get version from installed binary
get_installed_version() {
local binary_path="$1"
if [ -x "$binary_path" ]; then
"$binary_path" version 2>/dev/null | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+' | head -1 || echo ""
else
echo ""
fi
}
# Compare versions (returns 0 if v1 >= v2)
version_gte() {
local v1="$1"
local v2="$2"
# Strip 'v' prefix for comparison
v1="${v1#v}"
v2="${v2#v}"
# Use sort -V if available, otherwise simple string comparison
if printf '%s\n%s' "$v2" "$v1" | sort -V -C 2>/dev/null; then
return 0
fi
# Fallback: simple string comparison (works for same-length versions)
[ "$v1" = "$v2" ] || [ "$(printf '%s\n%s' "$v1" "$v2" | sort -V | tail -1)" = "$v1" ]
}
# Clean up old backups, keeping the most recent ones
cleanup_old_backups() {
local install_dir="$1"
local keep_count="${2:-3}"
local backup_pattern="${install_dir}/${BINARY_NAME}.backup.*"
# Count existing backups
# shellcheck disable=SC2086
local backup_count
backup_count=$(ls -1 $backup_pattern 2>/dev/null | wc -l)
if [ "$backup_count" -gt "$keep_count" ]; then
# Remove oldest backups, keep most recent $keep_count
# shellcheck disable=SC2086
ls -1t $backup_pattern 2>/dev/null | tail -n +"$((keep_count + 1))" | while read -r old_backup; do
rm -f "$old_backup"
info "Removed old backup: $(basename "$old_backup")"
done
fi
}
# Check if binary is in PATH
check_path() {
local install_dir="$1"
case ":$PATH:" in
*":${install_dir}:"*) return 0 ;;
*) return 1 ;;
esac
}
# Main installation function
main() {
echo ""
printf '%b%s\n' "${GREEN}" '▄▀█ █ █ ▀█▀ █▀█ █▀ █▀█ █▀▀ █▀▀'
printf '%s%b\n' '█▀█ █▄█ █ █▄█ ▄█ █▀▀ ██▄ █▄▄' "${NC}"
echo " Installer"
echo ""
# Check dependencies
check_dependencies
# Detect platform
local os
local arch
os=$(detect_os)
arch=$(detect_arch)
info "Detected platform: ${os}/${arch}"
# Determine version to install
local version="${AUTOSPEC_VERSION:-}"
if [ -z "$version" ]; then
info "Fetching latest version..."
version=$(get_latest_version)
fi
info "Installing version: ${version}"
# Determine install directory
local install_dir="${AUTOSPEC_INSTALL_DIR:-$DEFAULT_INSTALL_DIR}"
info "Install directory: ${install_dir}"
# Check for existing installation
local target_binary="${install_dir}/${BINARY_NAME}"
local existing_version=""
local backup_path=""
if [ -f "$target_binary" ]; then
existing_version=$(get_installed_version "$target_binary")
if [ -n "$existing_version" ]; then
info "Found existing installation: ${existing_version}"
# Check if already up-to-date or newer
if version_gte "$existing_version" "$version"; then
success "Already up-to-date (installed: ${existing_version}, requested: ${version})"
echo ""
echo "To force reinstall, remove the existing binary first:"
echo " rm ${target_binary}"
echo ""
exit 0
fi
info "Upgrading from ${existing_version} to ${version}"
else
info "Found existing installation (unknown version)"
fi
# Create timestamped backup
backup_path="${target_binary}.backup.$(date +%Y%m%d_%H%M%S)"
info "Creating backup at ${backup_path}..."
if [ -w "$install_dir" ]; then
cp "$target_binary" "$backup_path"
else
sudo cp "$target_binary" "$backup_path"
fi
success "Backup created"
# Clean up old backups (keep last 3)
cleanup_old_backups "$install_dir" 3
fi
# Create temporary directory with cleanup on exit or interrupt
local tmp_dir
tmp_dir=$(mktemp -d)
cleanup() { rm -rf "$tmp_dir"; }
trap cleanup EXIT
trap 'warn "Interrupted, cleaning up..."; cleanup; exit 1' INT TERM
# Download and verify
local archive_path
archive_path=$(download_and_verify "$version" "$os" "$arch" "$tmp_dir")
# Extract
local binary_path
binary_path=$(extract_binary "$archive_path" "$tmp_dir")
# Install
info "Installing to ${install_dir}..."
install_binary "$binary_path" "$install_dir"
# Verify installation works
info "Verifying installation..."
if ! "${target_binary}" version >/dev/null 2>&1; then
warn "Installed binary failed verification!"
if [ -n "$backup_path" ] && [ -f "$backup_path" ]; then
warn "Restoring from backup..."
if [ -w "$install_dir" ]; then
mv "$backup_path" "$target_binary"
else
sudo mv "$backup_path" "$target_binary"
fi
error "Installation failed. Previous version restored from backup."
else
error "Installation failed. No backup available to restore."
fi
fi
success "Verification passed"
echo ""
success "Successfully installed ${BINARY_NAME} ${version} to ${install_dir}/${BINARY_NAME}"
if [ -n "$existing_version" ]; then
success "Upgraded from ${existing_version} to ${version}"
fi
echo ""
# Check PATH
if check_path "$install_dir"; then
success "${install_dir} is already in your PATH"
else
warn "${install_dir} is NOT in your PATH"
echo ""
echo "Add it to your shell config:"
echo ""
echo " # Bash (~/.bashrc or ~/.bash_profile)"
echo " export PATH=\"${install_dir}:\$PATH\""
echo ""
echo " # Zsh (~/.zshrc)"
echo " export PATH=\"${install_dir}:\$PATH\""
echo ""
echo " # Fish (~/.config/fish/config.fish)"
echo " fish_add_path ${install_dir}"
echo ""
echo "Then reload your shell:"
echo " source ~/.bashrc # Bash"
echo " source ~/.zshrc # Zsh"
echo " exec fish # Fish"
echo ""
echo "Or start a new terminal session."
echo ""
fi
# Verify installation
if command -v "$BINARY_NAME" >/dev/null 2>&1; then
echo "Verify installation:"
echo ""
echo " ${BINARY_NAME} version"
echo " ${BINARY_NAME} doctor"
echo ""
fi
echo "Get started:"
echo ""
echo " ${BINARY_NAME} init # Initialize configuration"
echo " ${BINARY_NAME} --help # Show available commands"
echo ""
echo "Documentation: https://github.com/${GITHUB_REPO}"
echo ""
# Clean up temp dir and clear trap (tmp_dir is local, would be unbound after function exits)
rm -rf "$tmp_dir"
trap - EXIT INT TERM
}
# Run main
main "$@"