-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·360 lines (306 loc) · 13.3 KB
/
install.sh
File metadata and controls
executable file
·360 lines (306 loc) · 13.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
#!/usr/bin/env bash
# vx installer script for Linux and macOS
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/loonghao/vx/main/install.sh | bash
#
# With specific version:
# VX_VERSION="0.8.4" curl -fsSL https://raw.githubusercontent.com/loonghao/vx/main/install.sh | bash
#
# With custom install directory:
# VX_INSTALL_DIR="$HOME/bin" curl -fsSL https://raw.githubusercontent.com/loonghao/vx/main/install.sh | bash
#
# With custom release mirrors (comma separated):
# VX_RELEASE_BASE_URLS="https://mirror.example.com/vx/releases,https://github.com/loonghao/vx/releases" curl -fsSL https://raw.githubusercontent.com/loonghao/vx/main/install.sh | bash
#
# With GitHub token (to avoid rate limits when specifying a version):
# GITHUB_TOKEN="your_token" curl -fsSL https://raw.githubusercontent.com/loonghao/vx/main/install.sh | bash
#
# CDN acceleration (disabled by default, opt-in for slow GitHub access):
# VX_CDN=1 curl -fsSL https://raw.githubusercontent.com/loonghao/vx/main/install.sh | bash
#
# Environment variables:
# VX_VERSION - Version to install (default: latest stable)
# VX_INSTALL_DIR - Installation directory (default: $HOME/.local/bin)
# VX_RELEASE_BASE_URLS- Comma-separated release mirror URLs
# GITHUB_TOKEN - GitHub API token to avoid rate limits
# VX_CDN - Set to "1" to enable CDN acceleration (disabled by default)
set -euo pipefail
# Global temp dir so the EXIT trap can always reference it
_VX_TEMP_DIR=""
REPO_OWNER="loonghao"
REPO_NAME="vx"
BASE_URL="https://github.com/$REPO_OWNER/$REPO_NAME/releases"
VX_VERSION="${VX_VERSION:-}"
VX_INSTALL_DIR="${VX_INSTALL_DIR:-$HOME/.local/bin}"
VX_RELEASE_BASE_URLS="${VX_RELEASE_BASE_URLS:-$BASE_URL}"
# ── Logging ───────────────────────────────────────────────────────────────────
step() { printf " \033[36m%s\033[0m %s\n" "$REPO_NAME" "$1" >&2; }
ok() { printf " \033[32m%s\033[0m %s\n" "$REPO_NAME" "$1" >&2; }
fail() { printf " \033[31m%s\033[0m %s\n" "$REPO_NAME" "$1" >&2; exit 1; }
# ── Platform detection ────────────────────────────────────────────────────────
detect_platform() {
local os arch
case "$(uname -s)" in
Linux*) os="unknown-linux" ;;
Darwin*) os="apple-darwin" ;;
*) fail "Unsupported OS: $(uname -s)" ;;
esac
case "$(uname -m)" in
x86_64|amd64) arch="x86_64" ;;
aarch64|arm64) arch="aarch64" ;;
*) fail "Unsupported architecture: $(uname -m)" ;;
esac
if [[ "$os" == "unknown-linux" ]]; then
# Prefer musl on Alpine or when PREFER_STATIC is set
local libc="gnu"
if [[ "${PREFER_STATIC:-false}" == "true" ]] || \
[[ -f /etc/alpine-release ]] || \
(ldd --version 2>&1 | grep -q musl); then
libc="musl"
fi
echo "$arch-$os-$libc"
else
echo "$arch-$os"
fi
}
# ── Download helper ───────────────────────────────────────────────────────────
download() {
local url="$1" dest="$2"
local auth_opts=""
if [[ -n "${GITHUB_TOKEN:-}" ]]; then
auth_opts="-H \"Authorization: Bearer $GITHUB_TOKEN\""
fi
local max_retries=3
for i in $(seq 1 $max_retries); do
if command -v curl >/dev/null 2>&1; then
if eval curl -fsSL --connect-timeout 15 --max-time 120 $auth_opts "\"$url\"" -o "\"$dest\"" 2>/dev/null; then
local size
size=$(stat -f%z "$dest" 2>/dev/null || stat -c%s "$dest" 2>/dev/null || echo 0)
[[ "$size" -gt 1024 ]] && return 0
fi
elif command -v wget >/dev/null 2>&1; then
if eval wget -q --timeout=120 $auth_opts "\"$url\"" -O "\"$dest\"" 2>/dev/null; then
local size
size=$(stat -f%z "$dest" 2>/dev/null || stat -c%s "$dest" 2>/dev/null || echo 0)
[[ "$size" -gt 1024 ]] && return 0
fi
else
fail "Neither curl nor wget is available"
fi
rm -f "$dest"
[[ $i -lt $max_retries ]] && sleep 2
done
return 1
}
get_release_base_urls() {
local raw="${VX_RELEASE_BASE_URLS//;/,}"
IFS=',' read -r -a _vx_urls <<< "$raw"
for u in "${_vx_urls[@]}"; do
# trim leading spaces
u="${u#${u%%[![:space:]]*}}"
# trim trailing spaces
u="${u%${u##*[![:space:]]}}"
[[ -n "$u" ]] && printf '%s\n' "$u"
done
}
resolve_latest_version() {
local auth_opts=""
if [[ -n "${GITHUB_TOKEN:-}" ]]; then
auth_opts="-H \"Authorization: Bearer $GITHUB_TOKEN\""
fi
local api_url="https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases?per_page=20"
local json=""
if command -v curl >/dev/null 2>&1; then
json=$(eval curl -fsSL --connect-timeout 15 --max-time 30 $auth_opts "\"$api_url\"" 2>/dev/null || true)
elif command -v wget >/dev/null 2>&1; then
json=$(wget -qO- --timeout=30 "$api_url" 2>/dev/null || true)
fi
[[ -z "$json" ]] && return 1
# Find the first non-draft, non-prerelease release that has actual binary
# assets. This avoids selecting a release whose build workflow failed and
# left the release with zero downloadable files.
#
# Strategy: iterate through releases in the JSON and for each one check
# that it is not a draft/prerelease and that it contains at least one
# "browser_download_url" entry (which indicates an uploaded asset).
local tag=""
# Use POSIX-compatible awk (works on macOS and Linux)
# NOTE: In awk, `exit` jumps to the END block rather than terminating
# immediately, so we use a `found` flag to prevent double-printing.
tag=$(printf '%s' "$json" | awk '
BEGIN { found = 0; in_release = 0; cur_tag = ""; is_draft = 0; is_pre = 0; has_assets = 0 }
/"tag_name"/ {
# Check if the previous release qualifies
if (in_release && cur_tag != "" && !is_draft && !is_pre && has_assets) {
print cur_tag
found = 1
exit
}
# Start tracking a new release
in_release = 1
is_draft = 0
is_pre = 0
has_assets = 0
# Extract tag value (POSIX awk compatible)
s = $0
gsub(/.*"tag_name"[[:space:]]*:[[:space:]]*"/, "", s)
gsub(/".*/, "", s)
cur_tag = s
}
/"draft"[[:space:]]*:[[:space:]]*true/ { is_draft = 1 }
/"prerelease"[[:space:]]*:[[:space:]]*true/ { is_pre = 1 }
/"browser_download_url"/ { has_assets = 1 }
END {
if (!found && in_release && cur_tag != "" && !is_draft && !is_pre && has_assets) {
print cur_tag
}
}
')
# Safety: take only the first line in case awk produced multiple lines
tag=$(printf '%s' "$tag" | head -1)
[[ -z "$tag" ]] && return 1
tag="${tag#v}"
tag="${tag#vx-v}"
printf '%s\n' "$tag"
}
# ── Main ──────────────────────────────────────────────────────────────────────
main() {
local platform
platform=$(detect_platform)
step "Installing vx for $(uname -s)..."
step "Detected: $(uname -s) $(uname -m) -> $platform"
_VX_TEMP_DIR=$(mktemp -d)
trap 'rm -rf "$_VX_TEMP_DIR"' EXIT
local temp_dir="$_VX_TEMP_DIR"
# Build list of (url, archive) candidates to try
local candidates=()
local release_bases=()
while IFS= read -r _base; do
[[ -n "$_base" ]] && release_bases+=("$_base")
done < <(get_release_base_urls)
if [[ ${#release_bases[@]} -eq 0 ]]; then
release_bases=("$BASE_URL")
fi
if [[ ${#release_bases[@]} -gt 1 ]]; then
step "Using release mirrors: ${release_bases[*]}"
fi
if [[ -z "$VX_VERSION" || "$VX_VERSION" == "latest" ]]; then
# No version specified — try latest URL first, then resolved stable version
for base in "${release_bases[@]}"; do
candidates+=("$base/latest/download/vx-$platform.tar.gz")
done
local latest_ver=""
latest_ver=$(resolve_latest_version || true)
if [[ -n "$latest_ver" ]]; then
step "Resolved latest stable version with assets: $latest_ver"
for base in "${release_bases[@]}"; do
for tag in "v$latest_ver" "vx-v$latest_ver"; do
candidates+=("$base/download/$tag/vx-$latest_ver-$platform.tar.gz")
candidates+=("$base/download/$tag/vx-$platform.tar.gz")
done
if [[ "$platform" == *"linux-gnu"* ]]; then
local fallback="${platform/linux-gnu/linux-musl}"
for tag in "v$latest_ver" "vx-v$latest_ver"; do
candidates+=("$base/download/$tag/vx-$latest_ver-$fallback.tar.gz")
candidates+=("$base/download/$tag/vx-$fallback.tar.gz")
done
elif [[ "$platform" == *"linux-musl"* ]]; then
local fallback="${platform/linux-musl/linux-gnu}"
for tag in "v$latest_ver" "vx-v$latest_ver"; do
candidates+=("$base/download/$tag/vx-$latest_ver-$fallback.tar.gz")
candidates+=("$base/download/$tag/vx-$fallback.tar.gz")
done
fi
done
fi
else
# Normalize version
local ver="${VX_VERSION#v}"
ver="${ver#vx-v}"
# Try v{ver} tag first (v0.7.0+), then vx-v{ver} (legacy)
for base in "${release_bases[@]}"; do
for tag in "v$ver" "vx-v$ver"; do
candidates+=("$base/download/$tag/vx-$ver-$platform.tar.gz")
candidates+=("$base/download/$tag/vx-$platform.tar.gz")
done
# Also try fallback libc variant for Linux
if [[ "$platform" == *"linux-gnu"* ]]; then
local fallback="${platform/linux-gnu/linux-musl}"
for tag in "v$ver" "vx-v$ver"; do
candidates+=("$base/download/$tag/vx-$ver-$fallback.tar.gz")
candidates+=("$base/download/$tag/vx-$fallback.tar.gz")
done
elif [[ "$platform" == *"linux-musl"* ]]; then
local fallback="${platform/linux-musl/linux-gnu}"
for tag in "v$ver" "vx-v$ver"; do
candidates+=("$base/download/$tag/vx-$ver-$fallback.tar.gz")
candidates+=("$base/download/$tag/vx-$fallback.tar.gz")
done
fi
done
fi
# Try each candidate URL
local archive_path=""
local archive_name=""
for url in "${candidates[@]}"; do
archive_name="${url##*/}"
local dest="$temp_dir/$archive_name"
step "Downloading from: $url"
if download "$url" "$dest"; then
archive_path="$dest"
break
fi
done
if [[ -z "$archive_path" ]]; then
local hint_ver="${latest_ver:-0.8.4}"
fail "Download failed. Check your internet connection or specify a version:
VX_VERSION='$hint_ver' curl -fsSL https://raw.githubusercontent.com/loonghao/vx/main/install.sh | bash"
fi
# Extract
step "Extracting..."
mkdir -p "$VX_INSTALL_DIR"
tar -xzf "$archive_path" -C "$temp_dir"
# Find and install binary
local binary
binary=$(find "$temp_dir" -name "vx" -type f | head -n1)
[[ -z "$binary" ]] && fail "vx binary not found in archive"
cp "$binary" "$VX_INSTALL_DIR/vx"
chmod +x "$VX_INSTALL_DIR/vx"
# Detect installed version
local installed_version
installed_version=$("$VX_INSTALL_DIR/vx" --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || echo "unknown")
ok "Installed: vx $installed_version"
# Update PATH
local path_export="export PATH=\"$VX_INSTALL_DIR:\$PATH\""
if [[ ":$PATH:" != *":$VX_INSTALL_DIR:"* ]]; then
local shell_config
case "${SHELL:-bash}" in
*/zsh) shell_config="$HOME/.zshrc" ;;
*/fish) shell_config="$HOME/.config/fish/config.fish" ;;
*) shell_config="$HOME/.bashrc" ;;
esac
if [[ -w "$(dirname "$shell_config")" ]]; then
echo "" >> "$shell_config"
echo "# Added by vx installer" >> "$shell_config"
if [[ "$shell_config" == *"fish"* ]]; then
echo "set -gx PATH \"$VX_INSTALL_DIR\" \$PATH" >> "$shell_config"
else
echo "$path_export" >> "$shell_config"
fi
ok "Added to PATH in $shell_config"
fi
export PATH="$VX_INSTALL_DIR:$PATH"
fi
# GitHub Actions support
if [[ -n "${GITHUB_PATH:-}" ]]; then
echo "$VX_INSTALL_DIR" >> "$GITHUB_PATH"
fi
echo "" >&2
ok "vx installed successfully!"
echo "" >&2
printf " Run: vx --help\n" >&2
printf " Docs: https://github.com/%s/%s\n" "$REPO_OWNER" "$REPO_NAME" >&2
echo "" >&2
}
main "$@"