1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-06 15:27:58 +00:00

optimize code structure and reduce duplication

This commit is contained in:
Tw93
2025-12-02 15:24:19 +08:00
parent 2ad69b3845
commit bb50a345b6
21 changed files with 421 additions and 409 deletions

View File

@@ -106,7 +106,7 @@ clean_media_players() {
has_offline_music=true
elif [[ -d "$spotify_cache" ]]; then
local cache_size_kb
cache_size_kb=$(du -sk "$spotify_cache" 2> /dev/null | awk '{print $1}' || echo "0")
cache_size_kb=$(get_path_size_kb "$spotify_cache")
# Large cache (>500MB) likely contains offline music
if [[ $cache_size_kb -ge 512000 ]]; then
has_offline_music=true

View File

@@ -232,7 +232,7 @@ clean_orphaned_app_data() {
if is_orphaned "$bundle_id" "$match"; then
# Use timeout to prevent du from hanging on large/problematic directories
local size_kb
size_kb=$(run_with_timeout 2 du -sk "$match" 2> /dev/null | awk '{print $1}' || echo "0")
size_kb=$(run_with_timeout 2 get_path_size_kb "$match")
if [[ -z "$size_kb" || "$size_kb" == "0" ]]; then
continue
fi

View File

@@ -75,7 +75,7 @@ clean_service_worker_cache() {
# Pattern matches: letters/numbers, hyphens, then dot, then TLD
# Example: "abc123_https_example.com_0" → "example.com"
local domain=$(basename "$cache_dir" | grep -oE '[a-zA-Z0-9][-a-zA-Z0-9]*\.[a-zA-Z]{2,}' | head -1 || echo "")
local size=$(du -sk "$cache_dir" 2> /dev/null | awk '{print $1}')
local size=$(get_path_size_kb "$cache_dir")
# Check if domain is protected
local is_protected=false

View File

@@ -40,7 +40,7 @@ clean_broken_preferences() {
# Validate plist using plutil
if ! plutil -lint "$plist_file" > /dev/null 2>&1; then
local size_kb
size_kb=$(du -sk "$plist_file" 2> /dev/null | awk '{print $1}' || echo "0")
size_kb=$(get_path_size_kb "$plist_file")
if [[ "$DRY_RUN" != "true" ]]; then
rm -f "$plist_file" 2> /dev/null || true
@@ -67,7 +67,7 @@ clean_broken_preferences() {
if ! plutil -lint "$plist_file" > /dev/null 2>&1; then
local size_kb
size_kb=$(du -sk "$plist_file" 2> /dev/null | awk '{print $1}' || echo "0")
size_kb=$(get_path_size_kb "$plist_file")
if [[ "$DRY_RUN" != "true" ]]; then
rm -f "$plist_file" 2> /dev/null || true
@@ -143,7 +143,7 @@ clean_broken_login_items() {
# Program doesn't exist - this is a broken login item
local size_kb
size_kb=$(du -sk "$plist_file" 2> /dev/null | awk '{print $1}' || echo "0")
size_kb=$(get_path_size_kb "$plist_file")
if [[ "$DRY_RUN" != "true" ]]; then
# Unload first if loaded

View File

@@ -123,7 +123,7 @@ clean_time_machine_failed_backups() {
continue
fi
local size_kb=$(du -sk "$inprogress_file" 2> /dev/null | awk '{print $1}' || echo "0")
local size_kb=$(get_path_size_kb "$inprogress_file")
if [[ "$size_kb" -gt 0 ]]; then
local backup_name=$(basename "$inprogress_file")
@@ -175,7 +175,7 @@ clean_time_machine_failed_backups() {
continue
fi
local size_kb=$(du -sk "$inprogress_file" 2> /dev/null | awk '{print $1}' || echo "0")
local size_kb=$(get_path_size_kb "$inprogress_file")
if [[ "$size_kb" -gt 0 ]]; then
local backup_name=$(basename "$inprogress_file")

View File

@@ -273,7 +273,7 @@ clean_application_support_logs() {
check_ios_device_backups() {
local backup_dir="$HOME/Library/Application Support/MobileSync/Backup"
if [[ -d "$backup_dir" ]] && find "$backup_dir" -mindepth 1 -maxdepth 1 | read -r _; then
local backup_kb=$(du -sk "$backup_dir" 2> /dev/null | awk '{print $1}')
local backup_kb=$(get_path_size_kb "$backup_dir")
if [[ -n "${backup_kb:-}" && "$backup_kb" -gt 102400 ]]; then
local backup_human=$(du -sh "$backup_dir" 2> /dev/null | awk '{print $1}')
note_activity