-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Expand file tree
/
Copy pathinstall.sh
More file actions
394 lines (349 loc) · 12.7 KB
/
Copy pathinstall.sh
File metadata and controls
394 lines (349 loc) · 12.7 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
#!/usr/bin/env bash
# Claurst installer for Linux and macOS.
#
# Usage (one-liner):
# curl -fsSL https://github.com/Kuberwastaken/claurst/releases/latest/download/install.sh | bash
#
# Or download and run locally:
# curl -fsSL -O https://github.com/Kuberwastaken/claurst/releases/latest/download/install.sh
# chmod +x install.sh
# ./install.sh
set -euo pipefail
APP=claurst
REPO=Kuberwastaken/claurst
# ANSI colours
MUTED='\033[0;2m'
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[38;5;214m'
NC='\033[0m'
usage() {
cat <<EOF
Claurst installer
Usage: install.sh [options]
Options:
-h, --help Display this help message
-v, --version <version> Install a specific version (e.g., 0.1.0 or v0.1.0)
-b, --binary <path> Install from a local binary instead of downloading
--no-modify-path Don't modify shell config files (.zshrc, .bashrc, etc.)
--install-dir <dir> Override install location (default: ${XDG_BIN_HOME:-~/.local/bin})
Examples:
curl -fsSL https://github.com/Kuberwastaken/claurst/releases/latest/download/install.sh | bash
./install.sh --version 0.1.0
./install.sh --binary /path/to/claurst
EOF
}
print_message() {
local level=$1
local message=$2
local color=""
case "$level" in
info) color="$NC" ;;
success) color="$GREEN" ;;
warning) color="$ORANGE" ;;
error) color="$RED" ;;
*) color="$NC" ;;
esac
printf "${color}%b${NC}\n" "$message"
}
requested_version=${VERSION:-}
no_modify_path=false
binary_path=""
install_dir_override=""
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
-v|--version)
if [[ -n "${2:-}" ]]; then
requested_version="$2"
shift 2
else
print_message error "Error: --version requires a version argument"
exit 1
fi
;;
-b|--binary)
if [[ -n "${2:-}" ]]; then
binary_path="$2"
shift 2
else
print_message error "Error: --binary requires a path argument"
exit 1
fi
;;
--no-modify-path)
no_modify_path=true
shift
;;
--install-dir)
if [[ -n "${2:-}" ]]; then
install_dir_override="$2"
shift 2
else
print_message error "Error: --install-dir requires a path argument"
exit 1
fi
;;
*)
print_message warning "Warning: Unknown option '$1'"
shift
;;
esac
done
INSTALL_DIR="${install_dir_override:-${XDG_BIN_HOME:-$HOME/.local/bin}}"
mkdir -p "$INSTALL_DIR"
# ----- Detect platform & arch -----
detect_target() {
local raw_os arch
raw_os=$(uname -s)
case "$raw_os" in
Darwin*) os="macos" ;;
Linux*) os="linux" ;;
MINGW*|MSYS*|CYGWIN*)
print_message error "Detected Windows-like environment ($raw_os)."
print_message info "Run install.ps1 in PowerShell instead:"
print_message info " irm https://github.com/${REPO}/releases/latest/download/install.ps1 | iex"
exit 1
;;
*)
print_message error "Unsupported OS: $raw_os"
exit 1
;;
esac
arch=$(uname -m)
case "$arch" in
x86_64|amd64) arch="x86_64" ;;
aarch64|arm64) arch="aarch64" ;;
*)
print_message error "Unsupported architecture: $arch"
exit 1
;;
esac
# Apple Silicon under Rosetta: prefer the native arm64 binary.
if [ "$os" = "macos" ] && [ "$arch" = "x86_64" ]; then
rosetta_flag=$(sysctl -n sysctl.proc_translated 2>/dev/null || echo 0)
if [ "$rosetta_flag" = "1" ]; then
arch="aarch64"
fi
fi
target="${os}-${arch}"
}
# ----- Pre-flight: required tools -----
check_required_tools() {
local missing=()
command -v curl >/dev/null 2>&1 || missing+=("curl")
command -v tar >/dev/null 2>&1 || missing+=("tar")
if [ "${#missing[@]}" -gt 0 ]; then
print_message error "Missing required tools: ${missing[*]}"
print_message info "Please install them and try again."
exit 1
fi
}
resolve_version() {
if [[ -n "$requested_version" ]]; then
# Strip leading 'v' if present
requested_version="${requested_version#v}"
specific_version="$requested_version"
else
# Fetch latest version from GitHub API. Use sed instead of jq for portability.
specific_version=$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" \
| sed -n 's/.*"tag_name": *"v\{0,1\}\([^"]*\)".*/\1/p' \
| head -n 1)
if [[ -z "$specific_version" ]]; then
print_message error "Failed to fetch latest version from GitHub API"
exit 1
fi
fi
}
# ----- Already-installed check -----
check_existing_install() {
if command -v "$APP" >/dev/null 2>&1; then
existing_path=$(command -v "$APP")
installed_version=$("$APP" --version 2>/dev/null | awk '{print $NF}' || echo "unknown")
if [[ "$installed_version" == "$specific_version" ]]; then
print_message info "${MUTED}Version ${NC}$specific_version${MUTED} already installed at ${NC}$existing_path"
print_message info "${MUTED}Use --version to install a different version, or pass a different one to upgrade.${NC}"
exit 0
fi
print_message info "${MUTED}Found existing claurst at ${NC}$existing_path${MUTED} (v$installed_version) - upgrading to v$specific_version${NC}"
fi
}
# ----- Download & extract -----
download_and_install() {
local archive="${APP}-${target}.tar.gz"
local url="https://github.com/${REPO}/releases/download/v${specific_version}/${archive}"
local tmp_dir
tmp_dir=$(mktemp -d -t claurst-install-XXXXXX)
trap "rm -rf '$tmp_dir'" EXIT
print_message info "${MUTED}Installing ${NC}${APP} ${MUTED}v${NC}${specific_version} ${MUTED}(${target})${NC}"
print_message info "${MUTED}Downloading ${NC}${url}"
if ! curl -fL --progress-bar -o "$tmp_dir/$archive" "$url"; then
print_message error "Download failed."
print_message info "Check that release v${specific_version} exists for ${target}:"
print_message info " https://github.com/${REPO}/releases/tag/v${specific_version}"
exit 1
fi
# ----- Verify checksum (supply-chain integrity) -----
# Fetch SHA256SUMS from the same release and verify the archive before we
# extract and run it. Older releases may not ship SHA256SUMS — in that
# case we warn and continue so existing installs keep working. But if the
# file IS present and the hash does NOT match, we abort hard.
local sums_url="https://github.com/${REPO}/releases/download/v${specific_version}/SHA256SUMS"
if curl -fsSL -o "$tmp_dir/SHA256SUMS" "$sums_url" 2>/dev/null; then
# sha256sum emits "<hash> <filename>" (two spaces); awk collapses the
# whitespace so $1=hash, $2=bare filename. Match on the bare archive
# name (not the full temp path).
local expected actual
expected=$(awk -v f="$archive" '$2 == f {print $1}' "$tmp_dir/SHA256SUMS" | head -n 1)
if [[ -z "$expected" ]]; then
print_message warning "Warning: no checksum listed for ${archive} in SHA256SUMS - skipping verification."
else
if command -v sha256sum >/dev/null 2>&1; then
actual=$(sha256sum "$tmp_dir/$archive" | awk '{print $1}')
elif command -v shasum >/dev/null 2>&1; then
actual=$(shasum -a 256 "$tmp_dir/$archive" | awk '{print $1}')
else
actual=""
fi
if [[ -z "$actual" ]]; then
print_message warning "Warning: no sha256sum/shasum tool available - skipping checksum verification."
elif [[ "$actual" != "$expected" ]]; then
print_message error "Checksum verification FAILED for ${archive}"
print_message info " expected: $expected"
print_message info " actual: $actual"
print_message info "The download may be corrupted or tampered with. Aborting."
exit 1
else
print_message info "${MUTED}Checksum verified.${NC}"
fi
fi
else
print_message warning "Warning: could not fetch SHA256SUMS for v${specific_version} - skipping checksum verification."
fi
print_message info "${MUTED}Extracting...${NC}"
tar -xzf "$tmp_dir/$archive" -C "$tmp_dir"
if [[ ! -f "$tmp_dir/$APP" ]]; then
print_message error "Archive did not contain expected binary '$APP'"
print_message info "Contents:"
ls -la "$tmp_dir"
exit 1
fi
install_binary "$tmp_dir/$APP"
}
install_from_binary() {
if [[ ! -f "$binary_path" ]]; then
print_message error "Binary not found at $binary_path"
exit 1
fi
print_message info "${MUTED}Installing ${NC}${APP} ${MUTED}from ${NC}$binary_path"
install_binary "$binary_path"
}
install_binary() {
local source="$1"
local target_path="${INSTALL_DIR}/${APP}"
cp "$source" "$target_path"
chmod 755 "$target_path"
# On macOS, strip the quarantine attribute so Gatekeeper doesn't block the unsigned binary.
if [[ "$(uname -s)" == "Darwin" ]]; then
xattr -dr com.apple.quarantine "$target_path" 2>/dev/null || true
fi
print_message success "Installed: $target_path"
}
# ----- PATH modification -----
add_to_path_if_needed() {
if [[ "$no_modify_path" == "true" ]]; then
return
fi
if [[ ":$PATH:" == *":$INSTALL_DIR:"* ]]; then
return # already on PATH for this session
fi
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
local current_shell config_files=""
current_shell=$(basename "${SHELL:-/bin/bash}")
case "$current_shell" in
fish)
config_files="$HOME/.config/fish/config.fish"
;;
zsh)
config_files="${ZDOTDIR:-$HOME}/.zshrc ${ZDOTDIR:-$HOME}/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc"
;;
bash)
config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile"
;;
ash|sh)
config_files="$HOME/.profile /etc/profile"
;;
*)
config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile"
;;
esac
local config_file=""
for file in $config_files; do
if [[ -f "$file" ]]; then
config_file="$file"
break
fi
done
# Fall back to creating ~/.profile if nothing exists.
if [[ -z "$config_file" ]]; then
config_file="$HOME/.profile"
touch "$config_file"
fi
local path_line
if [[ "$current_shell" == "fish" ]]; then
path_line="fish_add_path $INSTALL_DIR"
else
path_line="export PATH=\"$INSTALL_DIR:\$PATH\""
fi
if grep -Fq "$INSTALL_DIR" "$config_file" 2>/dev/null; then
print_message info "${MUTED}PATH entry already in ${NC}$config_file"
return
fi
{
echo ""
echo "# claurst"
echo "$path_line"
} >> "$config_file"
print_message success "Added $INSTALL_DIR to PATH in $config_file"
print_message info "${MUTED}Restart your shell or run:${NC} source $config_file"
}
# ----- GitHub Actions environment hint -----
github_path_hint() {
if [ -n "${GITHUB_ACTIONS-}" ] && [ "${GITHUB_ACTIONS}" = "true" ] && [ -n "${GITHUB_PATH-}" ]; then
echo "$INSTALL_DIR" >> "$GITHUB_PATH"
print_message info "Added $INSTALL_DIR to \$GITHUB_PATH"
fi
}
# ----- Main flow -----
main() {
check_required_tools
if [[ -n "$binary_path" ]]; then
# Local binary install - skip detection & version resolution.
specific_version="local"
install_from_binary
else
detect_target
resolve_version
check_existing_install
download_and_install
fi
add_to_path_if_needed
github_path_hint
# Goodbye banner
echo ""
print_message success "claurst is installed!"
echo ""
echo -e "${MUTED}Quickstart:${NC}"
echo -e " ${MUTED}# Set an API key${NC}"
echo -e " export ANTHROPIC_API_KEY=sk-ant-..."
echo -e ""
echo -e " ${MUTED}# Open a new terminal, then:${NC}"
echo -e " ${GREEN}claurst${NC} ${MUTED}# Interactive TUI${NC}"
echo -e " ${GREEN}claurst -p \"...\"${NC} ${MUTED}# Headless one-shot${NC}"
echo ""
echo -e "${MUTED}Docs: ${NC}https://github.com/${REPO}"
}
main "$@"