-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·235 lines (210 loc) · 5.28 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·235 lines (210 loc) · 5.28 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
#!/bin/sh
set -eu
REPO="${LING_REPO:-LISTENAI/ling}"
BIN="ling"
INSTALL_DIR="${LING_INSTALL_DIR:-$HOME/.local/bin}"
LIBC="${LING_LIBC:-musl}"
TOKEN="${GH_TOKEN:-${GITHUB_TOKEN:-}}"
API_URL="https://api.github.com/repos/$REPO"
say() {
printf '%s\n' "$*"
}
die() {
printf 'error: %s\n' "$*" >&2
exit 1
}
have() {
command -v "$1" >/dev/null 2>&1
}
http_get() {
url="$1"
if have curl; then
if [ -n "$TOKEN" ]; then
curl -fsSL \
-H "Authorization: Bearer $TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"$url"
else
curl -fsSL "$url"
fi
elif have wget; then
if [ -n "$TOKEN" ]; then
wget -qO- \
--header "Authorization: Bearer $TOKEN" \
--header "X-GitHub-Api-Version: 2022-11-28" \
"$url"
else
wget -qO- "$url"
fi
else
die "curl or wget is required"
fi
}
http_download() {
url="$1"
out="$2"
if have curl; then
if [ -n "$TOKEN" ]; then
curl -fL --progress-bar \
-H "Authorization: Bearer $TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-o "$out" "$url"
else
curl -fL --progress-bar -o "$out" "$url"
fi
elif have wget; then
if [ -n "$TOKEN" ]; then
wget -qO "$out" \
--header "Authorization: Bearer $TOKEN" \
--header "X-GitHub-Api-Version: 2022-11-28" \
"$url"
else
wget -qO "$out" "$url"
fi
else
die "curl or wget is required"
fi
}
api_asset_download() {
asset_id="$1"
out="$2"
url="$API_URL/releases/assets/$asset_id"
if have curl; then
curl -fL --progress-bar \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/octet-stream" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-o "$out" "$url"
elif have wget; then
wget -qO "$out" \
--header "Authorization: Bearer $TOKEN" \
--header "Accept: application/octet-stream" \
--header "X-GitHub-Api-Version: 2022-11-28" \
"$url"
else
die "curl or wget is required"
fi
}
normalize_arch() {
case "$1" in
x86_64|amd64) printf 'x86_64' ;;
arm64|aarch64) printf 'aarch64' ;;
*) die "unsupported CPU architecture: $1" ;;
esac
}
detect_target() {
os="$(uname -s)"
arch="$(normalize_arch "$(uname -m)")"
case "$os" in
Darwin)
printf '%s-apple-darwin' "$arch"
;;
Linux)
case "$LIBC" in
musl|gnu) ;;
*) die "unsupported LING_LIBC=$LIBC; expected musl or gnu" ;;
esac
printf '%s-unknown-linux-%s' "$arch" "$LIBC"
;;
*)
die "unsupported OS: $os"
;;
esac
}
latest_version() {
json="$(http_get "$API_URL/releases/latest")"
tag="$(printf '%s\n' "$json" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)"
[ -n "$tag" ] || die "failed to resolve latest release tag for $REPO"
printf '%s' "$tag"
}
load_release_json() {
if [ -z "${release_json:-}" ]; then
release_json="$(http_get "$API_URL/releases/tags/$version")"
fi
}
asset_id_for() {
name="$1"
load_release_json
printf '%s\n' "$release_json" | awk -v name="$name" '
/"id"[[:space:]]*:/ {
line = $0
sub(/^[^0-9]*/, "", line)
sub(/[^0-9].*$/, "", line)
if (line != "") id = line
}
$0 ~ "\"name\"[[:space:]]*:[[:space:]]*\"" name "\"" {
print id
exit
}
'
}
download_release_asset() {
name="$1"
out="$2"
if [ -n "$TOKEN" ]; then
id="$(asset_id_for "$name")"
[ -n "$id" ] || die "release asset not found: $name"
api_asset_download "$id" "$out"
else
http_download "https://github.com/$REPO/releases/download/$version/$name" "$out"
fi
}
sha256_file() {
file="$1"
if have sha256sum; then
sha256sum "$file" | awk '{print tolower($1)}'
elif have shasum; then
shasum -a 256 "$file" | awk '{print tolower($1)}'
else
return 1
fi
}
version="${LING_VERSION:-}"
if [ -z "$version" ]; then
version="$(latest_version)"
fi
target="$(detect_target)"
asset="$BIN-$version-$target.tar.gz"
tmp="$(mktemp -d 2>/dev/null || mktemp -d -t ling-install)"
cleanup() {
rm -rf "$tmp"
}
trap cleanup EXIT INT HUP TERM
archive="$tmp/$asset"
checksums="$tmp/SHA256SUMS"
say "Installing $BIN $version for $target"
say "Downloading $asset"
download_release_asset "$asset" "$archive"
if download_release_asset "SHA256SUMS" "$checksums" >/dev/null 2>&1; then
expected="$(grep "[[:space:]]$asset$" "$checksums" | awk '{print tolower($1)}' | head -n 1)"
if [ -n "$expected" ]; then
actual="$(sha256_file "$archive" || true)"
if [ -n "$actual" ]; then
[ "$expected" = "$actual" ] || die "checksum mismatch for $asset"
say "Checksum verified"
else
say "Skipping checksum verification: sha256sum or shasum not found"
fi
fi
fi
tar -xzf "$archive" -C "$tmp"
src="$tmp/$BIN-$version-$target/$BIN"
[ -f "$src" ] || die "binary not found in archive: $src"
mkdir -p "$INSTALL_DIR"
if have install; then
install -m 755 "$src" "$INSTALL_DIR/$BIN"
else
cp "$src" "$INSTALL_DIR/$BIN"
chmod 755 "$INSTALL_DIR/$BIN"
fi
say "Installed to $INSTALL_DIR/$BIN"
"$INSTALL_DIR/$BIN" --help >/dev/null || die "installed binary failed to run"
case ":$PATH:" in
*":$INSTALL_DIR:"*) ;;
*)
say ""
say "Add $INSTALL_DIR to PATH if 'ling' is not found:"
say " export PATH=\"$INSTALL_DIR:\$PATH\""
;;
esac
say "Done. Try: $BIN --help"