-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·373 lines (331 loc) · 9.63 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·373 lines (331 loc) · 9.63 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
#!/usr/bin/env bash
# AnyRouter CLI installer. Downloads GitHub Release binaries (not npm).
# curl -fsSL https://raw.githubusercontent.com/anyrouter-dev/cli/main/setup.sh | bash
# curl -fsSL https://anyrouter.dev/setup.sh | bash
# ANYR_CHANNEL=beta ./setup.sh --channel beta
# Assets: https://github.com/anyrouter-dev/cli/releases/download/<tag>/anyr-<os>-<arch>
set -euo pipefail
REPO="anyrouter-dev/cli"
GITHUB="https://github.com/anyrouter-dev/cli"
GITHUB_API="https://api.github.com/repos/anyrouter-dev/cli"
BIN_NAME="anyr"
BIN_DIR="${ANYR_BIN_DIR:-${HOME}/.local/bin}"
CHANNEL="${ANYR_CHANNEL:-stable}"
VERSION="${ANYR_VERSION:-}"
CURL="${ANYR_CURL:-curl}"
usage() {
cat <<EOF
Install AnyRouter CLI (${BIN_NAME}) from GitHub Releases.
Usage:
./setup.sh [--channel stable|beta] [--version X.Y.Z]
Channels:
stable (default) GitHub /releases/latest if that asset exists; otherwise
the newest release (including prerelease) that has binaries
beta newest prerelease that has binaries
Listing uses GH_TOKEN/GITHUB_TOKEN against the GitHub API when set.
Without a token, setup.sh never calls api.github.com (unauth quota 403s);
it reads github.com/releases HTML instead.
Tagged version:
${GITHUB}/releases/download/v\${version}/anyr-\${os}-\${arch}
Env:
ANYR_CHANNEL stable (default) | beta
ANYR_VERSION install this tag (v prefix optional)
ANYR_BIN_DIR install directory (default: ~/.local/bin)
ANYR_SETUP_BIN if this is a file, copy it instead of downloading (tests)
GH_TOKEN / GITHUB_TOKEN optional; authenticated api.github.com (avoids 403)
ANYR_CURL curl binary (tests)
Assets: anyr-linux-x86_64 anyr-linux-arm64 anyr-darwin-x86_64 anyr-darwin-arm64
EOF
}
die() {
echo "setup.sh: $*" >&2
exit 1
}
while [ $# -gt 0 ]; do
case "$1" in
-h | --help)
usage
exit 0
;;
--channel)
[ $# -ge 2 ] || die "--channel requires a value"
CHANNEL="$2"
shift 2
;;
--channel=*)
CHANNEL="${1#--channel=}"
shift
;;
--version)
[ $# -ge 2 ] || die "--version requires a value"
VERSION="$2"
shift 2
;;
--version=*)
VERSION="${1#--version=}"
shift
;;
--bin-dir)
[ $# -ge 2 ] || die "--bin-dir requires a value"
BIN_DIR="$2"
shift 2
;;
--bin-dir=*)
BIN_DIR="${1#--bin-dir=}"
shift
;;
*)
die "unknown argument: $1"
;;
esac
done
CHANNEL="$(printf '%s' "$CHANNEL" | tr '[:upper:]' '[:lower:]')"
case "$CHANNEL" in
stable | beta) ;;
*) die "unknown channel \"${CHANNEL}\" (use stable or beta)" ;;
esac
detect_os() {
case "$(uname -s)" in
Linux) printf 'linux' ;;
Darwin) printf 'darwin' ;;
*) die "unsupported OS: $(uname -s) (need linux or darwin)" ;;
esac
}
detect_arch() {
case "$(uname -m)" in
x86_64 | amd64) printf 'x86_64' ;;
arm64 | aarch64) printf 'arm64' ;;
*) die "unsupported arch: $(uname -m) (need x86_64 or arm64)" ;;
esac
}
install_bin() {
local src="$1"
[ -f "$src" ] || die "source is not a file: $src"
mkdir -p "$BIN_DIR"
cp "$src" "${BIN_DIR}/${BIN_NAME}"
chmod +x "${BIN_DIR}/${BIN_NAME}"
ln -sfn "$BIN_NAME" "${BIN_DIR}/anyrouter"
ln -sfn "$BIN_NAME" "${BIN_DIR}/ar"
echo "Installed ${BIN_DIR}/${BIN_NAME}"
echo "Symlinks: ${BIN_DIR}/anyrouter ${BIN_DIR}/ar"
case ":${PATH}:" in
*":${BIN_DIR}:"*) ;;
*)
echo "Add ${BIN_DIR} to PATH:"
echo " export PATH=\"${BIN_DIR}:\$PATH\""
;;
esac
echo "Next:"
echo " ${BIN_NAME} auth login"
echo " ${BIN_NAME} claude"
}
# Local test mode: copy ANYR_SETUP_BIN, never hit the network.
if [ -n "${ANYR_SETUP_BIN:-}" ]; then
[ -f "$ANYR_SETUP_BIN" ] || die "ANYR_SETUP_BIN is not a file: $ANYR_SETUP_BIN"
echo "ANYR_SETUP_BIN=${ANYR_SETUP_BIN} (local, no download)"
install_bin "$ANYR_SETUP_BIN"
exit 0
fi
command -v "$CURL" >/dev/null 2>&1 || die "curl is required to download releases"
os="$(detect_os)"
arch="$(detect_arch)"
asset="anyr-${os}-${arch}"
github_token() {
if [ -n "${GH_TOKEN:-}" ]; then
printf '%s' "$GH_TOKEN"
elif [ -n "${GITHUB_TOKEN:-}" ]; then
printf '%s' "$GITHUB_TOKEN"
fi
}
curl_ua() {
"$CURL" -A "anyr-setup" "$@"
}
# Follow redirects. Empty /releases/latest 302s then 404s; a real asset ends 200.
asset_available() {
local url="$1"
local code
code="$(curl_ua -sI -L -o /dev/null -w '%{http_code}' "$url" || true)"
case "$code" in
200 | 206) return 0 ;;
*) return 1 ;;
esac
}
download_url_for_tag() {
printf '%s/releases/download/%s/%s' "$GITHUB" "$1" "$asset"
}
# Authenticated REST only. Never call api.github.com without a token.
pick_tag_from_api() {
local token="$1"
local json
json="$(curl_ua -fsSL \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${token}" \
"${GITHUB_API}/releases")" || return 1
command -v python3 >/dev/null 2>&1 || die "python3 is required to parse GitHub Releases JSON"
printf '%s' "$json" | python3 -c '
import json, sys
asset, channel = sys.argv[1], sys.argv[2]
releases = json.load(sys.stdin)
def tag_of(rel):
if rel.get("draft"):
return ""
return rel.get("tag_name") or ""
def has_asset(rel):
names = [a.get("name") for a in (rel.get("assets") or [])]
return asset in names
def is_pre(rel):
return bool(rel.get("prerelease"))
ordered = [r for r in releases if tag_of(r)]
# GitHub already returns newest-first; keep that order.
if channel == "beta":
for rel in ordered:
if is_pre(rel) and has_asset(rel):
print(tag_of(rel))
sys.exit(0)
sys.stderr.write("no beta release has %s\n" % asset)
sys.exit(1)
for rel in ordered:
if (not is_pre(rel)) and has_asset(rel):
print(tag_of(rel))
sys.exit(0)
# Stable latest is often empty (v0.1.11). Use newest release that has binaries.
for rel in ordered:
if has_asset(rel):
print(tag_of(rel))
sys.exit(0)
sys.stderr.write("no GitHub release has %s\n" % asset)
sys.exit(1)
' "$asset" "$CHANNEL"
}
list_tags_from_html() {
local html="$1"
if command -v python3 >/dev/null 2>&1; then
printf '%s' "$html" | python3 -c '
import re, sys
html = sys.stdin.read()
seen = set()
tags = []
for m in re.finditer(r"anyrouter-dev/cli/releases/tag/(v[0-9][^\"<>\s#?]*)", html):
tag = m.group(1).rstrip("/")
if tag in seen or "/" in tag:
continue
seen.add(tag)
tags.append(tag)
def ver_key(tag):
s = tag[1:] if tag.startswith("v") else tag
core, _, pre = s.partition("-")
nums = []
for p in core.split("."):
try:
nums.append(int(p))
except ValueError:
nums.append(0)
while len(nums) < 3:
nums.append(0)
if pre:
pre_nums = [int(p) for p in re.split(r"[^0-9]+", pre) if p]
return (tuple(nums), 0, tuple(pre_nums))
return (tuple(nums), 1, ())
tags.sort(key=ver_key, reverse=True)
for t in tags:
print(t)
'
return
fi
printf '%s' "$html" | grep -oE 'anyrouter-dev/cli/releases/tag/v[^"<>[:space:]#?]+' \
| sed 's|.*/||' | awk '!s[$0]++'
}
pick_tag_from_html() {
local html tag url
html="$(curl_ua -fsSL "${GITHUB}/releases")" \
|| die "could not fetch ${GITHUB}/releases (set GH_TOKEN to use the GitHub API)"
local tags
tags="$(list_tags_from_html "$html")"
[ -n "$tags" ] || die "no GitHub release tags found at ${GITHUB}/releases"
if [ "$CHANNEL" = "beta" ]; then
while IFS= read -r tag; do
[ -n "$tag" ] || continue
case "$tag" in
*-*)
url="$(download_url_for_tag "$tag")"
if asset_available "$url"; then
printf '%s' "$tag"
return 0
fi
;;
esac
done <<EOF
$tags
EOF
die "no beta release has ${asset}"
fi
# stable: prefer a non-prerelease tag that actually has the asset
while IFS= read -r tag; do
[ -n "$tag" ] || continue
case "$tag" in
*-*) continue ;;
esac
url="$(download_url_for_tag "$tag")"
if asset_available "$url"; then
printf '%s' "$tag"
return 0
fi
done <<EOF
$tags
EOF
echo "setup.sh: GitHub /releases/latest has no ${asset}; using newest release that has binaries" >&2
while IFS= read -r tag; do
[ -n "$tag" ] || continue
url="$(download_url_for_tag "$tag")"
if asset_available "$url"; then
printf '%s' "$tag"
return 0
fi
done <<EOF
$tags
EOF
die "no GitHub release has ${asset}"
}
resolve_download_url() {
local tag url token
if [ -n "$VERSION" ]; then
tag="v${VERSION#v}"
printf '%s' "$(download_url_for_tag "$tag")"
return 0
fi
token="$(github_token || true)"
if [ -n "$token" ]; then
if tag="$(pick_tag_from_api "$token")"; then
[ -n "$tag" ] || die "empty tag from GitHub API"
printf '%s' "$(download_url_for_tag "$tag")"
return 0
fi
echo "setup.sh: authenticated GitHub API failed; listing github.com/releases instead" >&2
fi
if [ "$CHANNEL" = "stable" ]; then
url="${GITHUB}/releases/latest/download/${asset}"
if asset_available "$url"; then
printf '%s' "$url"
return 0
fi
fi
tag="$(pick_tag_from_html)"
[ -n "$tag" ] || die "could not resolve a GitHub release for ${asset}"
printf '%s' "$(download_url_for_tag "$tag")"
}
echo "channel=${CHANNEL} os=${os} arch=${arch}"
url="$(resolve_download_url)"
echo "Downloading ${url}"
tmp="$(mktemp)"
trap 'rm -f "$tmp"' EXIT
if ! curl_ua -fsSL "$url" -o "$tmp"; then
die "download failed: $url"
fi
if [ ! -s "$tmp" ]; then
die "download was empty: $url"
fi
# GitHub 404 pages are HTML; release assets are ELF / Mach-O.
if head -c 16 "$tmp" | grep -q '<'; then
die "download did not look like a binary (got HTML?): $url"
fi
install_bin "$tmp"