-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·424 lines (352 loc) · 9.46 KB
/
install.sh
File metadata and controls
executable file
·424 lines (352 loc) · 9.46 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
#!/usr/bin/env bash
# Modern installer for Local Brain CLI (Go version)
# This script provides multiple installation methods following Go best practices
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
REPO="SanderMoon/local-brain"
BINARY_NAME="brain"
INSTALL_DIR="${INSTALL_DIR:-$HOME/.local/bin}"
CONFIG_DIR="$HOME/.config/brain"
# Print functions
print_success() {
echo -e "${GREEN}✓${NC} $1"
}
print_info() {
echo -e "${BLUE}→${NC} $1"
}
print_error() {
echo -e "${RED}✗${NC} $1"
}
print_warning() {
echo -e "${YELLOW}⚠${NC} $1"
}
print_header() {
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "$1"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
}
# Detect OS and architecture
detect_platform() {
OS="$(uname -s)"
ARCH="$(uname -m)"
case "$OS" in
Darwin*)
OS="darwin"
print_info "Detected macOS"
;;
Linux*)
OS="linux"
print_info "Detected Linux"
;;
*)
print_error "Unsupported OS: $OS"
exit 1
;;
esac
case "$ARCH" in
x86_64)
ARCH="amd64"
;;
arm64|aarch64)
ARCH="arm64"
;;
*)
print_error "Unsupported architecture: $ARCH"
exit 1
;;
esac
print_success "Platform: $OS/$ARCH"
}
# Check if a command exists
command_exists() {
command -v "$1" &> /dev/null
}
# Check for Go installation
check_go() {
if command_exists go; then
GO_VERSION=$(go version | awk '{print $3}' | sed 's/go//')
print_success "Go $GO_VERSION installed"
return 0
else
print_warning "Go not installed"
return 1
fi
}
# Check for Homebrew (macOS only)
check_brew() {
if [[ "$OS" == "darwin" ]] && command_exists brew; then
print_success "Homebrew installed"
return 0
else
return 1
fi
}
# Installation method 1: Homebrew (macOS, recommended)
install_via_homebrew() {
print_header "Installing via Homebrew"
if ! check_brew; then
print_error "Homebrew not found"
return 1
fi
print_info "Adding tap SanderMoon/tap..."
brew tap SanderMoon/tap || {
print_warning "Tap not available yet - falling back to other methods"
return 1
}
print_info "Installing brain..."
brew install brain
print_success "Installed via Homebrew"
return 0
}
# Installation method 2: go install (requires Go)
install_via_go_install() {
print_header "Installing via 'go install'"
if ! check_go; then
print_error "Go is required for this installation method"
return 1
fi
print_info "Installing from source..."
go install github.com/$REPO@latest || {
print_error "Failed to install via go install"
return 1
}
# Check where it was installed
GOBIN="${GOBIN:-$(go env GOPATH)/bin}"
if [[ -f "$GOBIN/$BINARY_NAME" ]]; then
print_success "Installed to $GOBIN/$BINARY_NAME"
# Check if GOBIN is in PATH
if ! echo "$PATH" | grep -q "$GOBIN"; then
print_warning "Add $GOBIN to your PATH:"
echo " export PATH=\"$GOBIN:\$PATH\""
fi
return 0
else
print_error "Installation failed"
return 1
fi
}
# Installation method 3: Download pre-built binary
install_via_binary_download() {
print_header "Downloading Pre-built Binary"
print_info "Fetching latest release info..."
# Get latest release info from GitHub API
if command_exists curl; then
RELEASE_INFO=$(curl -s "https://api.github.com/repos/$REPO/releases/latest")
elif command_exists wget; then
RELEASE_INFO=$(wget -qO- "https://api.github.com/repos/$REPO/releases/latest")
else
print_error "Neither curl nor wget found"
return 1
fi
# Extract download URL for our platform
DOWNLOAD_URL=$(echo "$RELEASE_INFO" | grep "browser_download_url.*${BINARY_NAME}_.*${OS}_${ARCH}" | cut -d '"' -f 4 | head -n 1)
if [[ -z "$DOWNLOAD_URL" ]]; then
print_error "No binary found for $OS/$ARCH"
return 1
fi
VERSION=$(echo "$RELEASE_INFO" | grep '"tag_name":' | cut -d '"' -f 4)
print_info "Downloading version $VERSION..."
# Create temporary directory
TMP_DIR=$(mktemp -d)
trap "rm -rf $TMP_DIR" EXIT
# Download archive
if command_exists curl; then
curl -L "$DOWNLOAD_URL" -o "$TMP_DIR/brain.tar.gz"
else
wget -O "$TMP_DIR/brain.tar.gz" "$DOWNLOAD_URL"
fi
# Extract binary
print_info "Extracting..."
tar -xzf "$TMP_DIR/brain.tar.gz" -C "$TMP_DIR"
# Install binary
mkdir -p "$INSTALL_DIR"
cp "$TMP_DIR/$BINARY_NAME" "$INSTALL_DIR/"
chmod +x "$INSTALL_DIR/$BINARY_NAME"
print_success "Installed to $INSTALL_DIR/$BINARY_NAME"
# Check if install dir is in PATH
if ! echo "$PATH" | grep -q "$INSTALL_DIR"; then
print_warning "Add $INSTALL_DIR to your PATH:"
echo " export PATH=\"$INSTALL_DIR:\$PATH\""
fi
return 0
}
# Install dependencies
install_dependencies() {
print_header "Checking Dependencies"
local deps=("ripgrep:rg" "fzf:fzf" "bat:bat" "syncthing:syncthing" "jq:jq")
local missing=()
for dep in "${deps[@]}"; do
IFS=':' read -r name cmd <<< "$dep"
if command_exists "$cmd"; then
print_success "$name installed"
else
print_warning "$name not installed (optional but recommended)"
missing+=("$name")
fi
done
if [[ ${#missing[@]} -gt 0 ]]; then
echo ""
print_info "To install missing dependencies:"
if check_brew; then
echo " brew install ${missing[*]}"
elif [[ "$OS" == "linux" ]]; then
if command_exists apt; then
echo " sudo apt install ${missing[*]}"
elif command_exists dnf; then
echo " sudo dnf install ${missing[*]}"
elif command_exists pacman; then
echo " sudo pacman -S ${missing[*]}"
fi
fi
fi
}
# Initialize first brain
init_brain() {
print_header "Initialize Your First Brain"
if [[ ! -f "$CONFIG_DIR/config.json" ]]; then
print_info "Let's set up your first brain..."
echo ""
if command_exists brain; then
brain init
elif [[ -f "$INSTALL_DIR/$BINARY_NAME" ]]; then
"$INSTALL_DIR/$BINARY_NAME" init
elif [[ -f "$(go env GOPATH)/bin/$BINARY_NAME" ]]; then
"$(go env GOPATH)/bin/$BINARY_NAME" init
else
print_warning "Brain binary not found in PATH"
print_info "Please run 'brain init' after adding it to your PATH"
fi
else
print_success "Brain already configured"
fi
}
# Setup shell integration
setup_shell_integration() {
print_header "Shell Integration"
local rc_file=""
case "$SHELL" in
*/zsh)
rc_file="$HOME/.zshrc"
;;
*/bash)
if [[ "$OS" == "darwin" ]]; then
rc_file="$HOME/.bash_profile"
else
rc_file="$HOME/.bashrc"
fi
;;
*)
print_warning "Unsupported shell: $SHELL"
return
;;
esac
touch "$rc_file"
# Add PATH if needed
local path_export="export PATH=\"$INSTALL_DIR:\$PATH\""
if ! grep -q "$INSTALL_DIR" "$rc_file" 2>/dev/null; then
echo "" >> "$rc_file"
echo "# Local Brain CLI" >> "$rc_file"
echo "$path_export" >> "$rc_file"
print_success "Added $INSTALL_DIR to PATH in $rc_file"
else
print_success "PATH already configured"
fi
print_info "Restart your terminal or run: source $rc_file"
}
# Show installation menu
show_menu() {
print_header "Local Brain CLI Installer"
echo "Choose installation method:"
echo ""
local methods=()
local i=1
if check_brew; then
echo " $i) Homebrew (recommended for macOS)"
methods+=("homebrew")
((i++))
fi
if check_go; then
echo " $i) go install (build from source)"
methods+=("go_install")
((i++))
fi
echo " $i) Download pre-built binary"
methods+=("binary")
((i++))
echo " 0) Exit"
echo ""
read -rp "Select option [0-$((i-1))]: " choice
if [[ "$choice" == "0" ]]; then
print_info "Installation cancelled"
exit 0
fi
if [[ "$choice" -ge 1 ]] && [[ "$choice" -lt "$i" ]]; then
selected_method="${methods[$((choice-1))]}"
echo ""
case "$selected_method" in
homebrew)
install_via_homebrew || {
print_warning "Homebrew installation failed, trying alternative..."
install_via_binary_download
}
;;
go_install)
install_via_go_install || {
print_warning "go install failed, trying alternative..."
install_via_binary_download
}
;;
binary)
install_via_binary_download
;;
esac
else
print_error "Invalid option"
exit 1
fi
}
# Main installation flow
main() {
detect_platform
# If --auto flag is provided, try methods in order
if [[ "${1:-}" == "--auto" ]]; then
print_header "Automatic Installation"
if check_brew && install_via_homebrew; then
:
elif check_go && install_via_go_install; then
:
elif install_via_binary_download; then
:
else
print_error "All installation methods failed"
exit 1
fi
else
show_menu
fi
echo ""
install_dependencies
setup_shell_integration
init_brain
print_header "Installation Complete!"
echo ""
print_success "Local Brain has been installed successfully"
echo ""
print_info "Quick start:"
echo " 1. Restart your terminal (or source your shell config)"
echo " 2. Run: brain --help"
echo " 3. Try: brain add \"My first task\""
echo ""
print_info "For more information:"
echo " https://github.com/$REPO"
echo ""
}
# Run main installation
main "$@"