-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·528 lines (456 loc) · 16.9 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·528 lines (456 loc) · 16.9 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#!/usr/bin/env bash
# WPS 365 CLI installer
# Usage: curl -fsSL https://open-docs.wpscdn.cn/cli/install.sh | bash
# or: curl -fsSL https://open-docs.wpscdn.cn/cli/install.sh | bash -s -- --version v0.3.2
#
# Inspired by Homebrew, rustup, Starship install scripts.
set -euo pipefail
BIN_NAME="wps365-cli"
GITHUB_REPO="wps365-open/cli"
GO_MODULE_PATH="wps365-cli"
CDN_BASE_URL="${WPS365_CDN_URL:-https://open-docs.wpscdn.cn/cli/releases/download}"
CDN_LATEST_URL="${WPS365_CDN_LATEST_URL:-https://open-docs.wpscdn.cn/cli/latest.txt}"
GITHUB_BASE_URL="https://github.com/${GITHUB_REPO}/releases/download"
GITHUB_API_URL="https://api.github.com/repos/${GITHUB_REPO}/releases/latest"
VERSION="latest"
# Default to ~/.local/bin (no sudo). Override with WPS365_INSTALL_DIR or --install-dir.
INSTALL_DIR="${WPS365_INSTALL_DIR:-${HOME}/.local/bin}"
MODIFY_PATH=true
FORCE=false
INSECURE=false
TMP_DIR=""
# ------------------------------------------------------------------
# Logging
# ------------------------------------------------------------------
info() { printf "\033[1;34m==>\033[0m %s\n" "$*"; }
warn() { printf "\033[1;33mWarning:\033[0m %s\n" "$*" >&2; }
error() { printf "\033[1;31mError:\033[0m %s\n" "$*" >&2; }
abort() { error "$@"; exit 1; }
# ------------------------------------------------------------------
# Helpers
# ------------------------------------------------------------------
has_cmd() { command -v "$1" >/dev/null 2>&1; }
need_cmd() {
if ! has_cmd "$1"; then
abort "Required command '$1' not found. Please install it and retry."
fi
}
# Portable download: prefers curl, falls back to wget
do_download() {
local url="$1" dest="$2"
if has_cmd curl; then
# Allow plain HTTP for local/private URLs (e.g. WPS365_CDN_URL=http://...)
case "$url" in
http://127.0.0.1*|http://localhost*)
curl -fsSL --retry 3 -o "$dest" "$url"
;;
*)
curl --proto '=https' --tlsv1.2 -fsSL --retry 3 -o "$dest" "$url"
;;
esac
elif has_cmd wget; then
case "$url" in
http://127.0.0.1*|http://localhost*)
wget -q --tries=3 -O "$dest" "$url"
;;
*)
wget --https-only -q --tries=3 -O "$dest" "$url"
;;
esac
else
abort "Either 'curl' or 'wget' is required for downloading."
fi
}
# Download with fallback: CDN first, then GitHub
do_download_with_fallback() {
local cdn_url="$1" gh_url="$2" dest="$3"
if do_download "$cdn_url" "$dest" 2>/dev/null; then
return 0
fi
warn "CDN download failed, trying GitHub Releases..."
do_download "$gh_url" "$dest"
}
# ------------------------------------------------------------------
# parse_args
# ------------------------------------------------------------------
parse_args() {
while [ $# -gt 0 ]; do
case "$1" in
--version|-v)
shift
[ $# -eq 0 ] && abort "--version requires a value (e.g. v0.2.0)"
VERSION="$1"
;;
--install-dir|-d)
shift
[ $# -eq 0 ] && abort "--install-dir requires a path"
INSTALL_DIR="$1"
;;
--no-modify-path)
MODIFY_PATH=false
;;
--force|-f)
FORCE=true
;;
--insecure)
INSECURE=true
;;
--help|-h)
print_help
exit 0
;;
*)
abort "Unknown option: $1 (see --help)"
;;
esac
shift
done
}
print_help() {
cat <<EOF
WPS 365 CLI Installer
USAGE:
curl -fsSL https://open-docs.wpscdn.cn/cli/install.sh | bash
curl -fsSL https://open-docs.wpscdn.cn/cli/install.sh | bash -s -- [OPTIONS]
OPTIONS:
-v, --version <VERSION> Install a specific version (e.g. v0.3.2) [default: latest]
-d, --install-dir <DIR> Installation directory [default: ~/.local/bin]
--no-modify-path Do not modify shell profile for PATH
-f, --force Overwrite existing binary without prompting
--insecure Skip checksum verification (NOT recommended)
-h, --help Show this help message
ENVIRONMENT:
WPS365_CDN_URL Override CDN base URL for downloads
WPS365_CDN_LATEST_URL Override CDN latest.txt URL
WPS365_INSTALL_DIR Override installation directory [default: ~/.local/bin]
EXAMPLES:
# Install latest version
curl -fsSL https://open-docs.wpscdn.cn/cli/install.sh | bash
# Install specific version
curl -fsSL https://open-docs.wpscdn.cn/cli/install.sh | bash -s -- -v v0.3.2
# Install to /usr/local/bin
curl -fsSL https://open-docs.wpscdn.cn/cli/install.sh | bash -s -- -d /usr/local/bin
# Uninstall
rm ~/.local/bin/wps365-cli
EOF
}
# ------------------------------------------------------------------
# detect_platform: returns darwin / linux
# ------------------------------------------------------------------
detect_platform() {
local os
os="$(uname -s)"
case "$os" in
Darwin) echo "darwin" ;;
Linux) echo "linux" ;;
MINGW*|MSYS*|CYGWIN*)
echo "windows" ;;
*)
abort "Unsupported operating system: $os"
;;
esac
}
# ------------------------------------------------------------------
# detect_arch: returns x86_64 / aarch64
# ------------------------------------------------------------------
detect_arch() {
local arch
arch="$(uname -m)"
case "$arch" in
x86_64|amd64) echo "x86_64" ;;
arm64|aarch64) echo "aarch64" ;;
*)
abort "Unsupported architecture: $arch"
;;
esac
}
# ------------------------------------------------------------------
# resolve_target: maps OS+arch to release target triple
# ------------------------------------------------------------------
resolve_target() {
local platform="$1" arch="$2"
case "${platform}-${arch}" in
darwin-aarch64) echo "aarch64-apple-darwin" ;;
darwin-x86_64) echo "x86_64-apple-darwin" ;;
linux-x86_64) echo "x86_64-unknown-linux-gnu" ;;
linux-aarch64) echo "aarch64-unknown-linux-gnu" ;;
windows-x86_64) echo "x86_64-pc-windows-gnu" ;;
windows-aarch64) echo "aarch64-pc-windows-gnu" ;;
*)
abort "No prebuilt binary for ${platform}/${arch}. Try building from source: go install github.com/${GITHUB_REPO}/cmd/wps365-cli@latest"
;;
esac
}
# ------------------------------------------------------------------
# resolve_version: fetch latest tag or validate user-provided version
# ------------------------------------------------------------------
resolve_version() {
if [ "$VERSION" = "latest" ]; then
info "Fetching latest version..."
local tag=""
# Prefer CDN latest.txt to avoid GitHub API rate limits.
if has_cmd curl; then
tag="$(curl --proto '=https' --tlsv1.2 -fsSL "$CDN_LATEST_URL" 2>/dev/null | tr -d '[:space:]' || true)"
elif has_cmd wget; then
tag="$(wget --https-only -qO- "$CDN_LATEST_URL" 2>/dev/null | tr -d '[:space:]' || true)"
else
abort "Either 'curl' or 'wget' is required."
fi
if [ -z "$tag" ]; then
warn "CDN latest.txt unavailable, falling back to GitHub API..."
if has_cmd curl; then
tag="$(curl --proto '=https' --tlsv1.2 -fsSL "$GITHUB_API_URL" | grep '"tag_name"' | head -1 | sed 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/')"
elif has_cmd wget; then
tag="$(wget --https-only -qO- "$GITHUB_API_URL" | grep '"tag_name"' | head -1 | sed 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/')"
fi
fi
if [ -z "$tag" ]; then
abort "Failed to resolve latest version. Specify a version with --version."
fi
VERSION="$tag"
fi
# Normalize: ensure version starts with 'v'
case "$VERSION" in
v*) ;;
*) VERSION="v${VERSION}" ;;
esac
}
# ------------------------------------------------------------------
# verify_checksum: SHA256 verification
# ------------------------------------------------------------------
verify_checksum() {
local archive="$1" checksums_file="$2" archive_name="$3"
if [ "$INSECURE" = "true" ]; then
warn "Checksum verification is disabled (--insecure)."
return 0
fi
if [ ! -f "$checksums_file" ]; then
abort "Checksum file not available: ${checksums_file}"
fi
local expected
expected="$(awk -v target="$archive_name" '$2 == target {print $1}' "$checksums_file" | head -1)"
if [ -z "$expected" ]; then
abort "No checksum found for ${archive_name} in ${checksums_file}"
fi
local actual
if has_cmd sha256sum; then
actual="$(sha256sum "$archive" | awk '{print $1}')"
elif has_cmd shasum; then
actual="$(shasum -a 256 "$archive" | awk '{print $1}')"
else
abort "Neither sha256sum nor shasum found; cannot verify archive checksum."
fi
if [ "$expected" != "$actual" ]; then
abort "Checksum verification failed!\n Expected: ${expected}\n Actual: ${actual}\nThe downloaded file may be corrupted or tampered with."
fi
info "Checksum verified (SHA-256)"
}
# ------------------------------------------------------------------
# install_binary: extract archive and place binary
# ------------------------------------------------------------------
install_binary() {
local archive="$1" install_dir="$2" platform="$3"
mkdir -p "$install_dir"
local ext=""
[ "$platform" = "windows" ] && ext=".exe"
case "$archive" in
*.tar.gz)
tar xzf "$archive" -C "$install_dir" "${BIN_NAME}${ext}"
;;
*.zip)
need_cmd unzip
unzip -oq "$archive" "${BIN_NAME}${ext}" -d "$install_dir"
;;
*)
abort "Unknown archive format: $archive"
;;
esac
chmod +x "${install_dir}/${BIN_NAME}${ext}"
}
# ------------------------------------------------------------------
# detect_profile: find the user's shell profile file
# ------------------------------------------------------------------
detect_profile() {
local shell_name
shell_name="$(basename "${SHELL:-/bin/sh}")"
case "$shell_name" in
zsh)
local zshrc="${ZDOTDIR:-$HOME}/.zshrc"
if [ -f "$zshrc" ]; then
echo "$zshrc"
elif [ -f "$HOME/.zprofile" ]; then
echo "$HOME/.zprofile"
else
echo "$zshrc"
fi
;;
bash)
if [ -f "$HOME/.bashrc" ]; then
echo "$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
echo "$HOME/.bash_profile"
else
echo "$HOME/.bashrc"
fi
;;
fish)
echo "${XDG_CONFIG_HOME:-$HOME/.config}/fish/config.fish"
;;
*)
if [ -f "$HOME/.profile" ]; then
echo "$HOME/.profile"
else
echo ""
fi
;;
esac
}
# ------------------------------------------------------------------
# configure_path: add install dir to PATH via shell profile
# ------------------------------------------------------------------
configure_path() {
local install_dir="$1"
# Already in PATH? Nothing to do.
case ":${PATH}:" in
*":${install_dir}:"*)
return 0
;;
esac
if [ "$MODIFY_PATH" != "true" ]; then
warn "${install_dir} is not in your PATH. Add it manually:"
warn " export PATH=\"${install_dir}:\$PATH\""
return 0
fi
local profile
profile="$(detect_profile)"
if [ -z "$profile" ]; then
warn "Could not detect shell profile. Add to your PATH manually:"
warn " export PATH=\"${install_dir}:\$PATH\""
return 0
fi
local shell_name
shell_name="$(basename "${SHELL:-/bin/sh}")"
local path_line
if [ "$shell_name" = "fish" ]; then
path_line="set -gx PATH \"${install_dir}\" \$PATH"
else
path_line="export PATH=\"${install_dir}:\$PATH\""
fi
if [ -f "$profile" ] && grep -qF "$install_dir" "$profile" 2>/dev/null; then
return 0
fi
info "Adding ${install_dir} to PATH in ${profile}"
{
echo ""
echo "# wps365-cli"
echo "$path_line"
} >> "$profile"
}
# ------------------------------------------------------------------
# print_summary
# ------------------------------------------------------------------
print_summary() {
local install_dir="$1" version="$2" profile="$3" bin_name="$4"
echo ""
printf "\033[1;32m✓\033[0m WPS 365 CLI %s installed to %s/%s\n" "$version" "$install_dir" "$bin_name"
echo ""
# Check if available in current session
case ":${PATH}:" in
*":${install_dir}:"*)
echo " Run 'wps365-cli --version' to verify."
;;
*)
if [ -n "$profile" ]; then
echo " Restart your terminal or run:"
echo " source ${profile}"
else
echo " Add to your PATH:"
echo " export PATH=\"${install_dir}:\$PATH\""
fi
;;
esac
echo ""
echo " 快速开始:"
echo " ${bin_name} config init # 浏览器创建/绑定应用(仅需一次)"
echo " ${bin_name} auth login --device # 设备码授权(可省略 --scopes)"
echo " ${bin_name} user me # 确认当前登录用户"
echo ""
echo " To uninstall:"
echo " rm ${install_dir}/${bin_name}"
echo ""
}
# ------------------------------------------------------------------
# main
# ------------------------------------------------------------------
main() {
parse_args "$@"
info "WPS 365 CLI Installer"
echo ""
local platform arch target
platform="$(detect_platform)"
arch="$(detect_arch)"
target="$(resolve_target "$platform" "$arch")"
info "Detected platform: ${platform}/${arch} -> ${target}"
resolve_version
info "Version: ${VERSION}"
# Archive name and extension
local archive_ext="tar.gz"
[ "$platform" = "windows" ] && archive_ext="zip"
local archive_name="${BIN_NAME}-${target}.${archive_ext}"
# Create temp dir with cleanup trap
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "$TMP_DIR"' EXIT
# Download archive
local cdn_url="${CDN_BASE_URL}/${VERSION}/${archive_name}"
local gh_url="${GITHUB_BASE_URL}/${VERSION}/${archive_name}"
local archive_path="${TMP_DIR}/${archive_name}"
info "Downloading ${archive_name}..."
do_download_with_fallback "$cdn_url" "$gh_url" "$archive_path"
# Download and verify checksum
local checksums_path="${TMP_DIR}/checksums-sha256.txt"
local cdn_checksums="${CDN_BASE_URL}/${VERSION}/checksums-sha256.txt"
local gh_checksums="${GITHUB_BASE_URL}/${VERSION}/checksums-sha256.txt"
do_download_with_fallback "$cdn_checksums" "$gh_checksums" "$checksums_path"
verify_checksum "$archive_path" "$checksums_path" "$archive_name"
local ext=""
[ "$platform" = "windows" ] && ext=".exe"
local bin_name="${BIN_NAME}${ext}"
# Check for existing installation
local install_path="${INSTALL_DIR}/${bin_name}"
if [ -f "$install_path" ] && [ "$FORCE" != "true" ]; then
local existing_ver
existing_ver="$("$install_path" --version 2>/dev/null || echo "unknown")"
info "Existing installation found: ${existing_ver}"
info "Upgrading to ${VERSION}..."
fi
# Check write permission, use sudo if needed
local use_sudo=""
if [ -d "$INSTALL_DIR" ] && [ ! -w "$INSTALL_DIR" ]; then
warn "${INSTALL_DIR} is not writable. Using sudo..."
use_sudo="sudo"
elif [ ! -d "$INSTALL_DIR" ]; then
mkdir -p "$INSTALL_DIR" 2>/dev/null || {
warn "Cannot create ${INSTALL_DIR}. Using sudo..."
use_sudo="sudo"
$use_sudo mkdir -p "$INSTALL_DIR"
}
fi
# Extract to temp, then move (supports sudo)
local staging_dir="${TMP_DIR}/staging"
mkdir -p "$staging_dir"
install_binary "$archive_path" "$staging_dir" "$platform"
if [ -n "$use_sudo" ]; then
$use_sudo cp "${staging_dir}/${BIN_NAME}${ext}" "${INSTALL_DIR}/${BIN_NAME}${ext}"
$use_sudo chmod +x "${INSTALL_DIR}/${BIN_NAME}${ext}"
else
cp "${staging_dir}/${BIN_NAME}${ext}" "${INSTALL_DIR}/${BIN_NAME}${ext}"
chmod +x "${INSTALL_DIR}/${BIN_NAME}${ext}"
fi
info "Installed to ${INSTALL_DIR}/${BIN_NAME}${ext}"
# Configure PATH
local profile=""
configure_path "$INSTALL_DIR"
profile="$(detect_profile)"
print_summary "$INSTALL_DIR" "$VERSION" "$profile" "$bin_name"
}
main "$@"