diff options
Diffstat (limited to 'internal')
-rw-r--r-- | internal/readpassword/extpass_test.go | 4 | ||||
-rw-r--r-- | internal/readpassword/read.go | 19 | ||||
-rw-r--r-- | internal/readpassword/stdin_test.go | 6 |
3 files changed, 16 insertions, 13 deletions
diff --git a/internal/readpassword/extpass_test.go b/internal/readpassword/extpass_test.go index cdfea4e..b35153f 100644 --- a/internal/readpassword/extpass_test.go +++ b/internal/readpassword/extpass_test.go @@ -26,7 +26,7 @@ func TestExtpass(t *testing.T) { func TestOnceExtpass(t *testing.T) { p1 := "lkadsf0923rdfi48rqwhdsf" - p2 := string(Once("echo " + p1)) + p2 := string(Once("echo "+p1, "")) if p1 != p2 { t.Errorf("p1=%q != p2=%q", p1, p2) } @@ -34,7 +34,7 @@ func TestOnceExtpass(t *testing.T) { func TestTwiceExtpass(t *testing.T) { p1 := "w5w44t3wfe45srz434" - p2 := string(Once("echo " + p1)) + p2 := string(Once("echo "+p1, "")) if p1 != p2 { t.Errorf("p1=%q != p2=%q", p1, p2) } diff --git a/internal/readpassword/read.go b/internal/readpassword/read.go index e2fce8a..c99be5d 100644 --- a/internal/readpassword/read.go +++ b/internal/readpassword/read.go @@ -23,15 +23,18 @@ const ( ) // Once tries to get a password from the user, either from the terminal, extpass -// or stdin. -func Once(extpass string) []byte { +// or stdin. Leave "prompt" empty to use the default "Password: " prompt. +func Once(extpass string, prompt string) []byte { if extpass != "" { return readPasswordExtpass(extpass) } + if prompt == "" { + prompt = "Password" + } if !terminal.IsTerminal(int(os.Stdin.Fd())) { - return readPasswordStdin() + return readPasswordStdin(prompt) } - return readPasswordTerminal("Password: ") + return readPasswordTerminal(prompt + ": ") } // Twice is the same as Once but will prompt twice if we get the password from @@ -41,7 +44,7 @@ func Twice(extpass string) []byte { return readPasswordExtpass(extpass) } if !terminal.IsTerminal(int(os.Stdin.Fd())) { - return readPasswordStdin() + return readPasswordStdin("Password") } p1 := readPasswordTerminal("Password: ") p2 := readPasswordTerminal("Repeat: ") @@ -77,11 +80,11 @@ func readPasswordTerminal(prompt string) []byte { // readPasswordStdin reads a line from stdin. // It exits with a fatal error on read error or empty result. -func readPasswordStdin() []byte { - tlog.Info.Println("Reading password from stdin") +func readPasswordStdin(prompt string) []byte { + tlog.Info.Printf("Reading %s from stdin", prompt) p := readLineUnbuffered(os.Stdin) if len(p) == 0 { - tlog.Fatal.Println("Got empty password from stdin") + tlog.Fatal.Printf("Got empty %s from stdin", prompt) os.Exit(exitcodes.ReadPassword) } return p diff --git a/internal/readpassword/stdin_test.go b/internal/readpassword/stdin_test.go index 8cf9954..01dd701 100644 --- a/internal/readpassword/stdin_test.go +++ b/internal/readpassword/stdin_test.go @@ -11,7 +11,7 @@ import ( func TestStdin(t *testing.T) { p1 := "g55434t55wef" if os.Getenv("TEST_SLAVE") == "1" { - p2 := string(readPasswordStdin()) + p2 := string(readPasswordStdin("foo")) if p1 != p2 { fmt.Fprintf(os.Stderr, "%q != %q", p1, p2) os.Exit(1) @@ -44,7 +44,7 @@ func TestStdin(t *testing.T) { func TestStdinEof(t *testing.T) { p1 := "asd45as5f4a36" if os.Getenv("TEST_SLAVE") == "1" { - p2 := string(readPasswordStdin()) + p2 := string(readPasswordStdin("foo")) if p1 != p2 { fmt.Fprintf(os.Stderr, "%q != %q", p1, p2) os.Exit(1) @@ -76,7 +76,7 @@ func TestStdinEof(t *testing.T) { // Provide empty password via stdin func TestStdinEmpty(t *testing.T) { if os.Getenv("TEST_SLAVE") == "1" { - readPasswordStdin() + readPasswordStdin("foo") } cmd := exec.Command(os.Args[0], "-test.run=TestStdinEmpty$") cmd.Env = append(os.Environ(), "TEST_SLAVE=1") |