Print password prompts to stderr

This makes rclone with encrypted config better suited for use in
pipelines. E.g.:

$ rclone lsl mydrive:Some/Dir | sort -k 4

If the password prompt ("Enter configuration password") is printed to
stdout, it will be swallowed by sort. By printing it to stderr, you
still see the prompt, without sacrificing compatibility with the unix
pipeline.
This commit is contained in:
Hraban Luyat 2017-04-15 18:17:53 +01:00 committed by Nick Craig-Wood
parent 82b8d68ffb
commit 66e8c1600e
2 changed files with 5 additions and 5 deletions

View File

@ -516,15 +516,15 @@ func checkPassword(password string) (string, error) {
// GetPassword asks the user for a password with the prompt given.
func GetPassword(prompt string) string {
fmt.Println(prompt)
fmt.Fprintln(os.Stderr, prompt)
for {
fmt.Print("password:")
fmt.Fprint(os.Stderr, "password:")
password := ReadPassword()
password, err := checkPassword(password)
if err == nil {
return password
}
fmt.Printf("Bad password: %v\n", err)
fmt.Fprintf(os.Stderr, "Bad password: %v\n", err)
}
}
@ -553,7 +553,7 @@ func getConfigPassword(q string) {
if err == nil {
return
}
fmt.Println("Error:", err)
fmt.Fprintln(os.Stderr, "Error:", err)
}
}

View File

@ -18,7 +18,7 @@ import (
// ReadPassword reads a password without echoing it to the terminal.
func ReadPassword() string {
line, err := terminal.ReadPassword(int(os.Stdin.Fd()))
fmt.Println("")
fmt.Fprintln(os.Stderr)
if err != nil {
log.Fatalf("Failed to read password: %v", err)
}