Skip to content

Commit a9d7c39

Browse files
authored
fix(sudo): add GUI mode support for non-TTY environments (#536)
When Mole is called from GUI applications (e.g., SwiftUI apps), the request_sudo_access() function would fail with '/dev/tty: Device not configured' error because /dev/tty is not available in non-TTY contexts. This commit adds automatic detection of the runtime environment and uses macOS native password dialog (via osascript) when running in GUI mode, while preserving all existing TTY behavior including Touch ID support. Changes: - Detect TTY availability before attempting terminal-based authentication - Use osascript to display native password dialog in GUI mode - Maintain backward compatibility with all terminal-based workflows - Ensure secure password handling (unset after use) Fixes commands like 'mole clean', 'mole optimize', 'mole purge' when invoked from GUI applications.
1 parent ff69504 commit a9d7c39

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

lib/core/sudo.sh

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,41 @@ request_sudo_access() {
108108
return 0
109109
fi
110110

111-
# Get TTY path
111+
# Detect if running in TTY environment
112112
local tty_path="/dev/tty"
113+
local is_gui_mode=false
114+
113115
if [[ ! -r "$tty_path" || ! -w "$tty_path" ]]; then
114116
tty_path=$(tty 2> /dev/null || echo "")
115117
if [[ -z "$tty_path" || ! -r "$tty_path" || ! -w "$tty_path" ]]; then
116-
log_error "No interactive terminal available"
118+
is_gui_mode=true
119+
fi
120+
fi
121+
122+
# GUI mode: use osascript for password dialog
123+
if [[ "$is_gui_mode" == true ]]; then
124+
# Clear sudo cache before attempting authentication
125+
sudo -k 2> /dev/null
126+
127+
# Display native macOS password dialog
128+
local password
129+
password=$(osascript -e "display dialog \"$prompt_msg\" default answer \"\" with title \"Mole\" with icon caution with hidden answer" -e 'text returned of result' 2> /dev/null)
130+
131+
if [[ -z "$password" ]]; then
132+
# User cancelled the dialog
133+
unset password
117134
return 1
118135
fi
136+
137+
# Attempt sudo authentication with the provided password
138+
if printf '%s\n' "$password" | sudo -S -p "" -v > /dev/null 2>&1; then
139+
unset password
140+
return 0
141+
fi
142+
143+
# Password was incorrect
144+
unset password
145+
return 1
119146
fi
120147

121148
sudo -k

0 commit comments

Comments
 (0)