Commit c81d45e2c8

c81d45e2c83e7a9828d48c201df8dedbeaf878ec

parent: f635b2346d

Unsigned

Wraient <wraient.911@gmail.com> · 2026-07-22 20:36 UTC

Use GTK password dialog for sudo when Rofi/desktop mode

Prefer zenity (then yad, kdialog) for the update install password prompt
when RofiSelection is on or no TTY is available. Fall back to terminal
ReadPassword in CLI mode.

Layout: unified · split

internal/update.go +79 −1
@@ -442,11 +442,89 @@ func isCrossDeviceError(err error) bool {
442442 strings.Contains(msg, "invalid cross-device link")
443443}
444444
445func preferGUIPasswordPrompt() bool {
446 if cfg := GetGlobalConfig(); cfg != nil && cfg.RofiSelection {
447 return true
448 }
449 // No usable TTY (common when launched from a desktop entry / rofi).
450 if !term.IsTerminal(int(os.Stdin.Fd())) {
451 return true
452 }
453 return os.Getenv("DISPLAY") != "" || os.Getenv("WAYLAND_DISPLAY") != ""
454}
455
456// promptSudoPasswordGUI tries GTK/desktop password dialogs (zenity → yad → kdialog).
457func promptSudoPasswordGUI(prompt string) (string, error) {
458 if prompt == "" {
459 prompt = "Enter your password to install the Curd update:"
460 }
461
462 type dialog struct {
463 name string
464 args []string
465 }
466 dialogs := []dialog{
467 // GTK (GNOME / many desktops)
468 {"zenity", []string{"--password", "--title=Curd Update", "--text=" + prompt}},
469 // GTK-based yad
470 {"yad", []string{"--entry", "--hide-text", "--title=Curd Update", "--text=" + prompt, "--button=OK:0", "--button=Cancel:1"}},
471 // KDE
472 {"kdialog", []string{"--title", "Curd Update", "--password", prompt}},
473 }
474
475 for _, d := range dialogs {
476 bin, err := exec.LookPath(d.name)
477 if err != nil {
478 continue
479 }
480 cmd := exec.Command(bin, d.args...)
481 out, err := cmd.Output()
482 if err != nil {
483 // User cancel or dialog error — try next tool only on "not found"; cancel stops.
484 var exitErr *exec.ExitError
485 if errors.As(err, &exitErr) {
486 // zenity/yad/kdialog: non-zero usually means cancel.
487 return "", fmt.Errorf("password dialog cancelled")
488 }
489 Log(fmt.Sprintf("password dialog %s failed: %v", d.name, err))
490 continue
491 }
492 password := strings.TrimRight(string(out), "\r\n")
493 if password == "" {
494 return "", fmt.Errorf("empty password")
495 }
496 Log(fmt.Sprintf("Collected sudo password via %s dialog", d.name))
497 return password, nil
498 }
499 return "", fmt.Errorf("no GUI password dialog available (tried zenity, yad, kdialog)")
500}
501
445502func promptSudoPassword(prompt string) (string, error) {
446503 if prompt == "" {
447 prompt = "Sudo password: "
504 prompt = "Administrator password (sudo) to install update: "
505 }
506
507 // Rofi / desktop: prefer GTK password dialog so the user isn't dumped to a TTY.
508 if preferGUIPasswordPrompt() {
509 if password, err := promptSudoPasswordGUI(prompt); err == nil {
510 return password, nil
511 } else {
512 Log(fmt.Sprintf("GUI password prompt unavailable (%v); falling back to terminal", err))
513 // If cancel was explicit, don't fall through to a broken TTY prompt in rofi mode.
514 if strings.Contains(err.Error(), "cancelled") {
515 return "", err
516 }
517 }
518 }
519
520 // Terminal fallback (CLI mode or no GUI tool installed).
521 if !term.IsTerminal(int(os.Stdin.Fd())) {
522 return "", fmt.Errorf("no terminal available for password entry; install zenity for a GUI prompt")
448523 }
449524 fmt.Fprint(os.Stderr, prompt)
525 if !strings.HasSuffix(prompt, " ") {
526 fmt.Fprint(os.Stderr, " ")
527 }
450528 password, err := term.ReadPassword(int(os.Stdin.Fd()))
451529 fmt.Fprintln(os.Stderr)
452530 if err != nil {
internal/update_test.go +14
@@ -138,6 +138,20 @@ func TestFormatLocalTimeUsesLocalZone(t *testing.T) {
138138 }
139139}
140140
141func TestPreferGUIPasswordPromptWithRofi(t *testing.T) {
142 prev := GetGlobalConfig()
143 t.Cleanup(func() { SetGlobalConfig(prev) })
144
145 SetGlobalConfig(&CurdConfig{RofiSelection: true})
146 if !preferGUIPasswordPrompt() {
147 t.Fatal("expected GUI password preference when RofiSelection is on")
148 }
149
150 SetGlobalConfig(&CurdConfig{RofiSelection: false})
151 // Without rofi, result depends on TTY/DISPLAY — just ensure it doesn't panic.
152 _ = preferGUIPasswordPrompt()
153}
154
141155func TestIsCrossDeviceError(t *testing.T) {
142156 if !isCrossDeviceError(errors.New("rename /tmp/a /home/b: invalid cross-device link")) {
143157 t.Fatal("expected cross-device detection")