log: password prompt output fixed for unix - partially fixes #2220

This commit is contained in:
Filip Bartodziej 2018-06-10 00:27:42 +02:00 committed by Nick Craig-Wood
parent d7ac4ca44e
commit ce109ed9c0
2 changed files with 13 additions and 3 deletions

View File

@ -74,6 +74,9 @@ var (
// Key to use for password en/decryption. // Key to use for password en/decryption.
// When nil, no encryption will be used for saving. // When nil, no encryption will be used for saving.
configKey []byte configKey []byte
// output of prompt for password
PasswordPromptOutput = os.Stderr
) )
func init() { func init() {
@ -287,9 +290,9 @@ func checkPassword(password string) (string, error) {
// GetPassword asks the user for a password with the prompt given. // GetPassword asks the user for a password with the prompt given.
func GetPassword(prompt string) string { func GetPassword(prompt string) string {
_, _ = fmt.Fprintln(os.Stderr, prompt) _, _ = fmt.Fprintln(PasswordPromptOutput, prompt)
for { for {
_, _ = fmt.Fprint(os.Stderr, "password:") _, _ = fmt.Fprint(PasswordPromptOutput, "password:")
password := ReadPassword() password := ReadPassword()
password, err := checkPassword(password) password, err := checkPassword(password)
if err == nil { if err == nil {

View File

@ -8,12 +8,19 @@ import (
"log" "log"
"os" "os"
"github.com/ncw/rclone/fs/config"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
// redirectStderr to the file passed in // redirectStderr to the file passed in
func redirectStderr(f *os.File) { func redirectStderr(f *os.File) {
err := unix.Dup2(int(f.Fd()), int(os.Stderr.Fd())) passPromptFd, err := unix.Dup(int(os.Stderr.Fd()))
if err != nil {
log.Fatalf("Failed to duplicate stderr: %v", err)
}
config.PasswordPromptOutput = os.NewFile(uintptr(passPromptFd), "passPrompt")
err = unix.Dup2(int(f.Fd()), int(os.Stderr.Fd()))
if err != nil { if err != nil {
log.Fatalf("Failed to redirect stderr to file: %v", err) log.Fatalf("Failed to redirect stderr to file: %v", err)
} }