-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
396 lines (339 loc) · 9.91 KB
/
Copy pathinstall.sh
File metadata and controls
396 lines (339 loc) · 9.91 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
#!/bin/bash
# Wonopcode installer script
# Usage: curl -fsSL https://raw.githubusercontent.com/wonop-io/wonopcode/main/install.sh | bash
# or: curl -fsSL https://raw.githubusercontent.com/wonop-io/wonopcode/main/install.sh | bash -s -- --version v0.1.0
#
# Environment variables:
# WONOPCODE_INSTALL_DIR - Installation directory (default: ~/.local/bin or /usr/local/bin)
# WONOPCODE_VERSION - Specific version to install (default: latest)
set -euo pipefail
# Configuration
REPO="wonop-io/wonopcode"
BINARY_NAME="wonopcode"
GITHUB_API="https://api.github.com"
GITHUB_RELEASES="https://github.com/${REPO}/releases"
# Global temp directory (for cleanup trap)
TMP_DIR=""
# Colors (disabled if not a terminal)
if [ -t 1 ]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
BOLD='\033[1m'
NC='\033[0m' # No Color
else
RED=''
GREEN=''
YELLOW=''
BLUE=''
BOLD=''
NC=''
fi
# Logging functions
info() {
echo -e "${BLUE}==>${NC} ${BOLD}$1${NC}"
}
success() {
echo -e "${GREEN}==>${NC} ${BOLD}$1${NC}"
}
warn() {
echo -e "${YELLOW}warning:${NC} $1"
}
error() {
echo -e "${RED}error:${NC} $1" >&2
}
die() {
error "$1"
exit 1
}
# Parse arguments
VERSION="${WONOPCODE_VERSION:-}"
INSTALL_DIR="${WONOPCODE_INSTALL_DIR:-}"
while [[ $# -gt 0 ]]; do
case $1 in
--version|-v)
VERSION="$2"
shift 2
;;
--dir|-d)
INSTALL_DIR="$2"
shift 2
;;
--help|-h)
echo "Wonopcode Installer"
echo ""
echo "Usage: curl -fsSL https://raw.githubusercontent.com/wonop-io/wonopcode/main/install.sh | bash"
echo " or: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -v, --version VERSION Install a specific version (default: latest)"
echo " -d, --dir DIR Installation directory"
echo " -h, --help Show this help message"
echo ""
echo "Environment variables:"
echo " WONOPCODE_VERSION Specific version to install"
echo " WONOPCODE_INSTALL_DIR Installation directory"
exit 0
;;
*)
warn "Unknown option: $1"
shift
;;
esac
done
# Detect OS and architecture
detect_platform() {
local os arch
os="$(uname -s)"
arch="$(uname -m)"
case "$os" in
Linux*)
os="linux"
;;
Darwin*)
os="darwin"
;;
MINGW*|MSYS*|CYGWIN*)
os="windows"
;;
*)
die "Unsupported operating system: $os"
;;
esac
case "$arch" in
x86_64|amd64)
arch="x86_64"
;;
arm64|aarch64)
arch="aarch64"
;;
*)
die "Unsupported architecture: $arch"
;;
esac
echo "${os}-${arch}"
}
# Get the latest release version from GitHub
get_latest_version() {
local latest_url="${GITHUB_API}/repos/${REPO}/releases/latest"
if command -v curl &> /dev/null; then
curl -fsSL "$latest_url" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/'
elif command -v wget &> /dev/null; then
wget -qO- "$latest_url" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/'
else
die "Neither curl nor wget found. Please install one of them."
fi
}
# Download a file
download() {
local url="$1"
local output="$2"
info "Downloading from $url"
if command -v curl &> /dev/null; then
curl -fsSL "$url" -o "$output"
elif command -v wget &> /dev/null; then
wget -q "$url" -O "$output"
else
die "Neither curl nor wget found. Please install one of them."
fi
}
# Verify checksum
verify_checksum() {
local file="$1"
local checksums_file="$2"
local expected_name="$3"
if [ ! -f "$checksums_file" ]; then
warn "Checksums file not found, skipping verification"
return 0
fi
local expected_sum
expected_sum=$(grep "$expected_name" "$checksums_file" | awk '{print $1}')
if [ -z "$expected_sum" ]; then
warn "Checksum not found for $expected_name, skipping verification"
return 0
fi
local actual_sum
if command -v sha256sum &> /dev/null; then
actual_sum=$(sha256sum "$file" | awk '{print $1}')
elif command -v shasum &> /dev/null; then
actual_sum=$(shasum -a 256 "$file" | awk '{print $1}')
else
warn "sha256sum/shasum not found, skipping checksum verification"
return 0
fi
if [ "$expected_sum" != "$actual_sum" ]; then
die "Checksum verification failed!
Expected: $expected_sum
Actual: $actual_sum"
fi
info "Checksum verified"
}
# Extract the archive
extract() {
local archive="$1"
local dest="$2"
case "$archive" in
*.tar.gz|*.tgz)
tar -xzf "$archive" -C "$dest"
;;
*.zip)
unzip -q "$archive" -d "$dest"
;;
*)
die "Unknown archive format: $archive"
;;
esac
}
# Determine installation directory
get_install_dir() {
if [ -n "$INSTALL_DIR" ]; then
echo "$INSTALL_DIR"
return
fi
# Prefer ~/.local/bin if it exists or can be created
local local_bin="$HOME/.local/bin"
if [ -d "$local_bin" ] || mkdir -p "$local_bin" 2>/dev/null; then
echo "$local_bin"
return
fi
# Fall back to /usr/local/bin (requires sudo)
echo "/usr/local/bin"
}
# Check if directory is in PATH
check_path() {
local dir="$1"
if [[ ":$PATH:" != *":$dir:"* ]]; then
warn "$dir is not in your PATH"
echo ""
echo "Add it to your shell configuration:"
echo ""
local shell_name
shell_name=$(basename "$SHELL")
case "$shell_name" in
bash)
echo " echo 'export PATH=\"$dir:\$PATH\"' >> ~/.bashrc"
echo " source ~/.bashrc"
;;
zsh)
echo " echo 'export PATH=\"$dir:\$PATH\"' >> ~/.zshrc"
echo " source ~/.zshrc"
;;
fish)
echo " fish_add_path $dir"
;;
*)
echo " export PATH=\"$dir:\$PATH\""
;;
esac
echo ""
fi
}
# Main installation function
main() {
echo ""
echo -e "${BOLD}Wonopcode Installer${NC}"
echo ""
# Detect platform
local platform
platform=$(detect_platform)
info "Detected platform: $platform"
# Get version
if [ -z "$VERSION" ]; then
info "Fetching latest version..."
VERSION=$(get_latest_version)
if [ -z "$VERSION" ]; then
die "Failed to determine latest version"
fi
fi
info "Version: $VERSION"
# Determine artifact name and extension
local artifact_name extension
case "$platform" in
linux-x86_64)
artifact_name="wonopcode-linux-x86_64"
extension="tar.gz"
;;
linux-aarch64)
artifact_name="wonopcode-linux-aarch64"
extension="tar.gz"
;;
darwin-x86_64)
artifact_name="wonopcode-darwin-x86_64"
extension="tar.gz"
;;
darwin-aarch64)
artifact_name="wonopcode-darwin-aarch64"
extension="tar.gz"
;;
windows-x86_64)
artifact_name="wonopcode-windows-x86_64"
extension="zip"
;;
*)
die "No pre-built binary available for $platform"
;;
esac
# Create temporary directory
TMP_DIR=$(mktemp -d)
trap 'rm -rf "$TMP_DIR"' EXIT
# Download archive and checksums
local archive_name="${artifact_name}.${extension}"
local archive_path="${TMP_DIR}/${archive_name}"
local checksums_path="${TMP_DIR}/checksums.txt"
local download_url="${GITHUB_RELEASES}/download/${VERSION}/${archive_name}"
local checksums_url="${GITHUB_RELEASES}/download/${VERSION}/checksums.txt"
download "$download_url" "$archive_path"
download "$checksums_url" "$checksums_path" || warn "Failed to download checksums file"
# Verify checksum
verify_checksum "$archive_path" "$checksums_path" "$archive_name"
# Extract archive
info "Extracting archive..."
local extract_dir="${TMP_DIR}/extract"
mkdir -p "$extract_dir"
extract "$archive_path" "$extract_dir"
# Find the binary
local binary_path
if [ "$platform" = "windows-x86_64" ]; then
binary_path=$(find "$extract_dir" -name "${BINARY_NAME}.exe" -type f | head -1)
else
binary_path=$(find "$extract_dir" -name "$BINARY_NAME" -type f | head -1)
fi
if [ -z "$binary_path" ] || [ ! -f "$binary_path" ]; then
die "Binary not found in archive"
fi
# Determine install directory
local install_dir
install_dir=$(get_install_dir)
info "Installing to: $install_dir"
# Create install directory if needed
if [ ! -d "$install_dir" ]; then
mkdir -p "$install_dir" 2>/dev/null || sudo mkdir -p "$install_dir"
fi
# Install binary
local dest_path="${install_dir}/${BINARY_NAME}"
if [ "$platform" = "windows-x86_64" ]; then
dest_path="${install_dir}/${BINARY_NAME}.exe"
fi
if [ -w "$install_dir" ]; then
cp "$binary_path" "$dest_path"
chmod +x "$dest_path"
else
info "Requesting sudo access to install to $install_dir"
sudo cp "$binary_path" "$dest_path"
sudo chmod +x "$dest_path"
fi
echo ""
success "Wonopcode $VERSION installed successfully!"
echo ""
# Check PATH
check_path "$install_dir"
# Verify installation
if command -v "$BINARY_NAME" &> /dev/null; then
echo "Run 'wonopcode --help' to get started"
else
echo "Run '$dest_path --help' to get started"
fi
echo ""
}
main "$@"