Commit 8dd90ce96d

8dd90ce96d7d82bbf885bbe79735850b90f4f056

parent: 8f901c3635

Unsigned

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

Fix: curd -u asks sudo password in the terminal when not in Rofi

CLI updates no longer open zenity just because DISPLAY is set. GUI
dialogs are used for Rofi/desktop; terminal ReadPassword is used for
interactive shells, with GUI→terminal fallback when dialogs are missing.

Layout: unified · split

cmd/curd/main.go +18 −13
@@ -120,31 +120,36 @@ func main() {
120120
121121 anime.Ep.ContinueLast = *continueLast
122122
123 // Apply UI flags before -u so password prompt mode (terminal vs GTK) is correct.
124 if *rofiSelection {
125 userCurdConfig.RofiSelection = true
126 }
127 if *noRofi || runtime.GOOS == "windows" {
128 userCurdConfig.RofiSelection = false
129 }
130 // `curd -u` is a CLI operation: always use the terminal for sudo when stdin is a TTY,
131 // even if RofiSelection is enabled in the config file.
132 if *updateScript {
133 userCurdConfig.RofiSelection = false
134 }
135 internal.SetGlobalConfig(&userCurdConfig)
136
123137 if *updateScript {
124138 repo := "wraient/curd"
125139 fileName := "curd"
126140
127141 if err := internal.UpdateCurd(repo, fileName); err != nil {
128 internal.CurdOut(fmt.Sprintf("Error updating executable: %v\n", err))
129 internal.ExitCurd(err)
130 } else {
131 internal.CurdOut("Program Updated!")
132 internal.ExitCurd(nil)
142 fmt.Fprintf(os.Stderr, "Error updating executable: %v\n", err)
143 os.Exit(1)
133144 }
145 fmt.Println("Program Updated!")
146 os.Exit(0)
134147 }
135148
136149 if *currentCategory {
137150 userCurdConfig.CurrentCategory = true
138151 }
139152
140 if *rofiSelection {
141 userCurdConfig.RofiSelection = true
142 }
143
144 if *noRofi || runtime.GOOS == "windows" {
145 userCurdConfig.RofiSelection = false
146 }
147
148153 if *imagePreview {
149154 userCurdConfig.ImagePreview = true
150155 }
internal/update.go +49 −13
@@ -662,15 +662,35 @@ func isCrossDeviceError(err error) bool {
662662 strings.Contains(msg, "invalid cross-device link")
663663}
664664
665func hasDisplay() bool {
666 return os.Getenv("DISPLAY") != "" || os.Getenv("WAYLAND_DISPLAY") != ""
667}
668
669func stdinIsTerminal() bool {
670 return term.IsTerminal(int(os.Stdin.Fd()))
671}
672
673// preferGUIPasswordPrompt decides whether to open a desktop password dialog.
674//
675// - Rofi/desktop mode → GUI first (zenity/yad/kdialog)
676// - Plain CLI with a TTY (e.g. `curd -u` in a terminal) → terminal only
677// - No TTY but a display → GUI
665678func preferGUIPasswordPrompt() bool {
666 if cfg := GetGlobalConfig(); cfg != nil && cfg.RofiSelection {
667 return true
679 rofi := false
680 if cfg := GetGlobalConfig(); cfg != nil {
681 rofi = cfg.RofiSelection
668682 }
669 // No usable TTY (common when launched from a desktop entry / rofi).
670 if !term.IsTerminal(int(os.Stdin.Fd())) {
683 tty := stdinIsTerminal()
684
685 // Interactive CLI: never pop a GUI dialog just because DISPLAY is set.
686 if tty && !rofi {
687 return false
688 }
689 if rofi {
671690 return true
672691 }
673 return os.Getenv("DISPLAY") != "" || os.Getenv("WAYLAND_DISPLAY") != ""
692 // Headless of TTY (desktop launcher without a terminal): GUI if available.
693 return !tty && hasDisplay()
674694}
675695
676696// promptSudoPasswordGUI tries GTK/desktop password dialogs (zenity → yad → kdialog).
@@ -724,22 +744,38 @@ func promptSudoPassword(prompt string) (string, error) {
724744 prompt = "Administrator password (sudo) to install update: "
725745 }
726746
727 // Rofi / desktop: prefer GTK password dialog so the user isn't dumped to a TTY.
747 rofi := false
748 if cfg := GetGlobalConfig(); cfg != nil {
749 rofi = cfg.RofiSelection
750 }
751 tty := stdinIsTerminal()
752
753 // Rofi/desktop: try zenity/yad/kdialog first.
728754 if preferGUIPasswordPrompt() {
729755 if password, err := promptSudoPasswordGUI(prompt); err == nil {
730756 return password, nil
731757 } else {
732 Log(fmt.Sprintf("GUI password prompt unavailable (%v); falling back to terminal", err))
733 // If cancel was explicit, don't fall through to a broken TTY prompt in rofi mode.
734 if strings.Contains(err.Error(), "cancelled") {
735 return "", err
758 Log(fmt.Sprintf("GUI password prompt unavailable (%v)", err))
759 // Explicit cancel in GUI: only abort if we shouldn't fall back to TTY.
760 // In rofi mode with no TTY, cancel means cancel.
761 if strings.Contains(err.Error(), "cancelled") && (!tty || rofi) {
762 // If we still have a real terminal under us (rare with rofi), allow TTY
763 // fallback only when not in rofi mode.
764 if !tty {
765 return "", err
766 }
767 if rofi {
768 // Rofi session: user closed the dialog on purpose.
769 return "", err
770 }
736771 }
772 // GUI missing/failed → fall through to terminal when possible.
737773 }
738774 }
739775
740 // Terminal fallback (CLI mode or no GUI tool installed).
741 if !term.IsTerminal(int(os.Stdin.Fd())) {
742 return "", fmt.Errorf("no terminal available for password entry; install zenity for a GUI prompt")
776 // Terminal path (CLI `curd -u`, or GUI unavailable).
777 if !tty {
778 return "", fmt.Errorf("no terminal available for password entry; install zenity/yad/kdialog for a GUI prompt")
743779 }
744780 fmt.Fprint(os.Stderr, prompt)
745781 if !strings.HasSuffix(prompt, " ") {
internal/update_test.go +4 −2
@@ -147,9 +147,11 @@ func TestPreferGUIPasswordPromptWithRofi(t *testing.T) {
147147 t.Fatal("expected GUI password preference when RofiSelection is on")
148148 }
149149
150 // CLI mode (Rofi off): never force GUI just because a display session exists.
150151 SetGlobalConfig(&CurdConfig{RofiSelection: false})
151 // Without rofi, result depends on TTY/DISPLAY — just ensure it doesn't panic.
152 _ = preferGUIPasswordPrompt()
152 if preferGUIPasswordPrompt() && stdinIsTerminal() {
153 t.Fatal("CLI with a TTY should use terminal password, not GUI")
154 }
153155}
154156
155157func TestIsCrossDeviceError(t *testing.T) {