-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathinstall.sh
More file actions
370 lines (309 loc) · 10.3 KB
/
Copy pathinstall.sh
File metadata and controls
370 lines (309 loc) · 10.3 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
#!/usr/bin/env bash
# sandcat shell installer.
# See https://github.com/VirtusLab/sandcat for the source repository.
# Usage:
# curl -fsSL https://raw.githubusercontent.com/VirtusLab/sandcat/master/install.sh | sh
#
# Env overrides:
# SANDCAT_HOME install root (default: ~/.local/share/sandcat)
# SANDCAT_BIN_DIR launcher symlink dir (default: ~/.local/bin)
# SANDCAT_REF branch/tag/commit to install (default: master)
# SANDCAT_NON_INTERACTIVE skip prompts when set to "true" (default: false)
# No pipefail: dash (sh on Debian/Ubuntu) lacks it, and no pipeline here
# relies on it.
set -eu
# --- Logging helpers ---------------------------------------------------------
info() {
printf '[INFO] %s\n' "$1"
}
warn() {
printf '[WARN] %s\n' "$1" >&2
}
err() {
printf '[ERROR] %s\n' "$1" >&2
}
# --- Cleanup / traps ---------------------------------------------------------
TMP_DIR=""
cleanup() {
if [ -n "${TMP_DIR:-}" ] && [ -d "$TMP_DIR" ]; then
rm -rf "$TMP_DIR"
fi
}
# With set -e, a failing command exits with its status, and the EXIT trap
# then runs cleanup. (An ERR trap would not be portable to dash.)
trap 'cleanup' EXIT
# --- Argument parsing --------------------------------------------------------
usage() {
cat <<'EOF'
Usage: install.sh [--uninstall] [--help]
Install (or update) sandcat CLI from a GitHub tarball snapshot.
Options:
--uninstall Remove sandcat install and launcher symlink
(preserves ~/.config/sandcat/ and Docker state)
--help Print this help and exit
Env overrides:
SANDCAT_HOME install root (default: ~/.local/share/sandcat)
SANDCAT_BIN_DIR launcher symlink dir (default: ~/.local/bin)
SANDCAT_REF branch/tag/commit to install (default: master)
SANDCAT_NON_INTERACTIVE skip prompts when set to "true" (default: false)
Examples:
# Install latest master
curl -fsSL https://raw.githubusercontent.com/VirtusLab/sandcat/master/install.sh | sh
# Pin to a specific tag or commit
curl -fsSL https://.../install.sh | SANDCAT_REF=v1.0.0 sh
curl -fsSL https://.../install.sh | SANDCAT_REF=abc123 sh
# Non-interactive (CI)
curl -fsSL https://.../install.sh | SANDCAT_NON_INTERACTIVE=true sh
# Uninstall
bash <(curl -fsSL https://.../install.sh) --uninstall
EOF
}
UNINSTALL=false
while [ $# -gt 0 ]; do
case "$1" in
--uninstall) UNINSTALL=true; shift ;;
--help|-h) usage; exit 0 ;;
*) err "Unknown argument: $1"; usage; exit 2 ;;
esac
done
# --- Path resolution ---------------------------------------------------------
SANDCAT_HOME="${SANDCAT_HOME:-$HOME/.local/share/sandcat}"
SANDCAT_BIN_DIR="${SANDCAT_BIN_DIR:-$HOME/.local/bin}"
SANDCAT_REF="${SANDCAT_REF:-master}"
SANDCAT_NON_INTERACTIVE="${SANDCAT_NON_INTERACTIVE:-false}"
# --- OS detection ------------------------------------------------------------
detect_os() {
case "$(uname -s)" in
Darwin) printf 'macos'; return ;;
Linux)
if [ -r /etc/os-release ]; then
local distro_id
# shellcheck disable=SC1091
distro_id=$(. /etc/os-release && printf '%s' "${ID:-linux-generic}")
case "$distro_id" in
debian|ubuntu) printf 'debian' ;;
alpine) printf 'alpine' ;;
*) printf 'linux-generic' ;;
esac
else
printf 'linux-generic'
fi
;;
*) printf 'unknown' ;;
esac
}
# --- Prerequisite check ------------------------------------------------------
print_yq_instructions() {
local os=$1
err "sandcat requires yq (Mike Farah's Go variant, https://github.com/mikefarah/yq)."
err "Install with:"
case "$os" in
macos)
err " brew install yq"
;;
debian)
err " snap install yq"
err " (avoid 'apt install yq' — it installs the incompatible Python variant)"
;;
alpine)
err " apk add yq-go"
;;
*)
err " Download the binary from https://github.com/mikefarah/yq/releases"
err " and place it on your PATH."
;;
esac
}
HTTP_CMD=""
check_prerequisites() {
if command -v curl >/dev/null 2>&1; then
HTTP_CMD=curl
elif command -v wget >/dev/null 2>&1; then
HTTP_CMD=wget
else
err "Neither curl nor wget is installed. Install one and retry."
exit 1
fi
if ! command -v tar >/dev/null 2>&1; then
err "tar is required but not installed."
exit 1
fi
if ! command -v yq >/dev/null 2>&1; then
print_yq_instructions "$(detect_os)"
exit 1
fi
if ! yq --version 2>&1 | grep -q mikefarah; then
err "Found yq but it is not the Mike Farah (Go) variant."
print_yq_instructions "$(detect_os)"
exit 1
fi
}
# --- Fetch + extract ---------------------------------------------------------
fetch_tarball() {
local url="https://codeload.github.com/VirtusLab/sandcat/tar.gz/$SANDCAT_REF"
info "Fetching $SANDCAT_REF from codeload.github.com..."
local dest="$TMP_DIR/sandcat.tar.gz"
if [ "$HTTP_CMD" = curl ]; then
if ! curl -fsSL "$url" -o "$dest"; then
err "Failed to fetch tarball. Reference '$SANDCAT_REF' not found in VirtusLab/sandcat (URL: $url)."
exit 1
fi
else
if ! wget -qO "$dest" "$url"; then
err "Failed to fetch tarball. Reference '$SANDCAT_REF' not found in VirtusLab/sandcat (URL: $url)."
exit 1
fi
fi
}
SOURCE_CLI_DIR=""
extract_tarball() {
info "Extracting to $TMP_DIR..."
if ! tar xzf "$TMP_DIR/sandcat.tar.gz" -C "$TMP_DIR"; then
err "Failed to extract tarball."
exit 1
fi
# GitHub codeload wraps the repo tree in a top-level dir named
# sandcat-<ref-slug>. Find the extracted cli/ within.
local found
found=$(find "$TMP_DIR" -maxdepth 3 -type d -name cli -path '*/sandcat-*/cli' | head -n1)
if [ -z "$found" ]; then
err "Extracted tarball has no sandcat-*/cli/ directory. Structure unexpected."
exit 1
fi
SOURCE_CLI_DIR="$found"
}
# --- Install files (two-phase swap) ------------------------------------------
prompt_yes_no() {
local msg=$1
if [ "$SANDCAT_NON_INTERACTIVE" = "true" ]; then
return 0
fi
printf '%s [y/N]: ' "$msg" >&2
local reply
# When piped (curl ... | sh), stdin is the script itself, so ask the
# terminal directly. Without one (CI without SANDCAT_NON_INTERACTIVE),
# the answer stays empty and defaults to "no".
if [ -t 0 ]; then
IFS= read -r reply || reply=""
else
{ IFS= read -r reply < /dev/tty; } 2>/dev/null || reply=""
fi
case "$reply" in
y|Y|yes|YES) return 0 ;;
*) return 1 ;;
esac
}
detect_existing_install() {
if [ -d "$SANDCAT_HOME/cli" ]; then
if ! prompt_yes_no "Sandcat is already installed at $SANDCAT_HOME/cli. Overwrite?"; then
info "Aborted — existing install preserved."
exit 0
fi
fi
# Guard rare case: SANDCAT_HOME itself exists as a file, not a directory.
if [ -e "$SANDCAT_HOME" ] && [ ! -d "$SANDCAT_HOME" ]; then
err "$SANDCAT_HOME already exists as a file, not a directory. Move it and retry."
exit 1
fi
}
install_files() {
mkdir -p "$SANDCAT_HOME"
# Two-phase swap. Guarantees that the user's existing cli/ is only
# replaced by a rename(2), and that on any earlier failure the old
# tree remains intact.
rm -rf "$SANDCAT_HOME/cli.new"
mv "$SOURCE_CLI_DIR" "$SANDCAT_HOME/cli.new"
rm -rf "$SANDCAT_HOME/cli.old"
if [ -d "$SANDCAT_HOME/cli" ]; then
mv "$SANDCAT_HOME/cli" "$SANDCAT_HOME/cli.old"
fi
mv "$SANDCAT_HOME/cli.new" "$SANDCAT_HOME/cli"
rm -rf "$SANDCAT_HOME/cli.old"
printf '%s (installed %s)\n' "$SANDCAT_REF" "$(date +'%F %T')" \
> "$SANDCAT_HOME/cli/.version"
info "Installed sandcat $SANDCAT_REF to $SANDCAT_HOME/cli/"
}
install_symlink() {
mkdir -p "$SANDCAT_BIN_DIR"
# Warn (but proceed) if the launcher path is a non-symlink file — user
# may have another script there. ln -sf will overwrite regardless.
if [ -e "$SANDCAT_BIN_DIR/sandcat" ] && [ ! -L "$SANDCAT_BIN_DIR/sandcat" ]; then
warn "Replacing existing non-symlink file at $SANDCAT_BIN_DIR/sandcat"
fi
ln -sf "$SANDCAT_HOME/cli/bin/sandcat" "$SANDCAT_BIN_DIR/sandcat"
info "Symlinked $SANDCAT_BIN_DIR/sandcat -> $SANDCAT_HOME/cli/bin/sandcat"
}
verify_install() {
if ! "$SANDCAT_BIN_DIR/sandcat" version >/dev/null 2>&1; then
err "Verification failed: '$SANDCAT_BIN_DIR/sandcat version' did not run cleanly."
exit 1
fi
}
path_contains() {
# Portable "is $1 present in colon-separated PATH?"
case ":$PATH:" in
*":$1:"*) return 0 ;;
*) return 1 ;;
esac
}
print_success() {
info "sandcat $SANDCAT_REF installed."
info ""
info "Next steps:"
if ! path_contains "$SANDCAT_BIN_DIR"; then
info " Add $SANDCAT_BIN_DIR to your PATH (append to ~/.bashrc or ~/.zshrc):"
info " export PATH=\"$SANDCAT_BIN_DIR:\$PATH\""
fi
info " Then in your project directory: sandcat init"
}
# --- Root-user warning -------------------------------------------------------
should_warn_root() {
local uid=$1
local sandcat_home=$2
[ "$uid" = "0" ] || return 1
case "$sandcat_home" in
/root/*) return 0 ;;
*) return 1 ;;
esac
}
warn_if_root_without_overrides() {
if should_warn_root "$(id -u)" "$SANDCAT_HOME"; then
warn "Running as root: sandcat will be installed under /root/. Set SANDCAT_HOME and SANDCAT_BIN_DIR for a system-wide install (e.g. SANDCAT_HOME=/opt/sandcat SANDCAT_BIN_DIR=/usr/local/bin)."
fi
}
# --- Uninstall ---------------------------------------------------------------
uninstall() {
if [ ! -d "$SANDCAT_HOME/cli" ] && [ ! -L "$SANDCAT_BIN_DIR/sandcat" ]; then
info "Sandcat is not installed at $SANDCAT_HOME (nothing to remove)."
return 0
fi
if ! prompt_yes_no "Remove sandcat install at $SANDCAT_HOME/cli/ and launcher at $SANDCAT_BIN_DIR/sandcat?"; then
info "Aborted — install preserved."
return 0
fi
rm -f "$SANDCAT_BIN_DIR/sandcat"
rm -rf "$SANDCAT_HOME/cli"
# Also sweep any stale swap dirs from an aborted install.
rm -rf "$SANDCAT_HOME/cli.new" "$SANDCAT_HOME/cli.old"
if [ -d "$SANDCAT_HOME" ] && [ -z "$(ls -A "$SANDCAT_HOME" 2>/dev/null)" ]; then
rmdir "$SANDCAT_HOME"
fi
info "Removed sandcat install ($SANDCAT_HOME/cli/) and launcher ($SANDCAT_BIN_DIR/sandcat)."
info "User settings preserved at ~/.config/sandcat/ (delete manually if desired)."
info "Docker images and cache volumes untouched. Use 'sandcat cache rm --all' or 'docker rmi' if wanted."
}
# --- Dispatch ----------------------------------------------------------------
if [ "$UNINSTALL" = "true" ]; then
uninstall
exit 0
fi
check_prerequisites
warn_if_root_without_overrides
TMP_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t sandcat-install)
detect_existing_install
fetch_tarball
extract_tarball
install_files
install_symlink
verify_install
print_success