-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevsetup
More file actions
executable file
·781 lines (664 loc) · 27.9 KB
/
devsetup
File metadata and controls
executable file
·781 lines (664 loc) · 27.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
#!/usr/bin/env bash
# =============================================================================
# devsetup — Interactive DevOps Bootstrapper for Linux
# Version: 1.1.0
# https://github.com/Silver595/DevSetUp
# =============================================================================
set -uo pipefail
DEVSETUP_VERSION="1.1.0"
DEVSETUP_DIR="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)"
LIB_DIR="${DEVSETUP_DIR}/lib"
CONF_DIR="${DEVSETUP_DIR}/config"
# ── Minimum bash version check ───────────────────────────────────────────────
if (( BASH_VERSINFO[0] < 4 )); then
echo "ERROR: devsetup requires bash 4.0+ (you have $BASH_VERSION)" >&2
echo " Install a newer bash: sudo apt install bash" >&2
exit 1
fi
# ── Verify library files exist ────────────────────────────────────────────────
_verify_libs() {
local missing=()
for lib in logger.sh detect.sh install.sh aliases.sh scaffold.sh tui.sh; do
[[ -f "${LIB_DIR}/${lib}" ]] || missing+=("$lib")
done
if [[ ${#missing[@]} -gt 0 ]]; then
echo "FATAL: Missing library files in ${LIB_DIR}: ${missing[*]}" >&2
echo " Reinstall devsetup or check your installation." >&2
exit 1
fi
}
_verify_libs
# ── Source libraries (order matters!) ─────────────────────────────────────────
# shellcheck source=lib/logger.sh
source "${LIB_DIR}/logger.sh"
# shellcheck source=lib/detect.sh
source "${LIB_DIR}/detect.sh"
# shellcheck source=lib/install.sh
source "${LIB_DIR}/install.sh"
# shellcheck source=lib/aliases.sh
source "${LIB_DIR}/aliases.sh"
# shellcheck source=lib/scaffold.sh
source "${LIB_DIR}/scaffold.sh"
# shellcheck source=lib/tui.sh
source "${LIB_DIR}/tui.sh"
# ── Global state ──────────────────────────────────────────────────────────────
DRY_RUN="${DRY_RUN:-false}"
export DRY_RUN
LOCK_FILE="/tmp/devsetup.lock"
LOCK_FD=9
declare -A TOOL_GROUPS=()
_PKG_UPDATED=false # track if pkg index was refreshed this session
# ── Lock management ───────────────────────────────────────────────────────────
_acquire_lock() {
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
log_error "Another devsetup instance is running (lock: $LOCK_FILE)"
log_info "If you're sure no other instance is running: rm -f $LOCK_FILE"
exit 1
fi
}
_release_lock() {
flock -u 9 2>/dev/null || true
rm -f "$LOCK_FILE" 2>/dev/null || true
}
# ── Cleanup traps ─────────────────────────────────────────────────────────────
_on_exit() {
local rc=$?
# Kill any leftover spinner
if [[ -n "${_SPINNER_PID:-}" ]]; then
kill "$_SPINNER_PID" 2>/dev/null || true
wait "$_SPINNER_PID" 2>/dev/null || true
_SPINNER_PID=""
fi
_release_lock
# Clean temp files
rm -f /tmp/devsetup_tui_*.tmp 2>/dev/null || true
return $rc
}
_on_abort() {
echo "" >&2
# Restore terminal state if TUI was running
stty sane 2>/dev/null || true
tput cnorm 2>/dev/null || true
printf '\033[H\033[2J' >&2 2>/dev/null || true
log_warn "Interrupted by user (Ctrl+C)"
# Print partial summary if we have any results
if (( ${#_SUMMARY_OK[@]} + ${#_SUMMARY_FAIL[@]} + ${#_SUMMARY_SKIP[@]} > 0 )); then
log_summary
fi
_release_lock
rm -f /tmp/devsetup_tui_*.tmp 2>/dev/null || true
exit 130
}
trap _on_exit EXIT
trap _on_abort INT TERM
# ── Banner ────────────────────────────────────────────────────────────────────
_print_banner() {
cat >&2 <<'BANNER'
██████╗ ███████╗██╗ ██╗███████╗███████╗████████╗██╗ ██╗██████╗
██╔══██╗██╔════╝██║ ██║██╔════╝██╔════╝╚══██╔══╝██║ ██║██╔══██╗
██║ ██║█████╗ ██║ ██║███████╗█████╗ ██║ ██║ ██║██████╔╝
██║ ██║██╔══╝ ╚██╗ ██╔╝╚════██║██╔══╝ ██║ ██║ ██║██╔═══╝
██████╔╝███████╗ ╚████╔╝ ███████║███████╗ ██║ ╚██████╔╝██║
╚═════╝ ╚══════╝ ╚═══╝ ╚══════╝╚══════╝ ╚═╝ ╚═════╝ ╚═╝
BANNER
printf " ${TEAL}${BOLD}devsetup${RESET} ${DIM}v${DEVSETUP_VERSION}${RESET} ${DIM}—${RESET} ${DIM}Interactive DevOps Bootstrapper${RESET}\n\n" >&2
}
# ── Help ──────────────────────────────────────────────────────────────────────
_print_help() {
_print_banner
printf " ${BWHITE}${BOLD}USAGE${RESET}\n"
printf " devsetup ${DIM}[COMMAND] [OPTIONS]${RESET}\n\n"
printf " ${BWHITE}${BOLD}COMMANDS${RESET}\n"
printf " ${GREEN}%-30s${RESET} %s\n" "(no args)" "Launch interactive TUI tool selector"
printf " ${GREEN}%-30s${RESET} %s\n" "--install TOOL [...]" "Install one or more tools directly"
printf " ${GREEN}%-30s${RESET} %s\n" "--update TOOL [...]" "Re-install/update tools to latest"
printf " ${GREEN}%-30s${RESET} %s\n" "--uninstall TOOL [...]" "Remove tools via package manager"
printf " ${GREEN}%-30s${RESET} %s\n" "--status" "Show version table of every installed tool"
printf " ${GREEN}%-30s${RESET} %s\n" "--doctor" "Pre-flight system readiness check"
printf " ${GREEN}%-30s${RESET} %s\n" "--list" "List all tools with installed/not-installed"
printf " ${GREEN}%-30s${RESET} %s\n" "--git-config" "Interactive Git global config wizard"
printf " ${GREEN}%-30s${RESET} %s\n" "--aliases" "Inject DevOps shell aliases"
printf " ${GREEN}%-30s${RESET} %s\n" "--remove-aliases" "Remove injected aliases"
printf " ${GREEN}%-30s${RESET} %s\n" "--preview-aliases" "Preview aliases without injecting"
printf " ${GREEN}%-30s${RESET} %s\n" "--scaffold [NAME]" "Create a DevOps project folder structure"
printf " ${GREEN}%-30s${RESET} %s\n" "--preview-scaffold" "Show scaffold tree (no creation)"
printf " ${GREEN}%-30s${RESET} %s\n" "--export FILE" "Export installed tool list to file"
printf " ${GREEN}%-30s${RESET} %s\n" "--import FILE" "Import and install tools from file"
printf " ${GREEN}%-30s${RESET} %s\n" "--log [N]" "Show last N log files (default: 5)"
printf " ${GREEN}%-30s${RESET} %s\n" "--self-update" "Update devsetup itself to latest"
printf " ${GREEN}%-30s${RESET} %s\n" "--dry-run [CMD...]" "Preview any action without changes"
printf " ${GREEN}%-30s${RESET} %s\n" "--version" "Print version string"
printf " ${GREEN}%-30s${RESET} %s\n" "--help" "Show this help"
echo "" >&2
printf " ${BWHITE}${BOLD}EXAMPLES${RESET}\n"
printf " devsetup --install docker nginx postgresql\n"
printf " devsetup --dry-run --install terraform\n"
printf " devsetup --uninstall nginx\n"
printf " devsetup --doctor\n"
printf " devsetup --export ~/my-tools.txt\n"
printf " DEVSETUP_TUI=whiptail devsetup\n"
echo "" >&2
printf " ${BWHITE}${BOLD}ENVIRONMENT${RESET}\n"
printf " ${CYAN}DEVSETUP_TUI${RESET}=${DIM}whiptail|dialog|fzf|bash${RESET} Force TUI backend\n"
printf " ${CYAN}DRY_RUN${RESET}=${DIM}true${RESET} Enable dry-run mode\n"
echo "" >&2
}
# ── Load tools.conf ───────────────────────────────────────────────────────────
_load_tools_conf() {
local conf="${CONF_DIR}/tools.conf"
if [[ ! -f "$conf" ]]; then
log_error "Tools config not found: $conf"
return 1
fi
TOOL_GROUPS=()
while IFS= read -r line || [[ -n "$line" ]]; do
# Skip blank lines and comments
[[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue
# Format: CATEGORY:TOOL_NAME
local cat="${line%%:*}"
local tool="${line#*:}"
cat="${cat// /}" # strip whitespace
tool="${tool// /}" # strip whitespace
[[ -z "$cat" || -z "$tool" ]] && continue
# Validate that install function exists
if ! declare -f "install_${tool,,}" &>/dev/null; then
log_warn "No install_${tool} function found — skipping '$tool' from tools.conf"
continue
fi
if [[ -n "${TOOL_GROUPS[$cat]:-}" ]]; then
TOOL_GROUPS[$cat]+=" $tool"
else
TOOL_GROUPS[$cat]="$tool"
fi
done < "$conf"
if [[ ${#TOOL_GROUPS[@]} -eq 0 ]]; then
log_error "No valid tools loaded from $conf"
return 1
fi
}
# ── Ensure pkg index is refreshed (once per session) ─────────────────────────
_ensure_pkg_updated() {
if [[ "$_PKG_UPDATED" != "true" ]]; then
_pkg_update
_PKG_UPDATED=true
fi
}
# ── Git config wizard ────────────────────────────────────────────────────────
run_git_config_wizard() {
log_section "Git Global Configuration Wizard"
if ! command -v git &>/dev/null; then
log_error "Git is not installed. Run: devsetup --install git"
return 1
fi
local current_name current_email
current_name="$(git config --global user.name 2>/dev/null || echo '')"
current_email="$(git config --global user.email 2>/dev/null || echo '')"
local name email editor
name="$(tui_read "Full name" "$current_name")"
email="$(tui_read "Email" "$current_email")"
editor="$(tui_read "Preferred editor" "$(git config --global core.editor 2>/dev/null || echo 'vim')")"
if [[ -z "$name" || -z "$email" ]]; then
log_error "Name and email are required."
return 1
fi
if [[ "${DRY_RUN}" == "true" ]]; then
log_info "[dry-run] Would set git config: name=$name, email=$email, editor=$editor"
return 0
fi
git config --global user.name "$name"
git config --global user.email "$email"
git config --global core.editor "$editor"
git config --global init.defaultBranch main
git config --global pull.rebase true
git config --global push.autoSetupRemote true
git config --global color.ui auto
log_ok "Git configured for: $name <$email>"
log_info "Editor: $editor | Default branch: main | Pull: rebase"
}
# ── Install tool list with progress ──────────────────────────────────────────
install_tool_list() {
local -a tools=("$@")
local total=${#tools[@]}
local i=0 started failed=0
if [[ $total -eq 0 ]]; then
log_warn "No tools to install."
return 0
fi
_ensure_pkg_updated
started="$(date +%s)"
for tool in "${tools[@]}"; do
(( i++ ))
log_progress "$i" "$total" "$tool"
local t_start; t_start="$(date +%s)"
if ! install_tool "$tool"; then
(( failed++ ))
fi
local t_end; t_end="$(date +%s)"
local elapsed=$(( t_end - t_start ))
if (( elapsed > 2 )); then
log_info " ⏱ ${tool} took ${elapsed}s"
fi
done
local total_time=$(( $(date +%s) - started ))
log_info "Total install time: ${total_time}s"
return "$failed"
}
# ── Print tool list with install status ───────────────────────────────────────
_print_list() {
_load_tools_conf || return 1
local -a sorted_cats=()
mapfile -t sorted_cats < <(printf '%s\n' "${!TOOL_GROUPS[@]}" | sort)
local installed=0 total=0
echo "" >&2
printf " ${BWHITE}${BOLD}Available Tools${RESET}\n\n" >&2
for cat in "${sorted_cats[@]}"; do
printf " ${INDIGO}${BOLD}▸ %s${RESET}\n" "$cat" >&2
for tool in ${TOOL_GROUPS[$cat]}; do
(( total++ ))
local cmd="$tool"
# Map tool names to their actual commands
case "$tool" in
awscli) cmd="aws" ;; azure) cmd="az" ;; ripgrep) cmd="rg" ;;
neovim) cmd="nvim" ;; postgresql) cmd="psql" ;; nvm) cmd="" ;;
phpfpm) cmd="php-fpm" ;; golang) cmd="go" ;; mysql) cmd="mysql" ;;
esac
local status_icon status_color
if [[ "$tool" == "nvm" ]]; then
if [[ -s "${NVM_DIR:-$HOME/.nvm}/nvm.sh" ]]; then
status_icon="${ICON_OK}"; status_color="${GREEN}"; (( installed++ ))
else
status_icon="◦"; status_color="${DIM}"
fi
elif [[ -n "$cmd" ]] && command -v "$cmd" &>/dev/null; then
status_icon="${ICON_OK}"; status_color="${GREEN}"; (( installed++ ))
else
status_icon="◦"; status_color="${DIM}"
fi
printf " ${status_color}%s${RESET} %-16s\n" "$status_icon" "$tool" >&2
done
done
echo "" >&2
printf " ${DIM}%d/%d tools installed${RESET}\n\n" "$installed" "$total" >&2
}
# ── Export / Import tool selections ───────────────────────────────────────────
_export_tools() {
local outfile="${1:-devsetup-tools.txt}"
_load_tools_conf || return 1
local -a installed_tools=()
local -a sorted_cats=()
mapfile -t sorted_cats < <(printf '%s\n' "${!TOOL_GROUPS[@]}" | sort)
for cat in "${sorted_cats[@]}"; do
for tool in ${TOOL_GROUPS[$cat]}; do
local cmd="$tool"
case "$tool" in
awscli) cmd="aws" ;; azure) cmd="az" ;; ripgrep) cmd="rg" ;;
neovim) cmd="nvim" ;; postgresql) cmd="psql" ;; golang) cmd="go" ;;
esac
if [[ "$tool" == "nvm" ]]; then
[[ -s "${NVM_DIR:-$HOME/.nvm}/nvm.sh" ]] && installed_tools+=("$tool")
elif command -v "$cmd" &>/dev/null; then
installed_tools+=("$tool")
fi
done
done
if [[ ${#installed_tools[@]} -eq 0 ]]; then
log_warn "No tools are currently installed."
return 1
fi
printf "# devsetup tool export — %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" > "$outfile"
printf "# Install with: devsetup --import %s\n" "$outfile" >> "$outfile"
printf '%s\n' "${installed_tools[@]}" >> "$outfile"
log_ok "Exported ${#installed_tools[@]} tools to: $outfile"
}
_import_tools() {
local infile="$1"
if [[ ! -f "$infile" ]]; then
log_error "File not found: $infile"
return 1
fi
local -a tools=()
while IFS= read -r line || [[ -n "$line" ]]; do
[[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue
tools+=("${line// /}")
done < "$infile"
if [[ ${#tools[@]} -eq 0 ]]; then
log_warn "No tools found in $infile"
return 1
fi
log_info "Importing ${#tools[@]} tools from $infile: ${tools[*]}"
if tui_confirm "Install ${#tools[@]} tools?" "y"; then
install_tool_list "${tools[@]}"
else
log_info "Import cancelled."
fi
}
# ── Self-update ───────────────────────────────────────────────────────────────
_self_update() {
log_section "Self-Update"
local repo_url="https://raw.githubusercontent.com/Silver595/DevSetUp/main"
if [[ "${DRY_RUN}" == "true" ]]; then
log_info "[dry-run] Would download latest version from $repo_url"
return 0
fi
# Check internet
if ! curl -fsSL --max-time 4 https://1.1.1.1 &>/dev/null \
&& ! curl -fsSL --max-time 4 https://google.com &>/dev/null; then
log_error "No internet connection — cannot self-update."
return 1
fi
local tmp; tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' RETURN
spinner_start "Downloading latest version"
# Try git clone first, fall back to curl
if command -v git &>/dev/null; then
git clone --depth=1 "https://github.com/Silver595/DevSetUp.git" "$tmp/repo" 2>/dev/null || {
spinner_stop fail
log_error "Failed to clone repository."
return 1
}
else
mkdir -p "$tmp/repo/lib" "$tmp/repo/config"
local files=("devsetup" "lib/logger.sh" "lib/detect.sh" "lib/install.sh"
"lib/aliases.sh" "lib/scaffold.sh" "lib/tui.sh"
"config/tools.conf" "config/aliases.conf" "config/folders.conf")
for f in "${files[@]}"; do
curl -fsSL "$repo_url/$f" -o "$tmp/repo/$f" 2>/dev/null || {
spinner_stop fail
log_error "Failed to download $f"; return 1
}
done
fi
spinner_stop ok
# Detect where we're installed
local install_dir="$DEVSETUP_DIR"
local bin_path
bin_path="$(readlink -f "${BASH_SOURCE[0]}")"
# Backup current version
cp "$bin_path" "${bin_path}.bak" 2>/dev/null || true
# Copy new files
spinner_start "Installing update"
if [[ -w "$install_dir" ]]; then
cp "$tmp/repo/devsetup" "$bin_path"
cp "$tmp/repo/lib/"*.sh "$install_dir/lib/" 2>/dev/null || true
cp "$tmp/repo/config/"* "$install_dir/config/" 2>/dev/null || true
else
$SUDO cp "$tmp/repo/devsetup" "$bin_path"
$SUDO cp "$tmp/repo/lib/"*.sh "$install_dir/lib/" 2>/dev/null || true
$SUDO cp "$tmp/repo/config/"* "$install_dir/config/" 2>/dev/null || true
fi
chmod +x "$bin_path" 2>/dev/null || $SUDO chmod +x "$bin_path"
spinner_stop ok
# Show new version
local new_ver
new_ver="$("$bin_path" --version 2>/dev/null || echo 'unknown')"
log_ok "Updated to: $new_ver"
log_info "Previous version backed up to: ${bin_path}.bak"
}
# ── Log viewer ────────────────────────────────────────────────────────────────
_show_logs() {
local count="${1:-5}"
local -a logs=()
mapfile -t logs < <(ls -1t /tmp/devsetup_*.log 2>/dev/null | head -n "$count")
if [[ ${#logs[@]} -eq 0 ]]; then
log_info "No devsetup log files found in /tmp/"
return 0
fi
echo "" >&2
printf " ${BWHITE}${BOLD}Recent Log Files${RESET} ${DIM}(showing %d)${RESET}\n\n" "${#logs[@]}" >&2
for f in "${logs[@]}"; do
local sz; sz="$(du -h "$f" 2>/dev/null | awk '{print $1}')"
local ts; ts="$(stat -c '%y' "$f" 2>/dev/null | cut -d. -f1)"
printf " ${CYAN}%s${RESET} ${DIM}%s %s${RESET}\n" "$(basename "$f")" "$sz" "$ts" >&2
done
echo "" >&2
if [[ ${#logs[@]} -gt 0 ]]; then
printf " ${DIM}Latest: %s${RESET}\n\n" "${logs[0]}" >&2
if tui_confirm "View the latest log?" "n"; then
less "${logs[0]}"
fi
fi
}
# ── Interactive mode ──────────────────────────────────────────────────────────
run_interactive() {
_print_banner
# Pre-flight checks
log_section "Pre-flight Checks"
detect_os
detect_print_summary
local doctor_issues=0
check_internet || (( doctor_issues++ ))
check_sudo
check_disk_space / || (( doctor_issues++ ))
check_pkg_manager || (( doctor_issues++ ))
check_required_tools || (( doctor_issues++ ))
check_pkg_lock
if (( doctor_issues > 0 )); then
echo "" >&2
if ! tui_confirm "Pre-flight found ${doctor_issues} issue(s). Continue anyway?" "n"; then
log_info "Exiting. Fix issues and re-run."
return 1
fi
fi
# Load tool config
_load_tools_conf || return 1
# TUI tool selection
_tui_out_file="$(mktemp /tmp/devsetup_tui_XXXXXX.tmp)"
export _tui_out_file
if ! tui_select_tools TOOL_GROUPS; then
log_info "No tools selected. Exiting."
rm -f "$_tui_out_file" 2>/dev/null
return 0
fi
local selected_raw
selected_raw="$(cat "$_tui_out_file" 2>/dev/null)"
rm -f "$_tui_out_file" 2>/dev/null
if [[ -z "${selected_raw// /}" ]]; then
log_info "No tools selected. Exiting."
return 0
fi
# Parse selected tools
local -a selected_tools=()
read -ra selected_tools <<< "$selected_raw"
log_section "Installing ${#selected_tools[@]} Tools"
log_info "Selected: ${selected_tools[*]}"
echo "" >&2
if ! tui_confirm "Proceed with installation?" "y"; then
log_info "Installation cancelled."
return 0
fi
# Install
install_tool_list "${selected_tools[@]}"
# Print summary
log_summary
# Offer aliases
echo "" >&2
if tui_confirm "Inject DevOps shell aliases into your shell config?" "y"; then
inject_aliases "${CONF_DIR}/aliases.conf"
fi
# Offer scaffold
if tui_confirm "Create a DevOps project folder structure?" "n"; then
scaffold_interactive "${CONF_DIR}/folders.conf"
fi
# Offer git config
if ! git config --global user.name &>/dev/null; then
if tui_confirm "Set up Git global config?" "y"; then
run_git_config_wizard
fi
fi
echo "" >&2
log_ok "devsetup complete! Happy coding!"
log_info "Log file: $LOG_FILE"
echo "" >&2
}
# ── Main entry point ──────────────────────────────────────────────────────────
main() {
# Handle --dry-run prefix: strip it and set flag
if [[ "${1:-}" == "--dry-run" ]]; then
DRY_RUN="true"
export DRY_RUN
shift
if [[ $# -eq 0 ]]; then
log_info "Dry-run mode enabled. Specify a command to preview."
_print_help
return 0
fi
fi
# Acquire lock for install operations
local needs_lock=true
case "${1:-}" in
--help|-h|--version|-v|--list|--status|--doctor|--preview-aliases|--preview-scaffold|--log)
needs_lock=false ;;
esac
[[ "$needs_lock" == "true" ]] && _acquire_lock
# Detect OS early for all commands
detect_os
case "${1:-}" in
--help|-h)
_print_help
;;
--version|-v)
echo "devsetup ${DEVSETUP_VERSION}"
;;
--doctor)
_print_banner
run_doctor
;;
--list)
_print_banner
_print_list
;;
--status)
_print_banner
show_status
;;
--install)
shift
if [[ $# -eq 0 ]]; then
log_error "Usage: devsetup --install TOOL [TOOL...]"
log_info "Run 'devsetup --list' to see available tools."
return 1
fi
_print_banner
log_section "Installing ${#} Tool(s)"
install_tool_list "$@"
log_summary
;;
--update)
shift
if [[ $# -eq 0 ]]; then
log_error "Usage: devsetup --update TOOL [TOOL...]"
return 1
fi
_print_banner
log_section "Updating ${#} Tool(s)"
# Force reinstall by unsetting the already-installed check
for tool in "$@"; do
log_progress "$((++_u_idx))" "$#" "$tool"
local fn="install_${tool,,}"
if declare -f "$fn" > /dev/null 2>&1; then
# Call install body directly, skipping _already_installed check
local body_fn="_install_${tool,,}_body"
if declare -f "$body_fn" > /dev/null 2>&1; then
log_step "Updating $tool..."
spinner_start "Updating $tool"
_do_install "$tool" "$body_fn" && { spinner_stop ok; summary_ok "$tool (updated)"; } \
|| summary_fail "$tool"
else
# Simple package reinstall
log_step "Reinstalling $tool..."
spinner_start "Reinstalling $tool"
_do_install "$tool" _pkg_install "$tool" && { spinner_stop ok; summary_ok "$tool (updated)"; } \
|| summary_fail "$tool"
fi
else
log_error "No installer for: $tool"
summary_fail "$tool (unknown)"
fi
done
log_summary
;;
--uninstall)
shift
if [[ $# -eq 0 ]]; then
log_error "Usage: devsetup --uninstall TOOL [TOOL...]"
return 1
fi
_print_banner
log_section "Uninstalling ${#} Tool(s)"
for tool in "$@"; do
log_step "Removing $tool..."
spinner_start "Removing $tool"
if uninstall_tool "$tool"; then
spinner_stop ok
summary_ok "$tool (removed)"
else
spinner_stop fail
summary_fail "$tool"
fi
done
log_summary
;;
--git-config)
_print_banner
run_git_config_wizard
;;
--aliases)
_print_banner
inject_aliases "${CONF_DIR}/aliases.conf"
;;
--remove-aliases)
remove_aliases
;;
--preview-aliases)
preview_aliases "${CONF_DIR}/aliases.conf"
;;
--scaffold)
_print_banner
shift
if [[ -n "${1:-}" ]]; then
scaffold_project "${CONF_DIR}/folders.conf" "$HOME/projects/$1"
else
scaffold_interactive "${CONF_DIR}/folders.conf"
fi
;;
--preview-scaffold)
preview_scaffold "${CONF_DIR}/folders.conf"
;;
--export)
shift
_export_tools "${1:-devsetup-tools.txt}"
;;
--import)
shift
if [[ -z "${1:-}" ]]; then
log_error "Usage: devsetup --import FILE"
return 1
fi
_print_banner
_import_tools "$1"
log_summary
;;
--log)
shift
_show_logs "${1:-5}"
;;
--self-update)
_print_banner
_self_update
;;
"")
# No args → interactive mode
run_interactive
;;
*)
log_error "Unknown command: $1"
echo "" >&2
_print_help
return 1
;;
esac
}
# Initialize update counter for --update
_u_idx=0
main "$@"