-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
407 lines (345 loc) · 11.3 KB
/
install.sh
File metadata and controls
407 lines (345 loc) · 11.3 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
#!/bin/bash
# ee installer script
# Usage: curl -sSfL https://raw.githubusercontent.com/n1rna/ee-cli/main/install.sh | sh
set -e
# Configuration
REPO="n1rna/ee-cli"
BINARY_NAME="ee"
VERSION="" # Will be set to latest if empty
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Utility functions
log() { printf "${GREEN}[INFO]${NC} %s\n" "$1"; }
warn() { printf "${YELLOW}[WARN]${NC} %s\n" "$1"; }
error() { printf "${RED}[ERROR]${NC} %s\n" "$1" >&2; }
debug() { [ "${DEBUG:-}" ] && printf "${BLUE}[DEBUG]${NC} %s\n" "$1" || true; }
# Detect OS and architecture
detect_platform() {
local os arch
case "$(uname -s)" in
Linux*) os="linux" ;;
Darwin*) os="darwin" ;;
CYGWIN*|MINGW*|MSYS*) os="windows" ;;
*) error "Unsupported operating system: $(uname -s)"; exit 1 ;;
esac
case "$(uname -m)" in
x86_64|amd64) arch="amd64" ;;
arm64|aarch64) arch="arm64" ;;
armv7*) arch="arm" ;;
i386|i686) arch="386" ;;
*) error "Unsupported architecture: $(uname -m)"; exit 1 ;;
esac
PLATFORM="${os}-${arch}"
debug "Detected platform: $PLATFORM"
# Set binary suffix for Windows
if [ "$os" = "windows" ]; then
BINARY_SUFFIX=".exe"
else
BINARY_SUFFIX=""
fi
}
# Get the latest version from GitHub API
get_latest_version() {
if [ -n "$VERSION" ]; then
debug "Using specified version: $VERSION"
return
fi
log "Fetching latest version..."
if command -v curl >/dev/null 2>&1; then
VERSION=$(curl -s "https://api.github.com/repos/$REPO/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
elif command -v wget >/dev/null 2>&1; then
VERSION=$(wget -qO- "https://api.github.com/repos/$REPO/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
else
error "Neither curl nor wget is available. Please install one of them."
exit 1
fi
if [ -z "$VERSION" ]; then
error "Failed to get the latest version"
exit 1
fi
debug "Latest version: $VERSION"
}
# Check if version exists
check_version_exists() {
local url="https://api.github.com/repos/$REPO/releases/tags/$VERSION"
debug "Checking if version $VERSION exists..."
if command -v curl >/dev/null 2>&1; then
if ! curl -sf "$url" >/dev/null; then
error "Version $VERSION not found"
exit 1
fi
elif command -v wget >/dev/null 2>&1; then
if ! wget -q --spider "$url"; then
error "Version $VERSION not found"
exit 1
fi
fi
}
# Check if a directory is in the current PATH
is_in_path() {
case ":$PATH:" in
*":$1:"*) return 0 ;;
*) return 1 ;;
esac
}
# Find the best user-writable install directory
# Priority: user-writable directories already in PATH, then well-known user dirs
find_install_dir() {
# Well-known user-level bin directories, in order of preference
local candidates="
$HOME/.local/bin
$HOME/bin
$HOME/.cargo/bin
$HOME/go/bin
$HOME/.local/share/bin
"
# 1. Check if any well-known user directory is already in PATH and writable
for dir in $candidates; do
if is_in_path "$dir" && [ -d "$dir" ] && [ -w "$dir" ]; then
debug "Found user-writable directory in PATH: $dir"
echo "$dir"
return
fi
done
# 2. Scan PATH for any other user-writable directory (under $HOME)
local IFS=':'
for dir in $PATH; do
case "$dir" in
"$HOME"*)
if [ -d "$dir" ] && [ -w "$dir" ]; then
debug "Found user-writable directory in PATH: $dir"
echo "$dir"
return
fi
;;
esac
done
unset IFS
# 3. Check /usr/local/bin if writable (common on macOS)
if [ -w "/usr/local/bin" ]; then
debug "Using writable /usr/local/bin"
echo "/usr/local/bin"
return
fi
# 4. Create ~/.local/bin (XDG standard, most shells source it)
local fallback="$HOME/.local/bin"
mkdir -p "$fallback"
# Check if it's already in PATH after creation
if is_in_path "$fallback"; then
debug "Created $fallback (already in PATH)"
else
debug "Created $fallback (not yet in PATH)"
fi
echo "$fallback"
}
# Detect the user's shell profile file
detect_shell_profile() {
local shell_name
shell_name=$(basename "${SHELL:-/bin/sh}")
case "$shell_name" in
zsh)
if [ -f "$HOME/.zshrc" ]; then
echo "$HOME/.zshrc"
else
echo "$HOME/.zprofile"
fi
;;
bash)
if [ -f "$HOME/.bashrc" ]; then
echo "$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
echo "$HOME/.bash_profile"
else
echo "$HOME/.profile"
fi
;;
fish)
echo "$HOME/.config/fish/config.fish"
;;
*)
echo "$HOME/.profile"
;;
esac
}
# Suggest how to add a directory to PATH
suggest_path_update() {
local dir="$1"
local profile
profile=$(detect_shell_profile)
local shell_name
shell_name=$(basename "${SHELL:-/bin/sh}")
warn "Add $dir to your PATH by running:"
if [ "$shell_name" = "fish" ]; then
warn " fish_add_path $dir"
else
warn " echo 'export PATH=\"$dir:\$PATH\"' >> $profile"
fi
warn "Then restart your shell or run:"
if [ "$shell_name" = "fish" ]; then
warn " source $profile"
else
warn " source $profile"
fi
}
# Download checksums and verify
verify_checksum() {
local tmp_dir="$1"
local archive_name="$2"
if [ "${SKIP_CHECKSUM:-}" = "true" ]; then
warn "Skipping checksum verification"
return
fi
local checksums_url="https://github.com/$REPO/releases/download/$VERSION/checksums.txt"
log "Verifying checksum..."
# Download checksums
if command -v curl >/dev/null 2>&1; then
curl -sL "$checksums_url" -o "$tmp_dir/checksums.txt"
elif command -v wget >/dev/null 2>&1; then
wget -q "$checksums_url" -O "$tmp_dir/checksums.txt"
else
warn "Cannot verify checksum: neither curl nor wget available"
return
fi
# Verify checksum against the archive
if command -v sha256sum >/dev/null 2>&1; then
local expected_checksum=$(grep "$archive_name" "$tmp_dir/checksums.txt" | cut -d' ' -f1)
local actual_checksum=$(sha256sum "$tmp_dir/$archive_name" | cut -d' ' -f1)
if [ "$expected_checksum" != "$actual_checksum" ]; then
error "Checksum verification failed!"
error "Expected: $expected_checksum"
error "Actual: $actual_checksum"
exit 1
fi
log "Checksum verified"
elif command -v shasum >/dev/null 2>&1; then
local expected_checksum=$(grep "$archive_name" "$tmp_dir/checksums.txt" | cut -d' ' -f1)
local actual_checksum=$(shasum -a 256 "$tmp_dir/$archive_name" | cut -d' ' -f1)
if [ "$expected_checksum" != "$actual_checksum" ]; then
error "Checksum verification failed!"
error "Expected: $expected_checksum"
error "Actual: $actual_checksum"
exit 1
fi
log "Checksum verified"
else
warn "sha256sum/shasum not available - skipping checksum verification"
fi
}
# Download and install
install_binary() {
local tmp_dir="/tmp/ee-install-$$"
local binary_name="${BINARY_NAME}-${PLATFORM}${BINARY_SUFFIX}"
local archive_name="${binary_name}.tar.gz"
local download_url="https://github.com/$REPO/releases/download/$VERSION/$archive_name"
log "Downloading ee $VERSION for $PLATFORM..."
debug "Download URL: $download_url"
# Create temporary directory
mkdir -p "$tmp_dir"
trap 'rm -rf "$tmp_dir"' EXIT
# Download archive
if command -v curl >/dev/null 2>&1; then
if ! curl -sL "$download_url" -o "$tmp_dir/$archive_name"; then
error "Failed to download $archive_name"
exit 1
fi
elif command -v wget >/dev/null 2>&1; then
if ! wget -q "$download_url" -O "$tmp_dir/$archive_name"; then
error "Failed to download $archive_name"
exit 1
fi
fi
# Verify checksum before extraction
verify_checksum "$tmp_dir" "$archive_name"
# Extract binary from archive
log "Extracting archive..."
if ! tar xzf "$tmp_dir/$archive_name" -C "$tmp_dir"; then
error "Failed to extract $archive_name"
exit 1
fi
# Rename extracted binary to just the binary name
if [ -f "$tmp_dir/$binary_name" ]; then
mv "$tmp_dir/$binary_name" "$tmp_dir/$BINARY_NAME$BINARY_SUFFIX"
fi
# Make binary executable
chmod +x "$tmp_dir/$BINARY_NAME$BINARY_SUFFIX"
# Determine install location — prefer user-writable directories already in PATH
local install_dir
install_dir=$(find_install_dir)
# Install binary
log "Installing to $install_dir/$BINARY_NAME$BINARY_SUFFIX..."
if ! cp "$tmp_dir/$BINARY_NAME$BINARY_SUFFIX" "$install_dir/$BINARY_NAME$BINARY_SUFFIX"; then
error "Failed to install binary to $install_dir"
warn "You may need to run with sudo or choose a different install location"
exit 1
fi
# Verify installation
if "$install_dir/$BINARY_NAME$BINARY_SUFFIX" --version >/dev/null 2>&1; then
log "ee $VERSION installed successfully!"
log "Location: $install_dir/$BINARY_NAME$BINARY_SUFFIX"
# Check if binary is in PATH
if command -v "$BINARY_NAME" >/dev/null 2>&1; then
log "You can now use 'ee' from anywhere!"
else
warn "$install_dir is not in your PATH"
suggest_path_update "$install_dir"
fi
log ""
log "Get started with: ee --help"
log "Documentation: https://github.com/$REPO"
else
error "Installation verification failed"
exit 1
fi
}
# Main installation flow
main() {
log "🚀 Installing ee CLI tool..."
# Parse arguments
while [ $# -gt 0 ]; do
case $1 in
--version)
VERSION="$2"
shift 2
;;
--debug)
DEBUG=true
shift
;;
--skip-checksum)
SKIP_CHECKSUM=true
shift
;;
--help|-h)
cat << EOF
ee installer script
Usage: $0 [options]
Options:
--version VERSION Install specific version (default: latest)
--debug Enable debug output
--skip-checksum Skip checksum verification
--help, -h Show this help message
Examples:
$0 # Install latest version
$0 --version v1.0.0 # Install specific version
$0 --debug # Install with debug output
EOF
exit 0
;;
*)
error "Unknown option: $1"
error "Use --help for usage information"
exit 1
;;
esac
done
detect_platform
get_latest_version
check_version_exists
install_binary
}
# Run main function
main "$@"