Skip to content

Commit 110399a

Browse files
committed
Add workaround for no password echo
This commit restores the terminal state in case the program is interrupted while being in password read mode. This ensures the terminal remains usable, also if the password input is being cancelled. Signed-off-by: Felix Niederwanger <[email protected]>
1 parent 17f7e40 commit 110399a

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

pkg/auth/auth.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"net/url"
99
"os"
10+
"os/signal"
1011
"path/filepath"
1112
"strings"
1213

@@ -244,7 +245,26 @@ func getUserAndPass(opts *LoginOptions, password, userFromAuthFile string) (user
244245
}
245246
if password == "" {
246247
fmt.Fprint(opts.Stdout, "Password: ")
247-
pass, err := terminal.ReadPassword(int(os.Stdin.Fd()))
248+
// Store and restore the terminal status on interruptions to
249+
// avoid that the terminal remains in the password state
250+
// This is necessary as a workaround for https://github.com/golang/go/issues/31180
251+
stdin := int(os.Stdin.Fd())
252+
oldState, err := terminal.GetState(stdin)
253+
if err != nil {
254+
oldState = nil
255+
}
256+
sigch := make(chan os.Signal, 1)
257+
signal.Notify(sigch, os.Interrupt)
258+
go func() {
259+
for _ = range sigch {
260+
if oldState != nil {
261+
terminal.Restore(stdin, oldState)
262+
}
263+
os.Exit(1)
264+
}
265+
}()
266+
pass, err := terminal.ReadPassword(stdin)
267+
signal.Reset(os.Interrupt)
248268
if err != nil {
249269
return "", "", fmt.Errorf("reading password: %w", err)
250270
}

0 commit comments

Comments
 (0)