termstatus: fully wrap reading password from terminal

This commit is contained in:
Michael Eischer
2025-10-03 18:55:46 +02:00
parent 013c565c29
commit ff5a0cc851
7 changed files with 56 additions and 44 deletions
+2 -23
View File
@@ -1,7 +1,6 @@
package main
import (
"bufio"
"context"
"fmt"
"io"
@@ -32,7 +31,6 @@ import (
"github.com/restic/restic/internal/options"
"github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/terminal"
"github.com/restic/restic/internal/textfile"
"github.com/restic/restic/internal/ui"
"github.com/restic/restic/internal/ui/progress"
@@ -232,14 +230,6 @@ func loadPasswordFromFile(pwdFile string) (string, error) {
return strings.TrimSpace(string(s)), errors.Wrap(err, "Readfile")
}
// readPassword reads the password from the given reader directly.
func readPassword(in io.Reader) (password string, err error) {
sc := bufio.NewScanner(in)
sc.Scan()
return sc.Text(), errors.WithStack(sc.Err())
}
// ReadPassword reads the password from a password file, the environment
// variable RESTIC_PASSWORD or prompts the user. If the context is canceled,
// the function leaks the password reading goroutine.
@@ -255,20 +245,9 @@ func ReadPassword(ctx context.Context, gopts GlobalOptions, prompt string, print
return gopts.password, nil
}
var (
password string
err error
)
if gopts.term.InputIsTerminal() {
password, err = terminal.ReadPassword(ctx, os.Stdin, os.Stderr, prompt)
} else {
printer.PT("reading repository password from stdin")
password, err = readPassword(os.Stdin)
}
password, err := gopts.term.ReadPassword(ctx, prompt)
if err != nil {
return "", errors.Wrap(err, "unable to read password")
return "", fmt.Errorf("unable to read password: %w", err)
}
if len(password) == 0 {
-11
View File
@@ -7,21 +7,10 @@ import (
"strings"
"testing"
"github.com/restic/restic/internal/errors"
rtest "github.com/restic/restic/internal/test"
"github.com/restic/restic/internal/ui/progress"
)
type errorReader struct{ err error }
func (r *errorReader) Read([]byte) (int, error) { return 0, r.err }
func TestReadPassword(t *testing.T) {
want := errors.New("foo")
_, err := readPassword(&errorReader{want})
rtest.Assert(t, errors.Is(err, want), "wrong error %v", err)
}
func TestReadRepo(t *testing.T) {
tempDir := rtest.TempDir(t)