forked from TrueCloudLab/restic
Merge pull request #341 from restic/read-password-from-stdin
Read password from stdin if terminal is not a tty
This commit is contained in:
commit
8de8ca05f1
1 changed files with 42 additions and 5 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/jessevdk/go-flags"
|
"github.com/jessevdk/go-flags"
|
||||||
"github.com/restic/restic/backend"
|
"github.com/restic/restic/backend"
|
||||||
|
@ -77,19 +78,55 @@ func (o GlobalOptions) Exitf(exitcode int, format string, args ...interface{}) {
|
||||||
os.Exit(exitcode)
|
os.Exit(exitcode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// readPassword reads the password from the given reader directly.
|
||||||
|
func readPassword(in io.Reader) (password string, err error) {
|
||||||
|
buf := make([]byte, 1000)
|
||||||
|
n, err := io.ReadFull(in, buf)
|
||||||
|
buf = buf[:n]
|
||||||
|
|
||||||
|
if err != nil && err != io.ErrUnexpectedEOF {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.TrimRight(string(buf), "\r\n"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// readPasswordTerminal reads the password from the given reader which must be a
|
||||||
|
// tty. Prompt is printed on the writer out before attempting to read the
|
||||||
|
// password.
|
||||||
|
func readPasswordTerminal(in *os.File, out io.Writer, prompt string) (password string, err error) {
|
||||||
|
fmt.Fprint(out, prompt)
|
||||||
|
buf, err := terminal.ReadPassword(int(in.Fd()))
|
||||||
|
fmt.Fprintln(out)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
password = string(buf)
|
||||||
|
return password, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (o GlobalOptions) ReadPassword(prompt string) string {
|
func (o GlobalOptions) ReadPassword(prompt string) string {
|
||||||
fmt.Fprint(os.Stderr, prompt)
|
var (
|
||||||
pw, err := terminal.ReadPassword(int(os.Stdin.Fd()))
|
password string
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
if terminal.IsTerminal(int(os.Stdin.Fd())) {
|
||||||
|
password, err = readPasswordTerminal(os.Stdin, os.Stderr, prompt)
|
||||||
|
} else {
|
||||||
|
password, err = readPassword(os.Stdin)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
o.Exitf(2, "unable to read password: %v", err)
|
o.Exitf(2, "unable to read password: %v", err)
|
||||||
}
|
}
|
||||||
fmt.Fprintln(os.Stderr)
|
|
||||||
|
|
||||||
if len(pw) == 0 {
|
if len(password) == 0 {
|
||||||
o.Exitf(1, "an empty password is not a password")
|
o.Exitf(1, "an empty password is not a password")
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(pw)
|
return password
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o GlobalOptions) ReadPasswordTwice(prompt1, prompt2 string) string {
|
func (o GlobalOptions) ReadPasswordTwice(prompt1, prompt2 string) string {
|
||||||
|
|
Loading…
Reference in a new issue