forked from NVIDIA/OpenShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·447 lines (375 loc) · 12.1 KB
/
install.sh
File metadata and controls
executable file
·447 lines (375 loc) · 12.1 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
#!/bin/sh
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Install the OpenShell CLI binary.
#
# Usage:
# curl -LsSf https://raw.githubusercontent.com/NVIDIA/OpenShell/main/install.sh | sh
#
# Or run directly:
# ./install.sh
#
# Environment variables:
# OPENSHELL_VERSION - Release tag to install (default: latest tagged release)
# OPENSHELL_INSTALL_DIR - Directory to install into (default: ~/.local/bin)
#
# CLI flags:
# --help - Print usage information
# --no-modify-path - Skip PATH modification in shell profiles
#
set -eu
APP_NAME="openshell"
REPO="NVIDIA/OpenShell"
GITHUB_URL="https://github.com/${REPO}"
NO_MODIFY_PATH=0
# ---------------------------------------------------------------------------
# Logging
# ---------------------------------------------------------------------------
info() {
printf '%s: %s\n' "$APP_NAME" "$*" >&2
}
warn() {
printf '%s: warning: %s\n' "$APP_NAME" "$*" >&2
}
error() {
printf '%s: error: %s\n' "$APP_NAME" "$*" >&2
exit 1
}
# ---------------------------------------------------------------------------
# Usage
# ---------------------------------------------------------------------------
usage() {
cat <<EOF
install.sh — Install the OpenShell CLI
USAGE:
curl -LsSf https://raw.githubusercontent.com/NVIDIA/OpenShell/main/install.sh | sh
./install.sh [OPTIONS]
OPTIONS:
--no-modify-path Don't add the install directory to PATH
--help Print this help message
ENVIRONMENT VARIABLES:
OPENSHELL_VERSION Release tag to install (default: latest tagged release)
OPENSHELL_INSTALL_DIR Directory to install into (default: ~/.local/bin)
EXAMPLES:
# Install latest release
curl -LsSf https://raw.githubusercontent.com/NVIDIA/OpenShell/main/install.sh | sh
# Install a specific version
OPENSHELL_VERSION=v0.0.4 curl -LsSf https://raw.githubusercontent.com/NVIDIA/OpenShell/main/install.sh | sh
# Install to /usr/local/bin
OPENSHELL_INSTALL_DIR=/usr/local/bin curl -LsSf https://raw.githubusercontent.com/NVIDIA/OpenShell/main/install.sh | sh
EOF
}
# ---------------------------------------------------------------------------
# HTTP helpers — prefer curl, fall back to wget
# ---------------------------------------------------------------------------
has_cmd() {
command -v "$1" >/dev/null 2>&1
}
check_downloader() {
if has_cmd curl; then
return 0
elif has_cmd wget; then
return 0
else
error "either 'curl' or 'wget' is required to download files"
fi
}
# Download a URL to a file. Outputs nothing on success.
download() {
_url="$1"
_output="$2"
if has_cmd curl; then
curl -fLsS --retry 3 -o "$_output" "$_url"
elif has_cmd wget; then
wget -q --tries=3 -O "$_output" "$_url"
fi
}
# Follow a URL and print the final resolved URL (for detecting redirect targets).
resolve_redirect() {
_url="$1"
if has_cmd curl; then
curl -fLsS -o /dev/null -w '%{url_effective}' "$_url"
elif has_cmd wget; then
# wget --spider follows redirects and prints the final URL
wget --spider -q --max-redirect=10 "$_url" 2>&1 | grep -oP 'Location: \K\S+' | tail -1
fi
}
# ---------------------------------------------------------------------------
# Platform detection
# ---------------------------------------------------------------------------
get_os() {
case "$(uname -s)" in
Darwin) echo "apple-darwin" ;;
Linux) echo "unknown-linux-musl" ;;
*) error "unsupported OS: $(uname -s)" ;;
esac
}
get_arch() {
case "$(uname -m)" in
x86_64|amd64) echo "x86_64" ;;
aarch64|arm64) echo "aarch64" ;;
*) error "unsupported architecture: $(uname -m)" ;;
esac
}
get_target() {
_arch="$(get_arch)"
_os="$(get_os)"
_target="${_arch}-${_os}"
# Only these targets have published binaries.
case "$_target" in
x86_64-unknown-linux-musl|aarch64-unknown-linux-musl|aarch64-apple-darwin) ;;
x86_64-apple-darwin) error "macOS x86_64 is not supported; use Apple Silicon (aarch64) or Rosetta 2" ;;
*) error "no prebuilt binary for $_target" ;;
esac
echo "$_target"
}
# ---------------------------------------------------------------------------
# Version resolution
# ---------------------------------------------------------------------------
resolve_version() {
if [ -n "${OPENSHELL_VERSION:-}" ]; then
echo "$OPENSHELL_VERSION"
return 0
fi
# Resolve "latest" by following the GitHub releases/latest redirect.
# GitHub redirects /releases/latest -> /releases/tag/<tag>
info "resolving latest version..."
_latest_url="${GITHUB_URL}/releases/latest"
_resolved="$(resolve_redirect "$_latest_url")" || error "failed to resolve latest release from ${_latest_url}"
# Extract the tag from the resolved URL: .../releases/tag/v0.0.4 -> v0.0.4
_version="${_resolved##*/}"
if [ -z "$_version" ] || [ "$_version" = "latest" ]; then
error "could not determine latest release version (resolved URL: ${_resolved})"
fi
echo "$_version"
}
# ---------------------------------------------------------------------------
# Checksum verification
# ---------------------------------------------------------------------------
verify_checksum() {
_archive="$1"
_checksums="$2"
_filename="$3"
_expected="$(grep "$_filename" "$_checksums" | awk '{print $1}')"
if [ -z "$_expected" ]; then
warn "no checksum found for $_filename, skipping verification"
return 0
fi
if has_cmd shasum; then
echo "$_expected $_archive" | shasum -a 256 -c --quiet 2>/dev/null
elif has_cmd sha256sum; then
echo "$_expected $_archive" | sha256sum -c --quiet 2>/dev/null
else
warn "sha256sum/shasum not found, skipping checksum verification"
return 0
fi
}
# ---------------------------------------------------------------------------
# Install location and PATH management
# ---------------------------------------------------------------------------
get_home() {
if [ -n "${HOME:-}" ]; then
echo "$HOME"
elif [ -n "${USER:-}" ]; then
getent passwd "$USER" | cut -d: -f6
else
getent passwd "$(id -un)" | cut -d: -f6
fi
}
get_default_install_dir() {
if [ -n "${XDG_BIN_HOME:-}" ]; then
echo "$XDG_BIN_HOME"
else
_home="$(get_home)"
echo "${_home}/.local/bin"
fi
}
# Check if a directory is already on PATH.
is_on_path() {
_dir="$1"
case ":${PATH}:" in
*":${_dir}:"*) return 0 ;;
*) return 1 ;;
esac
}
# Write a small env script that conditionally prepends the install dir to PATH.
write_env_script_sh() {
_install_dir_expr="$1"
_env_script="$2"
cat <<ENVEOF > "$_env_script"
#!/bin/sh
# Add OpenShell to PATH if not already present
case ":\${PATH}:" in
*:"${_install_dir_expr}":*)
;;
*)
export PATH="${_install_dir_expr}:\$PATH"
;;
esac
ENVEOF
}
write_env_script_fish() {
_install_dir_expr="$1"
_env_script="$2"
cat <<ENVEOF > "$_env_script"
# Add OpenShell to PATH if not already present
if not contains "${_install_dir_expr}" \$PATH
set -gx PATH "${_install_dir_expr}" \$PATH
end
ENVEOF
}
# Add a `. /path/to/env` line to a shell rc file if not already present.
add_source_line() {
_env_script_path="$1"
_rcfile="$2"
_shell_type="$3"
if [ "$_shell_type" = "fish" ]; then
_line="source \"${_env_script_path}\""
else
_line=". \"${_env_script_path}\""
fi
# Check if line already exists
if [ -f "$_rcfile" ] && grep -qF "$_line" "$_rcfile" 2>/dev/null; then
return 0
fi
# Append with a leading newline in case the file doesn't end with one
printf '\n%s\n' "$_line" >> "$_rcfile"
return 1
}
# Set up PATH modification in common shell rc files.
setup_path() {
_install_dir="$1"
_home="$(get_home)"
_env_script="${_install_dir}/env"
_fish_env_script="${_install_dir}/env.fish"
_needs_source=0
# Replace $HOME in the expression for late-bound references in rc files
if [ -n "${HOME:-}" ]; then
# shellcheck disable=SC2016
_install_dir_expr='$HOME'"${_install_dir#"$_home"}"
else
_install_dir_expr="$_install_dir"
fi
# Write the env scripts
write_env_script_sh "$_install_dir_expr" "$_env_script"
write_env_script_fish "$_install_dir_expr" "$_fish_env_script"
# POSIX shells: .profile, .bashrc, .bash_profile, .zshrc, .zshenv
for _rcfile_rel in .profile .bashrc .bash_profile .zshrc .zshenv; do
_rcdir="$_home"
# zsh respects ZDOTDIR
case "$_rcfile_rel" in
.zsh*) _rcdir="${ZDOTDIR:-$_home}" ;;
esac
_rcfile="${_rcdir}/${_rcfile_rel}"
if [ -f "$_rcfile" ]; then
if ! add_source_line "$_env_script" "$_rcfile" "sh"; then
_needs_source=1
fi
fi
done
# If none of the above existed, create .profile
if [ "$_needs_source" = "0" ]; then
_found_any=0
for _rcfile_rel in .profile .bashrc .bash_profile .zshrc .zshenv; do
if [ -f "${_home}/${_rcfile_rel}" ]; then
_found_any=1
break
fi
done
if [ "$_found_any" = "0" ]; then
if ! add_source_line "$_env_script" "${_home}/.profile" "sh"; then
_needs_source=1
fi
fi
fi
# Fish shell
_fish_conf_dir="${_home}/.config/fish/conf.d"
if [ -d "${_home}/.config/fish" ]; then
mkdir -p "$_fish_conf_dir"
add_source_line "$_fish_env_script" "${_fish_conf_dir}/${APP_NAME}.env.fish" "fish" || true
fi
# GitHub Actions: write to GITHUB_PATH for CI environments
if [ -n "${GITHUB_PATH:-}" ]; then
echo "$_install_dir" >> "$GITHUB_PATH"
fi
if [ "$_needs_source" = "1" ] || ! is_on_path "$_install_dir"; then
echo ""
info "to add ${APP_NAME} to your PATH, restart your shell or run:"
info ""
info " source \"${_env_script}\" (sh, bash, zsh)"
if [ -d "${_home}/.config/fish" ]; then
info " source \"${_fish_env_script}\" (fish)"
fi
fi
}
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
main() {
# Parse CLI flags
for arg in "$@"; do
case "$arg" in
--help)
usage
exit 0
;;
--no-modify-path)
NO_MODIFY_PATH=1
;;
*)
error "unknown option: $arg"
;;
esac
done
check_downloader
_version="$(resolve_version)"
_target="$(get_target)"
_filename="${APP_NAME}-${_target}.tar.gz"
_download_url="${GITHUB_URL}/releases/download/${_version}/${_filename}"
_checksums_url="${GITHUB_URL}/releases/download/${_version}/${APP_NAME}-checksums-sha256.txt"
# Determine install directory
_using_default_dir=0
if [ -n "${OPENSHELL_INSTALL_DIR:-}" ]; then
_install_dir="$OPENSHELL_INSTALL_DIR"
else
_install_dir="$(get_default_install_dir)"
_using_default_dir=1
fi
info "downloading ${APP_NAME} ${_version} (${_target})..."
_tmpdir="$(mktemp -d)"
trap 'rm -rf "$_tmpdir"' EXIT
if ! download "$_download_url" "${_tmpdir}/${_filename}"; then
error "failed to download ${_download_url}"
fi
# Verify checksum
info "verifying checksum..."
if download "$_checksums_url" "${_tmpdir}/checksums.txt"; then
if ! verify_checksum "${_tmpdir}/${_filename}" "${_tmpdir}/checksums.txt" "$_filename"; then
error "checksum verification failed for ${_filename}"
fi
else
warn "could not download checksums file, skipping verification"
fi
# Extract
info "extracting..."
tar -xzf "${_tmpdir}/${_filename}" -C "${_tmpdir}"
# Install
mkdir -p "$_install_dir" 2>/dev/null || true
if [ -w "$_install_dir" ] || mkdir -p "$_install_dir" 2>/dev/null; then
install -m 755 "${_tmpdir}/${APP_NAME}" "${_install_dir}/${APP_NAME}"
else
info "elevated permissions required to install to ${_install_dir}"
sudo mkdir -p "$_install_dir"
sudo install -m 755 "${_tmpdir}/${APP_NAME}" "${_install_dir}/${APP_NAME}"
fi
_installed_version="$("${_install_dir}/${APP_NAME}" --version 2>/dev/null || echo "${_version}")"
info "installed ${APP_NAME} ${_installed_version} to ${_install_dir}/${APP_NAME}"
# Set up PATH for default install location
if [ "$_using_default_dir" = "1" ] && [ "$NO_MODIFY_PATH" = "0" ]; then
if ! is_on_path "$_install_dir"; then
setup_path "$_install_dir"
fi
fi
}
main "$@"