-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·156 lines (132 loc) · 4.31 KB
/
install.sh
File metadata and controls
executable file
·156 lines (132 loc) · 4.31 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
#!/bin/sh
# Install Zerobox - Lightweight, cross-platform process sandboxing. Sandbox any command with file, network, and credential controls.
# GitHub: https://github.com/afshinm/zerobox
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/afshinm/zerobox/main/install.sh | sh
#
# Options:
# ZEROBOX_INSTALL — install directory (default: $HOME/.zerobox)
# ZEROBOX_VERSION — specific version to install (default: latest)
set -eu
main() {
need_cmd uname
need_cmd chmod
need_cmd mkdir
need_cmd tar
local _target
_target="$(detect_target)"
local _version
_version="${ZEROBOX_VERSION:-latest}"
local _install_dir
_install_dir="${ZEROBOX_INSTALL:-$HOME/.zerobox}"
local _bin_dir="$_install_dir/bin"
local _base_url="https://github.com/afshinm/zerobox/releases"
local _url
if [ "$_version" = "latest" ]; then
_url="$_base_url/latest/download/zerobox-${_target}.tar.gz"
else
_url="$_base_url/download/v${_version}/zerobox-${_target}.tar.gz"
fi
local _tmp
_tmp="$(mktemp -d)"
trap "rm -rf '$_tmp'" EXIT
log "Downloading zerobox for $_target"
download "$_url" "$_tmp/zerobox.tar.gz"
log "Extracting to $_bin_dir"
mkdir -p "$_bin_dir"
tar xzf "$_tmp/zerobox.tar.gz" -C "$_tmp"
mv "$_tmp/zerobox" "$_bin_dir/zerobox"
chmod +x "$_bin_dir/zerobox"
# Verify the binary runs.
if ! "$_bin_dir/zerobox" --version >/dev/null 2>&1; then
err "Downloaded binary failed to execute. Try installing from npm instead: npm install -g zerobox"
fi
log "Installed zerobox to $_bin_dir/zerobox"
echo ""
# Check if already in PATH.
case ":${PATH}:" in
*":$_bin_dir:"*) ;;
*)
log "Add zerobox to your PATH:"
echo ""
case "$(basename "${SHELL:-}")" in
fish)
echo " set -Ux fish_user_paths $_bin_dir \$fish_user_paths"
;;
zsh)
echo " echo 'export PATH=\"$_bin_dir:\$PATH\"' >> ~/.zshrc"
;;
*)
echo " echo 'export PATH=\"$_bin_dir:\$PATH\"' >> ~/.bashrc"
;;
esac
echo ""
;;
esac
}
detect_target() {
local _os _arch _target _musl
_os="$(uname -s)"
_arch="$(uname -m)"
case "$_os" in
Darwin)
case "$_arch" in
x86_64)
# Check for Rosetta 2 — prefer native arm64 binary.
if [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || echo 0)" = "1" ]; then
_target="aarch64-apple-darwin"
else
_target="x86_64-apple-darwin"
fi
;;
arm64) _target="aarch64-apple-darwin" ;;
*) err "Unsupported architecture: $_arch" ;;
esac
;;
Linux)
_musl=""
if command -v ldd >/dev/null 2>&1; then
if ldd --version 2>&1 | grep -qi musl; then
_musl="-musl"
fi
elif [ -f /etc/alpine-release ]; then
_musl="-musl"
fi
local _libc="gnu"
if [ -n "$_musl" ]; then _libc="musl"; fi
case "$_arch" in
x86_64 | amd64) _target="x86_64-unknown-linux-${_libc}" ;;
aarch64 | arm64) _target="aarch64-unknown-linux-${_libc}" ;;
*) err "Unsupported architecture: $_arch" ;;
esac
;;
*) err "Unsupported OS: $_os. Install via npm instead: npm install -g zerobox" ;;
esac
echo "$_target"
}
download() {
local _url="$1" _output="$2"
if command -v curl >/dev/null 2>&1; then
curl --fail --location --silent --show-error --output "$_output" "$_url" ||
err "Failed to download $_url"
elif command -v wget >/dev/null 2>&1; then
wget --quiet --output-document="$_output" "$_url" ||
err "Failed to download $_url"
else
err "curl or wget is required to download zerobox"
fi
}
log() {
echo " $1" >&2
}
err() {
echo "error: $1" >&2
exit 1
}
need_cmd() {
if ! command -v "$1" >/dev/null 2>&1; then
err "$1 is required but not found"
fi
}
main "$@"