1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-15 16:55:06 +00:00
This commit is contained in:
Tw93
2025-11-25 17:25:13 +08:00
parent 2830eb69ca
commit e517bf4a3e
16 changed files with 331 additions and 326 deletions

View File

@@ -90,5 +90,4 @@ main() {
printf '\n' printf '\n'
} }
main "$@" main "$@"

View File

@@ -666,8 +666,8 @@ perform_cleanup() {
fi fi
# Mark permissions as granted (won't prompt again) # Mark permissions as granted (won't prompt again)
mkdir -p "$(dirname "$permission_flag")" 2>/dev/null || true mkdir -p "$(dirname "$permission_flag")" 2> /dev/null || true
touch "$permission_flag" 2>/dev/null || true touch "$permission_flag" 2> /dev/null || true
fi fi
# Show whitelist info if patterns are active # Show whitelist info if patterns are active
@@ -730,7 +730,7 @@ perform_cleanup() {
# Check if cache is valid (less than 2 days old) # Check if cache is valid (less than 2 days old)
if [[ -f "$cask_cache" ]]; then if [[ -f "$cask_cache" ]]; then
local cache_age=$(( $(date +%s) - $(stat -f%m "$cask_cache") )) local cache_age=$(($(date +%s) - $(stat -f%m "$cask_cache")))
if [[ $cache_age -lt 172800 ]]; then if [[ $cache_age -lt 172800 ]]; then
use_cache=true use_cache=true
fi fi
@@ -750,7 +750,7 @@ perform_cleanup() {
while IFS= read -r cask; do while IFS= read -r cask; do
# Get app path from cask info # Get app path from cask info
local cask_info local cask_info
cask_info=$(brew info --cask "$cask" 2>/dev/null || true) cask_info=$(brew info --cask "$cask" 2> /dev/null || true)
# Extract app name from "AppName.app (App)" format # Extract app name from "AppName.app (App)" format
local app_name local app_name
@@ -764,13 +764,13 @@ perform_cleanup() {
# Check if app exists # Check if app exists
[[ ! -e "/Applications/$app_name" ]] && orphaned_casks+=("$cask") [[ ! -e "/Applications/$app_name" ]] && orphaned_casks+=("$cask")
done < <(brew list --cask 2>/dev/null || true) done < <(brew list --cask 2> /dev/null || true)
fi fi
# Remove orphaned casks if found # Remove orphaned casks if found
if [[ ${#orphaned_casks[@]} -gt 0 ]]; then if [[ ${#orphaned_casks[@]} -gt 0 ]]; then
# Check if sudo session is still valid (without prompting) # Check if sudo session is still valid (without prompting)
if sudo -n true 2>/dev/null; then if sudo -n true 2> /dev/null; then
if [[ -t 1 ]]; then if [[ -t 1 ]]; then
stop_inline_spinner stop_inline_spinner
MOLE_SPINNER_PREFIX=" " start_inline_spinner "Cleaning orphaned casks..." MOLE_SPINNER_PREFIX=" " start_inline_spinner "Cleaning orphaned casks..."
@@ -1041,7 +1041,7 @@ perform_cleanup() {
if [[ -f "$brew_cache_file" ]]; then if [[ -f "$brew_cache_file" ]]; then
local last_cleanup local last_cleanup
last_cleanup=$(cat "$brew_cache_file" 2>/dev/null || echo "0") last_cleanup=$(cat "$brew_cache_file" 2> /dev/null || echo "0")
local current_time local current_time
current_time=$(date +%s) current_time=$(date +%s)
local time_diff=$((current_time - last_cleanup)) local time_diff=$((current_time - last_cleanup))
@@ -1185,16 +1185,16 @@ perform_cleanup() {
local find_timeout=10 local find_timeout=10
local elapsed=0 local elapsed=0
while kill -0 $find_pid 2>/dev/null && [[ $elapsed -lt $find_timeout ]]; do while kill -0 $find_pid 2> /dev/null && [[ $elapsed -lt $find_timeout ]]; do
sleep 1 sleep 1
((elapsed++)) ((elapsed++))
done done
if kill -0 $find_pid 2>/dev/null; then if kill -0 $find_pid 2> /dev/null; then
kill -TERM $find_pid 2>/dev/null || true kill -TERM $find_pid 2> /dev/null || true
wait $find_pid 2>/dev/null || true wait $find_pid 2> /dev/null || true
else else
wait $find_pid 2>/dev/null || true wait $find_pid 2> /dev/null || true
fi fi
while IFS= read -r next_dir; do while IFS= read -r next_dir; do
@@ -1226,16 +1226,16 @@ perform_cleanup() {
local find_timeout=10 local find_timeout=10
local elapsed=0 local elapsed=0
while kill -0 $find_pid 2>/dev/null && [[ $elapsed -lt $find_timeout ]]; do while kill -0 $find_pid 2> /dev/null && [[ $elapsed -lt $find_timeout ]]; do
sleep 1 sleep 1
((elapsed++)) ((elapsed++))
done done
if kill -0 $find_pid 2>/dev/null; then if kill -0 $find_pid 2> /dev/null; then
kill -TERM $find_pid 2>/dev/null || true kill -TERM $find_pid 2> /dev/null || true
wait $find_pid 2>/dev/null || true wait $find_pid 2> /dev/null || true
else else
wait $find_pid 2>/dev/null || true wait $find_pid 2> /dev/null || true
fi fi
while IFS= read -r pycache; do while IFS= read -r pycache; do

View File

@@ -73,7 +73,7 @@ run_system_checks() {
show_optimization_summary() { show_optimization_summary() {
local safe_count="${OPTIMIZE_SAFE_COUNT:-0}" local safe_count="${OPTIMIZE_SAFE_COUNT:-0}"
local confirm_count="${OPTIMIZE_CONFIRM_COUNT:-0}" local confirm_count="${OPTIMIZE_CONFIRM_COUNT:-0}"
if (( safe_count == 0 && confirm_count == 0 )) && [[ -z "${AUTO_FIX_SUMMARY:-}" ]]; then if ((safe_count == 0 && confirm_count == 0)) && [[ -z "${AUTO_FIX_SUMMARY:-}" ]]; then
return return
fi fi
echo "" echo ""
@@ -104,7 +104,6 @@ show_optimization_summary() {
print_summary_block "success" "$summary_title" "${summary_details[@]}" print_summary_block "success" "$summary_title" "${summary_details[@]}"
} }
show_system_health() { show_system_health() {
local health_json="$1" local health_json="$1"
@@ -315,7 +314,6 @@ perform_security_fixes() {
SECURITY_FIXES=() SECURITY_FIXES=()
} }
cleanup_all() { cleanup_all() {
stop_sudo_session stop_sudo_session
cleanup_temp_files cleanup_temp_files

View File

@@ -39,7 +39,7 @@ show_suggestions() {
# Health suggestions # Health suggestions
if [[ -n "${CACHE_SIZE_GB:-}" ]]; then if [[ -n "${CACHE_SIZE_GB:-}" ]]; then
local cache_gb="${CACHE_SIZE_GB:-0}" local cache_gb="${CACHE_SIZE_GB:-0}"
if (( $(echo "$cache_gb > 5" | bc -l 2>/dev/null || echo 0) )); then if (($(echo "$cache_gb > 5" | bc -l 2> /dev/null || echo 0))); then
manual_items+=("Free up ${cache_gb}GB by cleaning caches|Run: mo clean") manual_items+=("Free up ${cache_gb}GB by cleaning caches|Run: mo clean")
has_suggestions=true has_suggestions=true
fi fi
@@ -51,7 +51,7 @@ show_suggestions() {
fi fi
if [[ -n "${DISK_FREE_GB:-}" && "${DISK_FREE_GB:-0}" -lt 50 ]]; then if [[ -n "${DISK_FREE_GB:-}" && "${DISK_FREE_GB:-0}" -lt 50 ]]; then
if [[ -z "${CACHE_SIZE_GB:-}" ]] || (( $(echo "${CACHE_SIZE_GB:-0} <= 5" | bc -l 2>/dev/null || echo 1) )); then if [[ -z "${CACHE_SIZE_GB:-}" ]] || (($(echo "${CACHE_SIZE_GB:-0} <= 5" | bc -l 2> /dev/null || echo 1))); then
manual_items+=("Low disk space (${DISK_FREE_GB}GB free)|Run: mo analyze to find large files") manual_items+=("Low disk space (${DISK_FREE_GB}GB free)|Run: mo analyze to find large files")
has_suggestions=true has_suggestions=true
fi fi
@@ -132,7 +132,7 @@ perform_auto_fix() {
# Fix Firewall # Fix Firewall
if [[ -n "${FIREWALL_DISABLED:-}" && "${FIREWALL_DISABLED}" == "true" ]]; then if [[ -n "${FIREWALL_DISABLED:-}" && "${FIREWALL_DISABLED}" == "true" ]]; then
echo -e "${BLUE}Enabling Firewall...${NC}" echo -e "${BLUE}Enabling Firewall...${NC}"
if sudo defaults write /Library/Preferences/com.apple.alf globalstate -int 1 2>/dev/null; then if sudo defaults write /Library/Preferences/com.apple.alf globalstate -int 1 2> /dev/null; then
echo -e "${GREEN}${NC} Firewall enabled" echo -e "${GREEN}${NC} Firewall enabled"
((fixed_count++)) ((fixed_count++))
fixed_items+=("Firewall enabled") fixed_items+=("Firewall enabled")
@@ -148,7 +148,7 @@ perform_auto_fix() {
local pam_file="/etc/pam.d/sudo" local pam_file="/etc/pam.d/sudo"
if sudo bash -c "grep -q 'pam_tid.so' '$pam_file' 2>/dev/null || sed -i '' '2i\\ if sudo bash -c "grep -q 'pam_tid.so' '$pam_file' 2>/dev/null || sed -i '' '2i\\
auth sufficient pam_tid.so auth sufficient pam_tid.so
' '$pam_file'" 2>/dev/null; then ' '$pam_file'" 2> /dev/null; then
echo -e "${GREEN}${NC} Touch ID configured" echo -e "${GREEN}${NC} Touch ID configured"
((fixed_count++)) ((fixed_count++))
fixed_items+=("Touch ID configured for sudo") fixed_items+=("Touch ID configured for sudo")

View File

@@ -5,13 +5,13 @@
check_touchid_sudo() { check_touchid_sudo() {
# Check if Touch ID is configured for sudo # Check if Touch ID is configured for sudo
local pam_file="/etc/pam.d/sudo" local pam_file="/etc/pam.d/sudo"
if [[ -f "$pam_file" ]] && grep -q "pam_tid.so" "$pam_file" 2>/dev/null; then if [[ -f "$pam_file" ]] && grep -q "pam_tid.so" "$pam_file" 2> /dev/null; then
echo -e " ${GREEN}${NC} Touch ID Enabled for sudo" echo -e " ${GREEN}${NC} Touch ID Enabled for sudo"
else else
# Check if Touch ID is supported # Check if Touch ID is supported
local is_supported=false local is_supported=false
if command -v bioutil > /dev/null 2>&1; then if command -v bioutil > /dev/null 2>&1; then
if bioutil -r 2>/dev/null | grep -q "Touch ID"; then if bioutil -r 2> /dev/null | grep -q "Touch ID"; then
is_supported=true is_supported=true
fi fi
elif [[ "$(uname -m)" == "arm64" ]]; then elif [[ "$(uname -m)" == "arm64" ]]; then
@@ -40,8 +40,8 @@ check_rosetta() {
check_git_config() { check_git_config() {
# Check basic Git configuration # Check basic Git configuration
if command -v git > /dev/null 2>&1; then if command -v git > /dev/null 2>&1; then
local git_name=$(git config --global user.name 2>/dev/null || echo "") local git_name=$(git config --global user.name 2> /dev/null || echo "")
local git_email=$(git config --global user.email 2>/dev/null || echo "") local git_email=$(git config --global user.email 2> /dev/null || echo "")
if [[ -n "$git_name" && -n "$git_email" ]]; then if [[ -n "$git_name" && -n "$git_email" ]]; then
echo -e " ${GREEN}${NC} Git Config Configured" echo -e " ${GREEN}${NC} Git Config Configured"

View File

@@ -20,14 +20,14 @@ check_disk_space() {
check_memory_usage() { check_memory_usage() {
local mem_total local mem_total
mem_total=$(sysctl -n hw.memsize 2>/dev/null || echo "0") mem_total=$(sysctl -n hw.memsize 2> /dev/null || echo "0")
if [[ -z "$mem_total" || "$mem_total" -le 0 ]]; then if [[ -z "$mem_total" || "$mem_total" -le 0 ]]; then
echo -e " ${GRAY}-${NC} Memory Unable to determine" echo -e " ${GRAY}-${NC} Memory Unable to determine"
return return
fi fi
local vm_output local vm_output
vm_output=$(vm_stat 2>/dev/null || echo "") vm_output=$(vm_stat 2> /dev/null || echo "")
local page_size local page_size
page_size=$(echo "$vm_output" | awk '/page size of/ {print $8}') page_size=$(echo "$vm_output" | awk '/page size of/ {print $8}')
@@ -133,7 +133,7 @@ check_cache_size() {
for cache_path in "${cache_paths[@]}"; do for cache_path in "${cache_paths[@]}"; do
if [[ -d "$cache_path" ]]; then if [[ -d "$cache_path" ]]; then
local size_output local size_output
size_output=$(du -sk "$cache_path" 2>/dev/null | awk 'NR==1 {print $1}' | tr -d '[:space:]' || echo "") size_output=$(du -sk "$cache_path" 2> /dev/null | awk 'NR==1 {print $1}' | tr -d '[:space:]' || echo "")
[[ "$size_output" =~ ^[0-9]+$ ]] || size_output=0 [[ "$size_output" =~ ^[0-9]+$ ]] || size_output=0
cache_size_kb=$((cache_size_kb + size_output)) cache_size_kb=$((cache_size_kb + size_output))
fi fi
@@ -162,7 +162,7 @@ check_cache_size() {
check_swap_usage() { check_swap_usage() {
# Check swap usage # Check swap usage
if command -v sysctl > /dev/null 2>&1; then if command -v sysctl > /dev/null 2>&1; then
local swap_info=$(sysctl vm.swapusage 2>/dev/null || echo "") local swap_info=$(sysctl vm.swapusage 2> /dev/null || echo "")
if [[ -n "$swap_info" ]]; then if [[ -n "$swap_info" ]]; then
local swap_used=$(echo "$swap_info" | grep -o "used = [0-9.]*[GM]" | awk '{print $3}' || echo "0M") local swap_used=$(echo "$swap_info" | grep -o "used = [0-9.]*[GM]" | awk '{print $3}' || echo "0M")
local swap_num=$(echo "$swap_used" | sed 's/[GM]//') local swap_num=$(echo "$swap_used" | sed 's/[GM]//')
@@ -184,13 +184,13 @@ check_swap_usage() {
check_timemachine() { check_timemachine() {
# Check Time Machine backup status # Check Time Machine backup status
if command -v tmutil > /dev/null 2>&1; then if command -v tmutil > /dev/null 2>&1; then
local tm_status=$(tmutil latestbackup 2>/dev/null || echo "") local tm_status=$(tmutil latestbackup 2> /dev/null || echo "")
if [[ -z "$tm_status" ]]; then if [[ -z "$tm_status" ]]; then
echo -e " ${YELLOW}${ICON_WARNING}${NC} Time Machine No backups found" echo -e " ${YELLOW}${ICON_WARNING}${NC} Time Machine No backups found"
echo -e " ${GRAY}Set up in System Settings → General → Time Machine (optional but recommended)${NC}" echo -e " ${GRAY}Set up in System Settings → General → Time Machine (optional but recommended)${NC}"
else else
# Get last backup time # Get last backup time
local backup_date=$(tmutil latestbackup 2>/dev/null | xargs basename 2>/dev/null || echo "") local backup_date=$(tmutil latestbackup 2> /dev/null | xargs basename 2> /dev/null || echo "")
if [[ -n "$backup_date" ]]; then if [[ -n "$backup_date" ]]; then
echo -e " ${GREEN}${NC} Time Machine Backup active" echo -e " ${GREEN}${NC} Time Machine Backup active"
else else

View File

@@ -5,7 +5,7 @@
check_filevault() { check_filevault() {
# Check FileVault encryption status # Check FileVault encryption status
if command -v fdesetup > /dev/null 2>&1; then if command -v fdesetup > /dev/null 2>&1; then
local fv_status=$(fdesetup status 2>/dev/null || echo "") local fv_status=$(fdesetup status 2> /dev/null || echo "")
if echo "$fv_status" | grep -q "FileVault is On"; then if echo "$fv_status" | grep -q "FileVault is On"; then
echo -e " ${GREEN}${NC} FileVault Enabled" echo -e " ${GREEN}${NC} FileVault Enabled"
else else
@@ -18,7 +18,7 @@ check_filevault() {
check_firewall() { check_firewall() {
# Check firewall status # Check firewall status
unset FIREWALL_DISABLED unset FIREWALL_DISABLED
local firewall_status=$(defaults read /Library/Preferences/com.apple.alf globalstate 2>/dev/null || echo "0") local firewall_status=$(defaults read /Library/Preferences/com.apple.alf globalstate 2> /dev/null || echo "0")
if [[ "$firewall_status" == "1" || "$firewall_status" == "2" ]]; then if [[ "$firewall_status" == "1" || "$firewall_status" == "2" ]]; then
echo -e " ${GREEN}${NC} Firewall Enabled" echo -e " ${GREEN}${NC} Firewall Enabled"
else else
@@ -32,7 +32,7 @@ check_firewall() {
check_gatekeeper() { check_gatekeeper() {
# Check Gatekeeper status # Check Gatekeeper status
if command -v spctl > /dev/null 2>&1; then if command -v spctl > /dev/null 2>&1; then
local gk_status=$(spctl --status 2>/dev/null || echo "") local gk_status=$(spctl --status 2> /dev/null || echo "")
if echo "$gk_status" | grep -q "enabled"; then if echo "$gk_status" | grep -q "enabled"; then
echo -e " ${GREEN}${NC} Gatekeeper Active" echo -e " ${GREEN}${NC} Gatekeeper Active"
unset GATEKEEPER_DISABLED unset GATEKEEPER_DISABLED
@@ -48,7 +48,7 @@ check_gatekeeper() {
check_sip() { check_sip() {
# Check System Integrity Protection # Check System Integrity Protection
if command -v csrutil > /dev/null 2>&1; then if command -v csrutil > /dev/null 2>&1; then
local sip_status=$(csrutil status 2>/dev/null || echo "") local sip_status=$(csrutil status 2> /dev/null || echo "")
if echo "$sip_status" | grep -q "enabled"; then if echo "$sip_status" | grep -q "enabled"; then
echo -e " ${GREEN}${NC} SIP Enabled" echo -e " ${GREEN}${NC} SIP Enabled"
else else

View File

@@ -8,11 +8,11 @@ CACHE_DIR="${HOME}/.cache/mole"
CACHE_TTL=600 # 10 minutes in seconds CACHE_TTL=600 # 10 minutes in seconds
# Ensure cache directory exists # Ensure cache directory exists
mkdir -p "$CACHE_DIR" 2>/dev/null || true mkdir -p "$CACHE_DIR" 2> /dev/null || true
clear_cache_file() { clear_cache_file() {
local file="$1" local file="$1"
rm -f "$file" 2>/dev/null || true rm -f "$file" 2> /dev/null || true
} }
reset_brew_cache() { reset_brew_cache() {
@@ -37,7 +37,7 @@ is_cache_valid() {
return 1 return 1
fi fi
local cache_age=$(($(date +%s) - $(stat -f%m "$cache_file" 2>/dev/null || echo 0))) local cache_age=$(($(date +%s) - $(stat -f%m "$cache_file" 2> /dev/null || echo 0)))
[[ $cache_age -lt $ttl ]] [[ $cache_age -lt $ttl ]]
} }
@@ -51,7 +51,7 @@ check_homebrew_updates() {
local cask_count=0 local cask_count=0
if is_cache_valid "$cache_file"; then if is_cache_valid "$cache_file"; then
read -r formula_count cask_count < "$cache_file" 2>/dev/null || true read -r formula_count cask_count < "$cache_file" 2> /dev/null || true
formula_count=${formula_count:-0} formula_count=${formula_count:-0}
cask_count=${cask_count:-0} cask_count=${cask_count:-0}
else else
@@ -61,18 +61,18 @@ check_homebrew_updates() {
fi fi
local outdated_list="" local outdated_list=""
outdated_list=$(brew outdated --quiet 2>/dev/null || echo "") outdated_list=$(brew outdated --quiet 2> /dev/null || echo "")
if [[ -n "$outdated_list" ]]; then if [[ -n "$outdated_list" ]]; then
formula_count=$(echo "$outdated_list" | wc -l | tr -d ' ') formula_count=$(echo "$outdated_list" | wc -l | tr -d ' ')
fi fi
local cask_list="" local cask_list=""
cask_list=$(brew outdated --cask --quiet 2>/dev/null || echo "") cask_list=$(brew outdated --cask --quiet 2> /dev/null || echo "")
if [[ -n "$cask_list" ]]; then if [[ -n "$cask_list" ]]; then
cask_count=$(echo "$cask_list" | wc -l | tr -d ' ') cask_count=$(echo "$cask_list" | wc -l | tr -d ' ')
fi fi
echo "$formula_count $cask_count" > "$cache_file" 2>/dev/null || true echo "$formula_count $cask_count" > "$cache_file" 2> /dev/null || true
# Stop spinner before output # Stop spinner before output
if [[ -t 1 ]]; then if [[ -t 1 ]]; then
@@ -110,7 +110,7 @@ get_software_updates() {
if [[ -z "$SOFTWARE_UPDATE_LIST" ]]; then if [[ -z "$SOFTWARE_UPDATE_LIST" ]]; then
# Check cache first # Check cache first
if is_cache_valid "$cache_file"; then if is_cache_valid "$cache_file"; then
SOFTWARE_UPDATE_LIST=$(cat "$cache_file" 2>/dev/null || echo "") SOFTWARE_UPDATE_LIST=$(cat "$cache_file" 2> /dev/null || echo "")
else else
# Show spinner while checking (only on first call) # Show spinner while checking (only on first call)
local show_spinner=false local show_spinner=false
@@ -120,9 +120,9 @@ get_software_updates() {
export SOFTWAREUPDATE_SPINNER_SHOWN="true" export SOFTWAREUPDATE_SPINNER_SHOWN="true"
fi fi
SOFTWARE_UPDATE_LIST=$(softwareupdate -l 2>/dev/null || echo "") SOFTWARE_UPDATE_LIST=$(softwareupdate -l 2> /dev/null || echo "")
# Save to cache # Save to cache
echo "$SOFTWARE_UPDATE_LIST" > "$cache_file" 2>/dev/null || true echo "$SOFTWARE_UPDATE_LIST" > "$cache_file" 2> /dev/null || true
# Stop spinner # Stop spinner
if [[ "$show_spinner" == "true" ]]; then if [[ "$show_spinner" == "true" ]]; then
@@ -208,7 +208,7 @@ check_mole_update() {
# Auto-detect version from mole main script # Auto-detect version from mole main script
local current_version local current_version
if [[ -f "${SCRIPT_DIR:-/usr/local/bin}/mole" ]]; then if [[ -f "${SCRIPT_DIR:-/usr/local/bin}/mole" ]]; then
current_version=$(grep '^VERSION=' "${SCRIPT_DIR:-/usr/local/bin}/mole" 2>/dev/null | head -1 | sed 's/VERSION="\(.*\)"/\1/' || echo "unknown") current_version=$(grep '^VERSION=' "${SCRIPT_DIR:-/usr/local/bin}/mole" 2> /dev/null | head -1 | sed 's/VERSION="\(.*\)"/\1/' || echo "unknown")
else else
current_version="${VERSION:-unknown}" current_version="${VERSION:-unknown}"
fi fi
@@ -220,7 +220,7 @@ check_mole_update() {
# Check cache first # Check cache first
if is_cache_valid "$cache_file"; then if is_cache_valid "$cache_file"; then
latest_version=$(cat "$cache_file" 2>/dev/null || echo "") latest_version=$(cat "$cache_file" 2> /dev/null || echo "")
else else
# Show spinner while checking # Show spinner while checking
if [[ -t 1 ]]; then if [[ -t 1 ]]; then
@@ -229,10 +229,10 @@ check_mole_update() {
# Try to get latest version from GitHub # Try to get latest version from GitHub
if command -v curl > /dev/null 2>&1; then if command -v curl > /dev/null 2>&1; then
latest_version=$(curl -fsSL https://api.github.com/repos/tw93/mole/releases/latest 2>/dev/null | grep '"tag_name"' | sed -E 's/.*"v?([^"]+)".*/\1/' || echo "") latest_version=$(curl -fsSL https://api.github.com/repos/tw93/mole/releases/latest 2> /dev/null | grep '"tag_name"' | sed -E 's/.*"v?([^"]+)".*/\1/' || echo "")
# Save to cache # Save to cache
if [[ -n "$latest_version" ]]; then if [[ -n "$latest_version" ]]; then
echo "$latest_version" > "$cache_file" 2>/dev/null || true echo "$latest_version" > "$cache_file" 2> /dev/null || true
fi fi
fi fi

View File

@@ -532,9 +532,9 @@ is_clamshell_mode() {
# Check if lid is closed; ignore pipeline failures so set -e doesn't exit # Check if lid is closed; ignore pipeline failures so set -e doesn't exit
local clamshell_state="" local clamshell_state=""
clamshell_state=$( (ioreg -r -k AppleClamshellState -d 4 2>/dev/null \ clamshell_state=$( (ioreg -r -k AppleClamshellState -d 4 2> /dev/null |
| grep "AppleClamshellState" \ grep "AppleClamshellState" |
| head -1) || true ) head -1) || true)
if [[ "$clamshell_state" =~ \"AppleClamshellState\"\ =\ Yes ]]; then if [[ "$clamshell_state" =~ \"AppleClamshellState\"\ =\ Yes ]]; then
return 0 # Lid is closed return 0 # Lid is closed
@@ -549,7 +549,7 @@ _request_password() {
local show_hint=true local show_hint=true
# Extra safety: ensure sudo cache is cleared before password input # Extra safety: ensure sudo cache is cleared before password input
sudo -k 2>/dev/null sudo -k 2> /dev/null
while ((attempts < 3)); do while ((attempts < 3)); do
local password="" local password=""
@@ -637,11 +637,11 @@ request_sudo_access() {
local elapsed=0 local elapsed=0
local timeout=50 # 50 * 0.1s = 5 seconds local timeout=50 # 50 * 0.1s = 5 seconds
while ((elapsed < timeout)); do while ((elapsed < timeout)); do
if ! kill -0 "$sudo_pid" 2>/dev/null; then if ! kill -0 "$sudo_pid" 2> /dev/null; then
# Process exited # Process exited
wait "$sudo_pid" 2>/dev/null wait "$sudo_pid" 2> /dev/null
local exit_code=$? local exit_code=$?
if [[ $exit_code -eq 0 ]] && sudo -n true 2>/dev/null; then if [[ $exit_code -eq 0 ]] && sudo -n true 2> /dev/null; then
# Touch ID succeeded # Touch ID succeeded
return 0 return 0
fi fi
@@ -655,13 +655,13 @@ request_sudo_access() {
# Touch ID failed/cancelled - clean up thoroughly before password input # Touch ID failed/cancelled - clean up thoroughly before password input
# Kill the sudo process if still running # Kill the sudo process if still running
if kill -0 "$sudo_pid" 2>/dev/null; then if kill -0 "$sudo_pid" 2> /dev/null; then
kill -9 "$sudo_pid" 2>/dev/null kill -9 "$sudo_pid" 2> /dev/null
wait "$sudo_pid" 2>/dev/null || true wait "$sudo_pid" 2> /dev/null || true
fi fi
# Clear sudo state immediately # Clear sudo state immediately
sudo -k 2>/dev/null sudo -k 2> /dev/null
# IMPORTANT: Wait longer for macOS to fully close Touch ID UI and SecurityAgent # IMPORTANT: Wait longer for macOS to fully close Touch ID UI and SecurityAgent
# Without this delay, subsequent sudo calls may re-trigger Touch ID # Without this delay, subsequent sudo calls may re-trigger Touch ID
@@ -702,17 +702,17 @@ update_via_homebrew() {
# Use background process to allow interruption # Use background process to allow interruption
local brew_update_timeout="${MO_BREW_UPDATE_TIMEOUT:-300}" local brew_update_timeout="${MO_BREW_UPDATE_TIMEOUT:-300}"
local brew_tmp_file local brew_tmp_file
brew_tmp_file=$(mktemp -t mole-brew-update 2>/dev/null || echo "/tmp/mole-brew-update.$$") brew_tmp_file=$(mktemp -t mole-brew-update 2> /dev/null || echo "/tmp/mole-brew-update.$$")
(brew update > "$brew_tmp_file" 2>&1) & (brew update > "$brew_tmp_file" 2>&1) &
local brew_pid=$! local brew_pid=$!
local elapsed=0 local elapsed=0
# Wait for completion or timeout # Wait for completion or timeout
while kill -0 $brew_pid 2>/dev/null; do while kill -0 $brew_pid 2> /dev/null; do
if [[ $elapsed -ge $brew_update_timeout ]]; then if [[ $elapsed -ge $brew_update_timeout ]]; then
kill -TERM $brew_pid 2>/dev/null || true kill -TERM $brew_pid 2> /dev/null || true
wait $brew_pid 2>/dev/null || true wait $brew_pid 2> /dev/null || true
if [[ -t 1 ]]; then stop_inline_spinner; fi if [[ -t 1 ]]; then stop_inline_spinner; fi
rm -f "$brew_tmp_file" rm -f "$brew_tmp_file"
log_error "Homebrew update timed out (${brew_update_timeout}s)" log_error "Homebrew update timed out (${brew_update_timeout}s)"
@@ -722,7 +722,7 @@ update_via_homebrew() {
((elapsed++)) ((elapsed++))
done done
wait $brew_pid 2>/dev/null || { wait $brew_pid 2> /dev/null || {
if [[ -t 1 ]]; then stop_inline_spinner; fi if [[ -t 1 ]]; then stop_inline_spinner; fi
rm -f "$brew_tmp_file" rm -f "$brew_tmp_file"
log_error "Homebrew update failed" log_error "Homebrew update failed"

View File

@@ -22,13 +22,13 @@ _pm_get_terminal_height() {
# Try stty size first (most reliable, real-time) # Try stty size first (most reliable, real-time)
# Use </dev/tty to ensure we read from terminal even if stdin is redirected # Use </dev/tty to ensure we read from terminal even if stdin is redirected
if [[ -t 0 ]] || [[ -t 2 ]]; then if [[ -t 0 ]] || [[ -t 2 ]]; then
height=$(stty size </dev/tty 2>/dev/null | awk '{print $1}') height=$(stty size < /dev/tty 2> /dev/null | awk '{print $1}')
fi fi
# Fallback to tput # Fallback to tput
if [[ -z "$height" || $height -le 0 ]]; then if [[ -z "$height" || $height -le 0 ]]; then
if command -v tput > /dev/null 2>&1; then if command -v tput > /dev/null 2>&1; then
height=$(tput lines 2>/dev/null || echo "24") height=$(tput lines 2> /dev/null || echo "24")
else else
height=24 height=24
fi fi

View File

@@ -14,13 +14,13 @@ _ms_get_terminal_height() {
# Try stty size first (most reliable, real-time) # Try stty size first (most reliable, real-time)
# Use </dev/tty to ensure we read from terminal even if stdin is redirected # Use </dev/tty to ensure we read from terminal even if stdin is redirected
if [[ -t 0 ]] || [[ -t 2 ]]; then if [[ -t 0 ]] || [[ -t 2 ]]; then
height=$(stty size </dev/tty 2>/dev/null | awk '{print $1}') height=$(stty size < /dev/tty 2> /dev/null | awk '{print $1}')
fi fi
# Fallback to tput # Fallback to tput
if [[ -z "$height" || $height -le 0 ]]; then if [[ -z "$height" || $height -le 0 ]]; then
if command -v tput > /dev/null 2>&1; then if command -v tput > /dev/null 2>&1; then
height=$(tput lines 2>/dev/null || echo "24") height=$(tput lines 2> /dev/null || echo "24")
else else
height=24 height=24
fi fi

View File

@@ -8,7 +8,10 @@ readonly MAIL_DOWNLOADS_MIN_KB=5120 # ~5MB threshold
_opt_get_dir_size_kb() { _opt_get_dir_size_kb() {
local path="$1" local path="$1"
[[ -e "$path" ]] || { echo 0; return; } [[ -e "$path" ]] || {
echo 0
return
}
du -sk "$path" 2> /dev/null | awk '{print $1}' || echo 0 du -sk "$path" 2> /dev/null | awk '{print $1}' || echo 0
} }

View File

@@ -34,7 +34,7 @@ _start_sudo_keepalive() {
sleep 30 sleep 30
kill -0 "$$" 2> /dev/null || exit kill -0 "$$" 2> /dev/null || exit
done done
) >/dev/null 2>&1 & ) > /dev/null 2>&1 &
local pid=$! local pid=$!
[[ "${MO_DEBUG:-}" == "1" ]] && echo "DEBUG: _start_sudo_keepalive: background PID = $pid" >&2 [[ "${MO_DEBUG:-}" == "1" ]] && echo "DEBUG: _start_sudo_keepalive: background PID = $pid" >&2

View File

@@ -20,7 +20,10 @@ format_brew_update_label() {
local detail_str="(${total} updates)" local detail_str="(${total} updates)"
if ((${#details[@]} > 0)); then if ((${#details[@]} > 0)); then
detail_str="($(IFS=', '; printf '%s' "${details[*]}"))" detail_str="($(
IFS=', '
printf '%s' "${details[*]}"
))"
fi fi
printf " • Homebrew %s" "$detail_str" printf " • Homebrew %s" "$detail_str"
} }
@@ -271,7 +274,7 @@ perform_updates() {
_perform_appstore_update() { _perform_appstore_update() {
echo -e "${BLUE}Updating App Store apps...${NC}" echo -e "${BLUE}Updating App Store apps...${NC}"
local appstore_log local appstore_log
appstore_log=$(mktemp -t mole-appstore 2>/dev/null || echo "/tmp/mole-appstore.log") appstore_log=$(mktemp -t mole-appstore 2> /dev/null || echo "/tmp/mole-appstore.log")
if [[ "$appstore_needs_fallback" == "true" ]]; then if [[ "$appstore_needs_fallback" == "true" ]]; then
echo -e " ${GRAY}Installing all available updates${NC}" echo -e " ${GRAY}Installing all available updates${NC}"
@@ -293,7 +296,7 @@ _perform_appstore_update() {
echo -e "${RED}${NC} App Store update failed" echo -e "${RED}${NC} App Store update failed"
fi fi
fi fi
rm -f "$appstore_log" 2>/dev/null || true rm -f "$appstore_log" 2> /dev/null || true
reset_softwareupdate_cache reset_softwareupdate_cache
echo "" echo ""
} }
@@ -304,7 +307,7 @@ _perform_macos_update() {
echo -e "${YELLOW}Note:${NC} System update may require restart" echo -e "${YELLOW}Note:${NC} System update may require restart"
local macos_log local macos_log
macos_log=$(mktemp -t mole-macos 2>/dev/null || echo "/tmp/mole-macos.log") macos_log=$(mktemp -t mole-macos 2> /dev/null || echo "/tmp/mole-macos.log")
if [[ "$macos_needs_fallback" == "true" ]]; then if [[ "$macos_needs_fallback" == "true" ]]; then
if sudo softwareupdate -i -r 2>&1 | tee "$macos_log" | grep -v "^$"; then if sudo softwareupdate -i -r 2>&1 | tee "$macos_log" | grep -v "^$"; then
@@ -322,11 +325,11 @@ _perform_macos_update() {
fi fi
fi fi
if grep -qi "restart" "$macos_log" 2>/dev/null; then if grep -qi "restart" "$macos_log" 2> /dev/null; then
echo -e "${YELLOW}${ICON_WARNING}${NC} Restart required to complete update" echo -e "${YELLOW}${ICON_WARNING}${NC} Restart required to complete update"
fi fi
rm -f "$macos_log" 2>/dev/null || true rm -f "$macos_log" 2> /dev/null || true
reset_softwareupdate_cache reset_softwareupdate_cache
echo "" echo ""
} }

View File

@@ -108,9 +108,11 @@ setup() {
run bash --noprofile --norc -c "cd '$PROJECT_ROOT'; printf \$' \\n' | HOME='$HOME' ./mo clean --whitelist" run bash --noprofile --norc -c "cd '$PROJECT_ROOT'; printf \$' \\n' | HOME='$HOME' ./mo clean --whitelist"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
! grep -q "\\.m2/repository" "$whitelist_file" run grep -q "\\.m2/repository" "$whitelist_file"
[ "$status" -eq 1 ]
run bash --noprofile --norc -c "cd '$PROJECT_ROOT'; printf \$'\\n' | HOME='$HOME' ./mo clean --whitelist" run bash --noprofile --norc -c "cd '$PROJECT_ROOT'; printf \$'\\n' | HOME='$HOME' ./mo clean --whitelist"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
! grep -q "\\.m2/repository" "$whitelist_file" run grep -q "\\.m2/repository" "$whitelist_file"
[ "$status" -eq 1 ]
} }