-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdot
More file actions
executable file
·563 lines (471 loc) · 15.9 KB
/
dot
File metadata and controls
executable file
·563 lines (471 loc) · 15.9 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#!/bin/bash
set -euo pipefail
# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DOT_HOME="$(dirname "$SCRIPT_DIR")"
# Source utilities
source "$DOT_HOME/lib/colors.sh"
source "$DOT_HOME/lib/constants.sh"
source "$DOT_HOME/lib/utils.sh"
source "$DOT_HOME/lib/logging.sh"
# Show usage
usage() {
local installed_system="unknown"
if is_system_installed; then
installed_system=$(head -n1 "$INSTALLATION_MARKER")
fi
cat << EOF
dot - Dotfiles management tool for $installed_system
USAGE:
dot [COMMAND] [ARGS...]
INSTALLATION=SYSTEM dot [COMMAND] [ARGS...]
COMMANDS:
install [MODULE] Install packages and applications
Modules: all (default)
build [MODULE] Build and deploy configuration files
Modules: all (default)
upgrade [MODULE] Upgrade source-built packages
Modules: all (default)
pull Update dotfiles repository from git
sync Pull latest changes and rebuild all
pwd Print dotfiles directory path
doctor Check system health and requirements
help Show this help message
EXAMPLES:
dot # Show this help message
dot install # Install all packages
dot install <package> # Install specific package
dot install --list # List available packages for install
dot build <path/package> # Build specific package
dot build --list # List available packages for build
dot upgrade # Upgrade all source-built packages
dot upgrade package # Upgrade specific source-built packages
dot upgrade --list # List available packages for upgrade
dot pull # Update dotfiles repository
dot sync # Pull and rebuild everything
dot pwd # Print dotfiles directory path
# Override system without installation marker:
INSTALLATION=debian dot install # Install debian packages
INSTALLATION=debian dot build # Build debian configs
EOF
}
# Check if system is set up
check_system_setup() {
# Allow override via environment variable
if [[ -n "${INSTALLATION:-}" ]]; then
return 0
fi
if ! is_system_installed; then
log_error "System not initialized. Please run one of the install scripts at bin/install-* first"
exit 1
fi
}
# Get system directory path
get_system_dir() {
# Use environment variable override if provided
if [[ -n "${INSTALLATION:-}" ]]; then
echo "$DOT_HOME/$INSTALLATION"
return 0
fi
local installed_system=$(head -n1 "$INSTALLATION_MARKER")
echo "$DOT_HOME/$installed_system"
}
# Resolve script name (handles numeric prefixes like 01-50-mise)
resolve_script_name() {
local module="$1"
local command="$2"
local system_dir="$(get_system_dir)"
local command_dir="$system_dir/$command"
# First, try the module name as-is
if [[ -f "$command_dir/$module" && -x "$command_dir/$module" ]]; then
echo "$module"
return 0
fi
# Try to find a script that ends with the module name (e.g., "mise" -> "01-50-mise")
local found_script=""
for script in "$command_dir"/*; do
[[ ! -f "$script" || ! -x "$script" ]] && continue
local script_name=$(basename "$script")
# Check if script ends with -$module
if [[ "$script_name" == *-"$module" ]]; then
found_script="$script_name"
break
fi
done
if [[ -n "$found_script" ]]; then
echo "$found_script"
return 0
fi
# Return original if no match found
echo "$module"
return 1
}
# Check system setup for --list operations
check_system_for_list() {
# Allow override via environment variable
if [[ -n "${INSTALLATION:-}" ]]; then
return 0
fi
if ! is_system_installed; then
log_error "System not initialized. Please run one of the install scripts at bin/install-* first"
exit 1
fi
}
# Get available modules for a given command
get_available_modules() {
local command="$1"
local modules=()
# Get the installed system type
local installed_system
if [[ -n "${INSTALLATION:-}" ]]; then
installed_system="$INSTALLATION"
else
installed_system=$(head -n1 "$INSTALLATION_MARKER")
fi
local system_dir="$DOT_HOME/$installed_system"
local command_dir="$system_dir/$command"
# Check if command directory exists
if [[ ! -d "$command_dir" ]]; then
return
fi
# Find all executable files in the command directory
# Use -perm for BSD find compatibility (macOS)
while IFS= read -r -d '' script; do
local script_name=$(basename "$script")
modules+=("$script_name")
done < <(find "$command_dir" -maxdepth 1 -type f -perm +111 -print0 2>/dev/null)
# Handle empty array to avoid unbound variable error
if [[ ${#modules[@]} -gt 0 ]]; then
printf '%s\n' "${modules[@]}" | sort
fi
}
# Run install command
cmd_install() {
local module="${1:-all}"
# Handle --list option
if [[ "$module" == "--list" ]]; then
check_system_for_list
echo "Available modules for install:"
get_available_modules "install"
return 0
fi
check_system_setup
init_logging "install"
log_header "Installing: $module"
# Export common variables for scripts
export DOT_HOME="$DOT_HOME"
export SYSTEM_DIR="$(get_system_dir)"
export SHARED_DIR="$DOT_HOME/shared"
if [[ "$module" == "all" ]]; then
log_info "Installing all modules"
# Get all available install scripts
local available_modules=()
while IFS= read -r line; do
[[ -n "$line" ]] && available_modules+=("$line")
done < <(get_available_modules "install")
for script_name in "${available_modules[@]}"; do
# Extract module name from script filename (e.g., "01-50-mise" -> "mise")
export MODULE_NAME="$(basename "$script_name" | sed 's/^[0-9]*-[0-9]*-//')"
init_module_logging "$MODULE_NAME"
if "$SYSTEM_DIR/install/$script_name"; then
finalize_module_logging "success"
else
finalize_module_logging "failed"
exit 1
fi
done
else
# Install specific module
local script_name=$(resolve_script_name "$module" "install")
if [[ -x "$SYSTEM_DIR/install/$script_name" ]]; then
# Extract module name from script filename
export MODULE_NAME="$(basename "$script_name" | sed 's/^[0-9]*-[0-9]*-//')"
init_module_logging "$MODULE_NAME"
if "$SYSTEM_DIR/install/$script_name"; then
finalize_module_logging "success"
else
finalize_module_logging "failed"
exit 1
fi
else
log_error "Module $module does not have an install script"
exit 1
fi
fi
finalize_logging "success"
}
# Run build command
cmd_build() {
local module="${1:-all}"
# Handle --list option
if [[ "$module" == "--list" ]]; then
check_system_for_list
echo "Available modules for build:"
get_available_modules "build"
return 0
fi
check_system_setup
init_logging "build"
log_header "Building: $module"
# Export common variables for scripts
export DOT_HOME="$DOT_HOME"
export SYSTEM_DIR="$(get_system_dir)"
export SHARED_DIR="$DOT_HOME/shared"
if [[ "$module" == "all" ]]; then
log_info "Building all modules"
# Get all available build scripts
local available_modules=()
while IFS= read -r line; do
[[ -n "$line" ]] && available_modules+=("$line")
done < <(get_available_modules "build")
for script_name in "${available_modules[@]}"; do
# Extract module name from script filename (e.g., "03-00-git" -> "git")
export MODULE_NAME="$(basename "$script_name" | sed 's/^[0-9]*-[0-9]*-//')"
init_module_logging "$MODULE_NAME"
if "$SYSTEM_DIR/build/$script_name"; then
finalize_module_logging "success"
else
finalize_module_logging "failed"
exit 1
fi
done
else
# Build specific module
local script_name=$(resolve_script_name "$module" "build")
if [[ -x "$SYSTEM_DIR/build/$script_name" ]]; then
# Extract module name from script filename
export MODULE_NAME="$(basename "$script_name" | sed 's/^[0-9]*-[0-9]*-//')"
init_module_logging "$MODULE_NAME"
if "$SYSTEM_DIR/build/$script_name"; then
finalize_module_logging "success"
else
finalize_module_logging "failed"
exit 1
fi
else
log_error "Module $module does not have a build script"
exit 1
fi
fi
finalize_logging "success"
}
# Run upgrade command
cmd_upgrade() {
local module="${1:-all}"
# Handle --list option
if [[ "$module" == "--list" ]]; then
check_system_for_list
echo "Available modules for upgrade:"
get_available_modules "upgrade"
return 0
fi
check_system_setup
init_logging "upgrade"
log_header "Upgrading: $module"
# Export common variables for scripts
export DOT_HOME="$DOT_HOME"
export SYSTEM_DIR="$(get_system_dir)"
export SHARED_DIR="$DOT_HOME/shared"
if [[ "$module" == "all" ]]; then
log_info "Upgrading all modules"
# Get all available upgrade scripts
local available_modules=()
while IFS= read -r line; do
[[ -n "$line" ]] && available_modules+=("$line")
done < <(get_available_modules "upgrade")
if [[ ${#available_modules[@]} -eq 0 ]]; then
log_info "No modules have upgrade scripts"
finalize_logging "success"
return
fi
for script_name in "${available_modules[@]}"; do
# Extract module name from script filename
export MODULE_NAME="$(basename "$script_name" | sed 's/^[0-9]*-[0-9]*-//')"
init_module_logging "$MODULE_NAME"
if "$SYSTEM_DIR/upgrade/$script_name"; then
finalize_module_logging "success"
else
finalize_module_logging "failed"
exit 1
fi
done
else
# Upgrade specific module
local script_name=$(resolve_script_name "$module" "upgrade")
if [[ -x "$SYSTEM_DIR/upgrade/$script_name" ]]; then
# Extract module name from script filename
export MODULE_NAME="$(basename "$script_name" | sed 's/^[0-9]*-[0-9]*-//')"
init_module_logging "$MODULE_NAME"
if "$SYSTEM_DIR/upgrade/$script_name"; then
finalize_module_logging "success"
else
finalize_module_logging "failed"
exit 1
fi
else
log_error "Module $module does not have an upgrade script"
exit 1
fi
fi
finalize_logging "success"
}
# Sync command - pull and build
cmd_sync() {
# First pull
cmd_pull
# Then build all
cmd_build "all"
}
# Pull command - update dotfiles repository
cmd_pull() {
init_logging "pull"
log_header "Updating dotfiles repository"
# Save current directory
local current_dir="$(pwd)"
# Change to dotfiles directory
cd "$DOT_HOME"
# Get current branch
local current_branch=$(git rev-parse --abbrev-ref HEAD)
log_info "Current branch: $current_branch"
# Check for uncommitted changes
if ! git diff-index --quiet HEAD --; then
log_warning "You have uncommitted changes:"
git status --short
log_error "Please commit or stash your changes before pulling"
cd "$current_dir"
finalize_logging "failed"
exit 1
fi
# Pull latest changes
log_step "Pulling latest changes from $current_branch"
if git pull origin "$current_branch"; then
log_success "Repository updated successfully"
# Show what changed
local changes=$(git log --oneline HEAD@{1}..HEAD 2>/dev/null)
if [[ -n "$changes" ]]; then
log_info "Recent changes:"
echo "$changes"
else
log_info "Already up to date"
fi
else
log_error "Failed to pull changes"
cd "$current_dir"
finalize_logging "failed"
exit 1
fi
# Return to original directory
cd "$current_dir"
finalize_logging "success"
}
# Doctor command - check system health
cmd_doctor() {
init_logging "doctor"
log_header "System Doctor"
local issues=0
# Check basic system
if ! is_system_installed; then
log_error "System not initialized. Run an install script first (bin/install-*)"
((issues++))
else
local installed_system=$(get_installed_system)
log_success "System is initialized: $installed_system"
# Check if the system directory exists
local system_dir="$DOT_HOME/$installed_system"
if [[ ! -d "$system_dir" ]]; then
log_error "System directory not found: $system_dir"
((issues++))
else
log_success "System directory exists: $system_dir"
fi
fi
# Check required directories
if [[ ! -d "$LOG_DIR" ]]; then
log_warning "Log directory missing: $LOG_DIR"
((issues++))
else
log_success "Log directory exists"
fi
# Check flatpak setup (if available)
if command_exists flatpak; then
if ! flatpak remote-list --system 2>/dev/null | grep -q flathub; then
log_warning "Flathub repository not configured (install apps may fail)"
((issues++))
else
log_success "Flathub repository configured"
fi
else
log_info "Flatpak not available on this system"
fi
# Run OS-specific checks if available
if is_system_installed; then
local installed_system=$(get_installed_system)
local system_doctor="$DOT_HOME/$installed_system/doctor"
if [[ -x "$system_doctor" ]]; then
log_step "Running $installed_system-specific checks"
if "$system_doctor" >/dev/null 2>&1; then
log_success "OS-specific checks passed"
else
local os_issues=$?
log_warning "OS-specific checks found $os_issues issue(s)"
((issues += os_issues))
fi
fi
fi
# Summary
echo
if [[ $issues -eq 0 ]]; then
log_success "No issues found!"
else
log_warning "Found $issues issue(s)"
fi
finalize_logging "success"
}
# Main command dispatcher
main() {
# Don't run as root
if is_root; then
log_error "Don't run dot as root/sudo. It will ask for sudo when needed."
exit 1
fi
if [[ $# -eq 0 ]]; then
usage
return
fi
local command="$1"
shift
case "$command" in
install)
cmd_install "$@"
;;
build)
cmd_build "$@"
;;
upgrade)
cmd_upgrade "$@"
;;
pull)
cmd_pull
;;
sync)
cmd_sync
;;
pwd)
echo "$DOT_HOME"
;;
doctor)
cmd_doctor
;;
help|--help|-h)
usage
;;
*)
log_error "Unknown command: $command"
echo
usage
exit 1
;;
esac
}
# Run main function
main "$@"