Skip to content

Commit 7986d2d

Browse files
authored
Merge pull request #18 from ooloth/feature/core-utils-bash
Add Core Utility Libraries Migration with Comprehensive Bash Testing
2 parents 62c5479 + 571fadb commit 7986d2d

File tree

4 files changed

+749
-0
lines changed

4 files changed

+749
-0
lines changed

bin/lib/machine-detection.bash

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env bash
2+
3+
# Machine detection utilities for dotfiles setup
4+
# Dynamically detects machine type based on hostname patterns
5+
6+
set -euo pipefail
7+
8+
# Detect machine type based on hostname
9+
detect_machine_type() {
10+
# Check for environment variable override first
11+
if [[ -n "${MACHINE:-}" ]]; then
12+
echo "$MACHINE"
13+
return 0
14+
fi
15+
16+
# Get hostname for detection
17+
local hostname
18+
if command -v hostname >/dev/null 2>&1; then
19+
hostname=$(hostname)
20+
else
21+
# Fallback if hostname command is not available
22+
hostname="${HOST:-unknown}"
23+
fi
24+
25+
# Pattern matching for machine types
26+
case "$hostname" in
27+
*Air*)
28+
echo "air"
29+
;;
30+
*Mini*)
31+
echo "mini"
32+
;;
33+
*)
34+
# Default to work for unknown hostnames
35+
echo "work"
36+
;;
37+
esac
38+
}
39+
40+
# Set machine-specific environment variables based on detected type
41+
set_machine_variables() {
42+
local machine_type="$1"
43+
44+
# Reset all machine variables
45+
export IS_AIR=false
46+
export IS_MINI=false
47+
export IS_WORK=false
48+
49+
# Set the appropriate variable based on machine type
50+
case "$machine_type" in
51+
"air")
52+
export IS_AIR=true
53+
export MACHINE="air"
54+
;;
55+
"mini")
56+
export IS_MINI=true
57+
export MACHINE="mini"
58+
;;
59+
*)
60+
# Default to work for unknown types (including explicit "work")
61+
export IS_WORK=true
62+
export MACHINE="work"
63+
;;
64+
esac
65+
}
66+
67+
# Initialize machine detection and set variables
68+
init_machine_detection() {
69+
local detected_type
70+
detected_type=$(detect_machine_type)
71+
set_machine_variables "$detected_type"
72+
73+
# Optional: print detection result for debugging
74+
if [[ "${DEBUG_MACHINE_DETECTION:-false}" == "true" ]]; then
75+
echo "Detected machine type: $MACHINE" >&2
76+
fi
77+
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
#!/usr/bin/env bash
2+
3+
# Prerequisite validation utilities for dotfiles setup
4+
# Validates system requirements before installation begins
5+
6+
set -euo pipefail
7+
8+
# Minimum supported macOS version (major.minor)
9+
readonly MIN_MACOS_VERSION="12.0"
10+
11+
# Validate Command Line Tools are installed
12+
validate_command_line_tools() {
13+
local xcode_path
14+
15+
# Check if xcode-select can find the developer directory
16+
if xcode_path=$(xcode-select -p 2>/dev/null); then
17+
# Verify the path exists and contains expected tools
18+
if [[ -d "$xcode_path" && -f "$xcode_path/usr/bin/git" ]]; then
19+
return 0
20+
else
21+
echo "Command Line Tools directory exists but appears incomplete" >&2
22+
return 1
23+
fi
24+
else
25+
echo "Command Line Tools not found. Install with: xcode-select --install" >&2
26+
return 1
27+
fi
28+
}
29+
30+
# Validate network connectivity to essential services
31+
validate_network_connectivity() {
32+
local hosts=("github.com" "raw.githubusercontent.com")
33+
local failed_hosts=()
34+
35+
for host in "${hosts[@]}"; do
36+
# Use ping with timeout to test connectivity
37+
if ! ping -c 1 -W 3000 "$host" >/dev/null 2>&1; then
38+
failed_hosts+=("$host")
39+
fi
40+
done
41+
42+
if [[ ${#failed_hosts[@]} -gt 0 ]]; then
43+
echo "Network connectivity failed for: ${failed_hosts[*]}" >&2
44+
echo "Please check your internet connection and try again" >&2
45+
return 1
46+
fi
47+
48+
return 0
49+
}
50+
51+
# Validate directory exists and has proper permissions
52+
validate_directory_permissions() {
53+
local directory="$1"
54+
55+
if [[ -z "$directory" ]]; then
56+
echo "Directory path is required" >&2
57+
return 1
58+
fi
59+
60+
# Check if directory exists
61+
if [[ ! -d "$directory" ]]; then
62+
echo "Directory does not exist: $directory" >&2
63+
return 1
64+
fi
65+
66+
return 0
67+
}
68+
69+
# Validate write permissions to a directory
70+
validate_write_permissions() {
71+
local directory="$1"
72+
73+
if [[ -z "$directory" ]]; then
74+
echo "Directory path is required" >&2
75+
return 1
76+
fi
77+
78+
# Check if directory exists first
79+
if [[ ! -d "$directory" ]]; then
80+
echo "Directory does not exist: $directory" >&2
81+
return 1
82+
fi
83+
84+
# Test write permissions by creating a temporary file
85+
local test_file="$directory/.dotfiles_write_test_$$"
86+
if touch "$test_file" 2>/dev/null; then
87+
rm -f "$test_file"
88+
return 0
89+
else
90+
echo "No write permission for directory: $directory" >&2
91+
return 1
92+
fi
93+
}
94+
95+
# Validate macOS version compatibility
96+
validate_macos_version() {
97+
local current_version
98+
local major minor
99+
local min_major min_minor
100+
101+
# Get current macOS version
102+
current_version=$(sw_vers -productVersion 2>/dev/null)
103+
if [[ -z "$current_version" ]]; then
104+
echo "Unable to determine macOS version" >&2
105+
return 1
106+
fi
107+
108+
# Parse current version
109+
major=$(echo "$current_version" | cut -d. -f1)
110+
minor=$(echo "$current_version" | cut -d. -f2)
111+
112+
# Parse minimum version
113+
min_major=$(echo "$MIN_MACOS_VERSION" | cut -d. -f1)
114+
min_minor=$(echo "$MIN_MACOS_VERSION" | cut -d. -f2)
115+
116+
# Compare versions
117+
if [[ "$major" -gt "$min_major" ]] ||
118+
[[ "$major" -eq "$min_major" && "$minor" -ge "$min_minor" ]]; then
119+
return 0
120+
else
121+
echo "Unsupported macOS version: $current_version (minimum: $MIN_MACOS_VERSION)" >&2
122+
return 1
123+
fi
124+
}
125+
126+
# Validate essential directories for dotfiles installation
127+
validate_essential_directories() {
128+
local directories=(
129+
"$HOME"
130+
"/usr/local"
131+
"/opt"
132+
)
133+
134+
local failed_dirs=()
135+
136+
for dir in "${directories[@]}"; do
137+
if ! validate_directory_permissions "$dir"; then
138+
failed_dirs+=("$dir")
139+
fi
140+
done
141+
142+
if [[ ${#failed_dirs[@]} -gt 0 ]]; then
143+
echo "Essential directory validation failed for: ${failed_dirs[*]}" >&2
144+
return 1
145+
fi
146+
147+
return 0
148+
}
149+
150+
# Run comprehensive prerequisite validation
151+
run_prerequisite_validation() {
152+
local validation_functions=(
153+
"validate_command_line_tools"
154+
"validate_network_connectivity"
155+
"validate_macos_version"
156+
"validate_essential_directories"
157+
)
158+
159+
local failed_validations=()
160+
local exit_code=0
161+
162+
echo "Running prerequisite validation checks..."
163+
164+
for validation_func in "${validation_functions[@]}"; do
165+
echo " Checking: ${validation_func#validate_}"
166+
167+
if ! "$validation_func"; then
168+
failed_validations+=("$validation_func")
169+
exit_code=1
170+
else
171+
echo " ✓ Passed"
172+
fi
173+
done
174+
175+
if [[ $exit_code -eq 0 ]]; then
176+
echo "✅ All prerequisite validation checks passed"
177+
else
178+
echo "❌ Prerequisite validation failed:"
179+
for failed_func in "${failed_validations[@]}"; do
180+
echo " - ${failed_func#validate_}"
181+
done
182+
fi
183+
184+
return $exit_code
185+
}

0 commit comments

Comments
 (0)