1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-22 21:55:08 +00:00

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.
This commit is contained in:
Noah Qin
2026-03-05 09:35:26 +08:00
committed by GitHub
parent ff69504f89
commit a9d7c3912f

View File

@@ -108,14 +108,41 @@ request_sudo_access() {
return 0 return 0
fi fi
# Get TTY path # Detect if running in TTY environment
local tty_path="/dev/tty" local tty_path="/dev/tty"
local is_gui_mode=false
if [[ ! -r "$tty_path" || ! -w "$tty_path" ]]; then if [[ ! -r "$tty_path" || ! -w "$tty_path" ]]; then
tty_path=$(tty 2> /dev/null || echo "") tty_path=$(tty 2> /dev/null || echo "")
if [[ -z "$tty_path" || ! -r "$tty_path" || ! -w "$tty_path" ]]; then if [[ -z "$tty_path" || ! -r "$tty_path" || ! -w "$tty_path" ]]; then
log_error "No interactive terminal available" is_gui_mode=true
fi
fi
# GUI mode: use osascript for password dialog
if [[ "$is_gui_mode" == true ]]; then
# Clear sudo cache before attempting authentication
sudo -k 2> /dev/null
# Display native macOS password dialog
local password
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)
if [[ -z "$password" ]]; then
# User cancelled the dialog
unset password
return 1 return 1
fi fi
# Attempt sudo authentication with the provided password
if printf '%s\n' "$password" | sudo -S -p "" -v > /dev/null 2>&1; then
unset password
return 0
fi
# Password was incorrect
unset password
return 1
fi fi
sudo -k sudo -k