-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathlib.sh
More file actions
106 lines (92 loc) · 3.13 KB
/
lib.sh
File metadata and controls
106 lines (92 loc) · 3.13 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
#!/usr/bin/env bash
set -e
# Color and logging helpers
if [ -t 1 ]; then
BOLD="\033[1m"; RESET="\033[0m"; RED="\033[31m"; GREEN="\033[32m"; YELLOW="\033[33m"; BLUE="\033[34m"
else
BOLD=""; RESET=""; RED=""; GREEN=""; YELLOW=""; BLUE=""
fi
log_info() { echo -e "${BLUE}[*]${RESET} $*"; }
log_ok() { echo -e "${GREEN}[✓]${RESET} $*"; }
log_warn() { echo -e "${YELLOW}[!]${RESET} $*"; }
log_error() { echo -e "${RED}[x]${RESET} $*"; }
log_step() { echo -e "${BOLD}$*${RESET}"; }
# Validation function to check if a path is safe to remove
function validate_path() {
local path="$1"
local description="$2"
# Check if path is empty
if [ -z "$path" ]; then
log_error "Invalid path: empty path for $description"
return 1
fi
# Check if path is a critical system directory
local critical_dirs=("/" "/home" "/usr" "/bin" "/sbin" "/etc" "/var" "/opt" "/root")
for critical in "${critical_dirs[@]}"; do
if [ "$path" = "$critical" ]; then
log_error "Refusing to remove critical system directory: $path"
return 1
fi
done
# Check if path is user's home directory
if [ "$path" = "$HOME" ]; then
log_error "Refusing to remove home directory: $path"
return 1
fi
# Check if path exists
if [ ! -e "$path" ]; then
log_warn "$description not found: $path"
return 2 # Non-fatal: path doesn't exist
fi
# Verify path is within expected locations for Cursor files
case "$path" in
"$HOME/.local/bin/"*|"$HOME/.local/share/"*|"$HOME/.config/"*|"$HOME/AppImages/"*|"$HOME/Applications/"*|"$HOME/.cursor"*)
return 0 # Valid path
;;
*)
log_warn "Path outside expected locations: $path"
read -r -p "Are you sure you want to remove this? (y/N) " confirm
if [[ ! $confirm =~ ^[Yy]$ ]]; then
log_info "Skipping removal of $path"
return 1
fi
return 0
;;
esac
}
# Safe removal function
function safe_remove() {
local path="$1"
local description="$2"
local recursive="${3:-false}"
local ret=0
validate_path "$path" "$description" || ret=$?
if [ $ret -eq 2 ]; then
return 0 # Path doesn't exist, continue
elif [ $ret -ne 0 ]; then
return 1 # Validation failed
fi
if [ "$recursive" = "true" ]; then
log_info "Removing $description: $path"
rm -rf "$path"
else
log_info "Removing $description: $path"
rm -f "$path"
fi
return 0
}
# Find the installed Cursor AppImage path (if any)
function find_cursor_appimage() {
local search_dirs=("$HOME/.local/bin" "$HOME/AppImages" "$HOME/Applications")
for dir in "${search_dirs[@]}"; do
# Skip non-existent directories to avoid 'find' non-zero status with set -e
[ -d "$dir" ] || continue
local appimage
appimage=$(find "$dir" -name "cursor.appimage" -print -quit 2>/dev/null || true)
if [ -n "$appimage" ]; then
echo "$appimage"
return 0
fi
done
return 1
}