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() {
120 120
121 anime.Ep.ContinueLast = *continueLast 121 anime.Ep.ContinueLast = *continueLast
122 122
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
123 if *updateScript { 137 if *updateScript {
124 repo := "wraient/curd" 138 repo := "wraient/curd"
125 fileName := "curd" 139 fileName := "curd"
126 140
127 if err := internal.UpdateCurd(repo, fileName); err != nil { 141 if err := internal.UpdateCurd(repo, fileName); err != nil {
128 internal.CurdOut(fmt.Sprintf("Error updating executable: %v\n", err)) 142 fmt.Fprintf(os.Stderr, "Error updating executable: %v\n", err)
129 internal.ExitCurd(err) 143 os.Exit(1)
130 } else {
131 internal.CurdOut("Program Updated!")
132 internal.ExitCurd(nil)
133 } 144 }
145 fmt.Println("Program Updated!")
146 os.Exit(0)
134 } 147 }
135 148
136 if *currentCategory { 149 if *currentCategory {
137 userCurdConfig.CurrentCategory = true 150 userCurdConfig.CurrentCategory = true
138 } 151 }
139 152
140 if *rofiSelection {
141 userCurdConfig.RofiSelection = true
142 }
143
144 if *noRofi || runtime.GOOS == "windows" {
145 userCurdConfig.RofiSelection = false
146 }
147
148 if *imagePreview { 153 if *imagePreview {
149 userCurdConfig.ImagePreview = true 154 userCurdConfig.ImagePreview = true
150 } 155 }
internal/update.go +49 −13
@@ -662,15 +662,35 @@ func isCrossDeviceError(err error) bool {
662 strings.Contains(msg, "invalid cross-device link") 662 strings.Contains(msg, "invalid cross-device link")
663} 663}
664 664
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
665func preferGUIPasswordPrompt() bool { 678func preferGUIPasswordPrompt() bool {
666 if cfg := GetGlobalConfig(); cfg != nil && cfg.RofiSelection { 679 rofi := false
667 return true 680 if cfg := GetGlobalConfig(); cfg != nil {
681 rofi = cfg.RofiSelection
668 } 682 }
669 // No usable TTY (common when launched from a desktop entry / rofi). 683 tty := stdinIsTerminal()
670 if !term.IsTerminal(int(os.Stdin.Fd())) { 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 {
671 return true 690 return true
672 } 691 }
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()
674} 694}
675 695
676// promptSudoPasswordGUI tries GTK/desktop password dialogs (zenity → yad → kdialog). 696// promptSudoPasswordGUI tries GTK/desktop password dialogs (zenity → yad → kdialog).
@@ -724,22 +744,38 @@ func promptSudoPassword(prompt string) (string, error) {
724 prompt = "Administrator password (sudo) to install update: " 744 prompt = "Administrator password (sudo) to install update: "
725 } 745 }
726 746
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.
728 if preferGUIPasswordPrompt() { 754 if preferGUIPasswordPrompt() {
729 if password, err := promptSudoPasswordGUI(prompt); err == nil { 755 if password, err := promptSudoPasswordGUI(prompt); err == nil {
730 return password, nil 756 return password, nil
731 } else { 757 } else {
732 Log(fmt.Sprintf("GUI password prompt unavailable (%v); falling back to terminal", err)) 758 Log(fmt.Sprintf("GUI password prompt unavailable (%v)", err))
733 // If cancel was explicit, don't fall through to a broken TTY prompt in rofi mode. 759 // Explicit cancel in GUI: only abort if we shouldn't fall back to TTY.
734 if strings.Contains(err.Error(), "cancelled") { 760 // In rofi mode with no TTY, cancel means cancel.
735 return "", err 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 }
736 } 771 }
772 // GUI missing/failed → fall through to terminal when possible.
737 } 773 }
738 } 774 }
739 775
740 // Terminal fallback (CLI mode or no GUI tool installed). 776 // Terminal path (CLI `curd -u`, or GUI unavailable).
741 if !term.IsTerminal(int(os.Stdin.Fd())) { 777 if !tty {
742 return "", fmt.Errorf("no terminal available for password entry; install zenity for a GUI prompt") 778 return "", fmt.Errorf("no terminal available for password entry; install zenity/yad/kdialog for a GUI prompt")
743 } 779 }
744 fmt.Fprint(os.Stderr, prompt) 780 fmt.Fprint(os.Stderr, prompt)
745 if !strings.HasSuffix(prompt, " ") { 781 if !strings.HasSuffix(prompt, " ") {
internal/update_test.go +4 −2
@@ -147,9 +147,11 @@ func TestPreferGUIPasswordPromptWithRofi(t *testing.T) {
147 t.Fatal("expected GUI password preference when RofiSelection is on") 147 t.Fatal("expected GUI password preference when RofiSelection is on")
148 } 148 }
149 149
150 // CLI mode (Rofi off): never force GUI just because a display session exists.
150 SetGlobalConfig(&CurdConfig{RofiSelection: false}) 151 SetGlobalConfig(&CurdConfig{RofiSelection: false})
151 // Without rofi, result depends on TTY/DISPLAY — just ensure it doesn't panic. 152 if preferGUIPasswordPrompt() && stdinIsTerminal() {
152 _ = preferGUIPasswordPrompt() 153 t.Fatal("CLI with a TTY should use terminal password, not GUI")
154 }
153} 155}
154 156
155func TestIsCrossDeviceError(t *testing.T) { 157func TestIsCrossDeviceError(t *testing.T) {