Commit c81d45e2c8
Unsigned
Layout: unified · split
internal/update.go +79 −1
| @@ -442,11 +442,89 @@ func isCrossDeviceError(err error) bool { | |||
| 442 | strings.Contains(msg, "invalid cross-device link") | 442 | strings.Contains(msg, "invalid cross-device link") |
| 443 | } | 443 | } |
| 444 | 444 | ||
| 445 | func 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). | ||
| 457 | func 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 | |||
| 445 | func promptSudoPassword(prompt string) (string, error) { | 502 | func promptSudoPassword(prompt string) (string, error) { |
| 446 | if prompt == "" { | 503 | 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") | ||
| 448 | } | 523 | } |
| 449 | fmt.Fprint(os.Stderr, prompt) | 524 | fmt.Fprint(os.Stderr, prompt) |
| 525 | if !strings.HasSuffix(prompt, " ") { | ||
| 526 | fmt.Fprint(os.Stderr, " ") | ||
| 527 | } | ||
| 450 | password, err := term.ReadPassword(int(os.Stdin.Fd())) | 528 | password, err := term.ReadPassword(int(os.Stdin.Fd())) |
| 451 | fmt.Fprintln(os.Stderr) | 529 | fmt.Fprintln(os.Stderr) |
| 452 | if err != nil { | 530 | if err != nil { |
internal/update_test.go +14
| @@ -138,6 +138,20 @@ func TestFormatLocalTimeUsesLocalZone(t *testing.T) { | |||
| 138 | } | 138 | } |
| 139 | } | 139 | } |
| 140 | 140 | ||
| 141 | func 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 | |||
| 141 | func TestIsCrossDeviceError(t *testing.T) { | 155 | func TestIsCrossDeviceError(t *testing.T) { |
| 142 | if !isCrossDeviceError(errors.New("rename /tmp/a /home/b: invalid cross-device link")) { | 156 | if !isCrossDeviceError(errors.New("rename /tmp/a /home/b: invalid cross-device link")) { |
| 143 | t.Fatal("expected cross-device detection") | 157 | t.Fatal("expected cross-device detection") |