-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·370 lines (324 loc) · 9.44 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·370 lines (324 loc) · 9.44 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
#!/bin/bash
set -e
# Installation script for fetch
# Usage: curl -fsSL https://raw.githubusercontent.com/ryanfowler/fetch/main/install.sh | bash
# curl -fsSL https://raw.githubusercontent.com/ryanfowler/fetch/main/install.sh | bash -s -- --completions
RESET=""
BOLD=""
DIM=""
RED=""
GREEN=""
YELLOW=""
# Set escape sequences if stderr is a terminal.
if [ -t 2 ]; then
RESET="\033[0m"
BOLD="\033[1m"
DIM="\033[2m"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
fi
# Print info message.
info() {
echo -e "${BOLD}${GREEN}info${RESET}: $1"
}
# Print warning message.
warning() {
echo -e "${BOLD}${YELLOW}warning${RESET}: $1"
}
# Print error message.
error() {
echo -e "${BOLD}${RED}error${RESET}: $1"
}
# Print compile from source message.
compile_msg() {
echo -e "\nTry compiling from source by running: '${DIM}cargo install --git https://github.com/ryanfowler/fetch --locked${RESET}'"
}
usage() {
cat <<'EOF'
Usage:
curl -fsSL https://raw.githubusercontent.com/ryanfowler/fetch/main/install.sh | bash
curl -fsSL https://raw.githubusercontent.com/ryanfowler/fetch/main/install.sh | bash -s -- --completions
Options:
--completions Install shell completions into the detected shell config.
--no-completions Do not modify shell completion files (default).
-h, --help Print this help.
Environment:
FETCH_INSTALL_COMPLETIONS=1 is equivalent to --completions.
FETCH_INSTALL_DIR overrides the installation directory.
EOF
}
sha256_file() {
if command -v sha256sum &> /dev/null; then
sha256sum "$1" | awk '{ print tolower($1) }'
elif command -v shasum &> /dev/null; then
shasum -a 256 "$1" | awk '{ print tolower($1) }'
elif command -v openssl &> /dev/null; then
openssl dgst -sha256 "$1" | awk '{ print tolower($NF) }'
else
return 1
fi
}
parse_sha256_checksum() {
awk '
{ contents = contents $0 "\n" }
END {
sub(/^[[:space:]]+/, "", contents)
print tolower(substr(contents, 1, 64))
}
' "$1"
}
install_completions() {
case "$SHELL" in
*/bash)
# shellcheck disable=SC2016
COMPLETION_CMD='eval "$(fetch --complete=bash)"'
if ! grep -qF "$COMPLETION_CMD" "$HOME/.bashrc" 2>/dev/null; then
printf '\n# fetch completions\n%s\n' "$COMPLETION_CMD" >> "$HOME/.bashrc"
info "completions appended to '${DIM}${HOME}/.bashrc${RESET}'"
fi
;;
*/fish)
mkdir -p "$HOME/.config/fish/completions"
"$INSTALL_DIR/fetch" --complete=fish > "$HOME/.config/fish/completions/fetch.fish"
info "completions installed to '${DIM}${HOME}/.config/fish/completions/fetch.fish${RESET}'"
;;
*/zsh)
# shellcheck disable=SC2016
COMPLETION_CMD='eval "$(fetch --complete=zsh)"'
if ! grep -qF "$COMPLETION_CMD" "$HOME/.zshrc" 2>/dev/null; then
printf '\n# fetch completions\n%s\n' "$COMPLETION_CMD" >> "$HOME/.zshrc"
info "completions appended to '${DIM}${HOME}/.zshrc${RESET}'"
fi
;;
*)
warning "completions were not installed because SHELL is not bash, fish, or zsh"
print_completion_commands
;;
esac
}
print_completion_commands() {
cat <<'EOF'
Shell completions were not installed automatically.
To enable completions, run the command for your shell:
# Bash
echo 'eval "$(fetch --complete bash)"' >> ~/.bashrc
# Zsh
echo 'eval "$(fetch --complete zsh)"' >> ~/.zshrc
# Fish
mkdir -p ~/.config/fish/completions
fetch --complete fish > ~/.config/fish/completions/fetch.fish
EOF
}
INSTALL_COMPLETIONS="${FETCH_INSTALL_COMPLETIONS:-0}"
while [ "$#" -gt 0 ]; do
case "$1" in
--completions)
INSTALL_COMPLETIONS=1
;;
--no-completions)
INSTALL_COMPLETIONS=0
;;
-h|--help)
usage
exit 0
;;
*)
error "unknown install option: $1"
usage
exit 1
;;
esac
shift
done
case "$INSTALL_COMPLETIONS" in
1|true|TRUE|yes|YES|on|ON)
INSTALL_COMPLETIONS=true
;;
0|false|FALSE|no|NO|off|OFF|"")
INSTALL_COMPLETIONS=false
;;
*)
error "FETCH_INSTALL_COMPLETIONS must be 1, 0, true, false, yes, no, on, or off"
exit 1
;;
esac
# Determine OS and architecture.
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
case "$OS" in
darwin) OS="darwin" ;;
linux) OS="linux" ;;
*)
error "platform not supported by install script: $OS/$ARCH"
compile_msg
exit 1
;;
esac
case "$ARCH" in
x86_64|amd64) ARCH="amd64" ;;
aarch64|arm64) ARCH="arm64" ;;
*)
error "platform not supported by install script: $OS/$ARCH"
compile_msg
exit 1
;;
esac
PLATFORM="${OS}-${ARCH}"
# Fetch the latest release asset.
info "fetching latest release tag"
if ! command -v curl &> /dev/null; then
error "curl is required but not installed"
exit 1
fi
if ! command -v sha256sum &> /dev/null &&
! command -v shasum &> /dev/null &&
! command -v openssl &> /dev/null; then
error "sha256sum, shasum, or openssl is required to verify the downloaded artifact"
exit 1
fi
HAS_JQ=false
if command -v jq &> /dev/null; then
HAS_JQ=true
fi
RELEASE_JSON=$(curl -fsSL https://api.github.com/repos/ryanfowler/fetch/releases/latest)
# Parse the version tag.
VERSION=""
if $HAS_JQ; then
VERSION=$(echo "$RELEASE_JSON" | jq -r .tag_name)
else
VERSION=$(echo "$RELEASE_JSON" | grep -o '"tag_name": *"[^"]*"' | sed 's/"tag_name": *"//;s/"//')
fi
if [ -z "$VERSION" ]; then
error "unable to determine the latest version"
exit 1
fi
# Parse the artifact and checksum urls.
ARTIFACT_NAME="fetch-${VERSION}-${PLATFORM}.tar.gz"
CHECKSUM_NAME="${ARTIFACT_NAME}.sha256"
DOWNLOAD_URL=""
CHECKSUM_URL=""
if $HAS_JQ; then
DOWNLOAD_URL=$(echo "$RELEASE_JSON" | jq -r --arg name "$ARTIFACT_NAME" '.assets[] | select(.name == $name) | .browser_download_url')
CHECKSUM_URL=$(echo "$RELEASE_JSON" | jq -r --arg name "$CHECKSUM_NAME" '.assets[] | select(.name == $name) | .browser_download_url')
else
DOWNLOAD_URL="https://github.com/ryanfowler/fetch/releases/download/${VERSION}/${ARTIFACT_NAME}"
CHECKSUM_URL="${DOWNLOAD_URL}.sha256"
fi
if [ -z "$DOWNLOAD_URL" ]; then
error "no release artifact found for ${OS}/${ARCH}"
exit 1
fi
if [ -z "$CHECKSUM_URL" ]; then
error "no checksum sidecar found for ${ARTIFACT_NAME}"
exit 1
fi
# Create temporary directory.
TMP_DIR=$(mktemp -d)
STAGED_PATH=""
cleanup() {
rm -rf "$TMP_DIR"
if [ -n "$STAGED_PATH" ]; then
rm -f "$STAGED_PATH"
fi
}
trap cleanup EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
BINARY_PATH="${TMP_DIR}/fetch"
ARCHIVE_PATH="${BINARY_PATH}.tar.gz"
CHECKSUM_PATH="${ARCHIVE_PATH}.sha256"
# Download the artifact.
info "downloading latest version (${VERSION})"
if ! curl -fsSL "$DOWNLOAD_URL" -o "$ARCHIVE_PATH"; then
error "unable to download artifact"
exit 1
fi
if ! curl -fsSL "$CHECKSUM_URL" -o "$CHECKSUM_PATH"; then
error "unable to download artifact checksum"
exit 1
fi
CHECKSUM_BYTES=$(wc -c < "$CHECKSUM_PATH" | tr -d '[:space:]')
if [ "${CHECKSUM_BYTES:-0}" -gt 1024 ]; then
error "artifact checksum sidecar is too large"
exit 1
fi
EXPECTED_SHA=$(parse_sha256_checksum "$CHECKSUM_PATH")
if ! printf '%s' "$EXPECTED_SHA" | grep -Eq '^[0-9a-f]{64}$'; then
error "artifact checksum sidecar does not start with a SHA-256 digest"
exit 1
fi
if ! ACTUAL_SHA=$(sha256_file "$ARCHIVE_PATH"); then
error "unable to calculate artifact checksum"
exit 1
fi
if [ "$ACTUAL_SHA" != "$EXPECTED_SHA" ]; then
error "artifact checksum mismatch for ${ARTIFACT_NAME}: expected ${EXPECTED_SHA}, got ${ACTUAL_SHA}"
exit 1
fi
info "verified artifact checksum"
EXTRACTED="${TMP_DIR}/fetch.new"
if ! tar -xOf "$ARCHIVE_PATH" fetch > "$EXTRACTED" 2>/dev/null &&
! tar -xOf "$ARCHIVE_PATH" ./fetch > "$EXTRACTED" 2>/dev/null; then
error "binary not found in archive"
exit 1
fi
mv "$EXTRACTED" "$BINARY_PATH"
chmod 755 "$BINARY_PATH"
# Determine installation directory.
if [ -n "${FETCH_INSTALL_DIR:-}" ]; then
INSTALL_DIR="$FETCH_INSTALL_DIR"
elif [ -w "/usr/local/bin" ]; then
# Can write to /usr/local/bin.
INSTALL_DIR="/usr/local/bin"
else
# Use home directory.
INSTALL_DIR="$HOME/.local/bin"
fi
if ! mkdir -p "$INSTALL_DIR"; then
error "unable to create installation directory '${INSTALL_DIR}'"
exit 1
fi
# Stage on the destination filesystem so replacing an existing installation is
# one atomic rename. Keep STAGED_PATH set until the rename succeeds so the exit
# trap removes partial copies after errors or interruption.
if ! STAGED_PATH=$(mktemp "${INSTALL_DIR}/.fetch.install.XXXXXX"); then
error "unable to create temporary file in installation directory '${INSTALL_DIR}'"
exit 1
fi
if ! cp "$BINARY_PATH" "$STAGED_PATH"; then
error "unable to stage fetch in installation directory '${INSTALL_DIR}'"
exit 1
fi
if ! chmod 755 "$STAGED_PATH"; then
error "unable to make staged fetch executable"
exit 1
fi
if ! "$STAGED_PATH" --version > /dev/null 2>&1; then
error "downloaded fetch binary failed validation"
exit 1
fi
TARGET_PATH="$INSTALL_DIR/fetch"
if [ -d "$TARGET_PATH" ]; then
error "installation destination '${TARGET_PATH}' is a directory"
exit 1
fi
if ! mv -f "$STAGED_PATH" "$TARGET_PATH"; then
error "unable to replace '${TARGET_PATH}'"
exit 1
fi
STAGED_PATH=""
info "fetch successfully installed to '${DIM}${TARGET_PATH}${RESET}'"
if [ "$INSTALL_COMPLETIONS" = true ]; then
install_completions
else
print_completion_commands
fi
# Clean up.
rm -rf "$TMP_DIR"
# Verify installation.
if ! command -v fetch &> /dev/null; then
echo ""
warning "you may need to add '${DIM}${INSTALL_DIR}${RESET}' to your PATH"
fi