This commit is contained in:
Yasuhiro Matsumoto 2017-04-14 20:05:18 +09:00
parent 47ae1f78c2
commit d57df64d26
2 changed files with 37 additions and 3 deletions

View File

@ -63,6 +63,17 @@ func textContent(s string) string {
return buf.String() return buf.String()
} }
var (
readUsername func() (string, error) = func() (string, error) {
b, _, err := bufio.NewReader(os.Stdin).ReadLine()
if err != nil {
return "", err
}
return string(b), nil
}
readPassword func() (string, error)
)
func prompt() (string, string, error) { func prompt() (string, string, error) {
t, err := tty.Open() t, err := tty.Open()
if err != nil { if err != nil {
@ -71,14 +82,18 @@ func prompt() (string, string, error) {
defer t.Close() defer t.Close()
fmt.Print("E-Mail: ") fmt.Print("E-Mail: ")
b, _, err := bufio.NewReader(os.Stdin).ReadLine() email, err := readUsername()
if err != nil { if err != nil {
return "", "", err return "", "", err
} }
email := string(b)
fmt.Print("Password: ") fmt.Print("Password: ")
password, err := t.ReadPassword() var password string
if readPassword == nil {
password, err = t.ReadPassword()
} else {
password, err = readPassword()
}
if err != nil { if err != nil {
return "", "", err return "", "", err
} }

View File

@ -108,3 +108,22 @@ func TestGetConfig(t *testing.T) {
t.Fatalf("want %q but %q", "foo", got) t.Fatalf("want %q but %q", "foo", got)
} }
} }
func TestPrompt(t *testing.T) {
readUsername = func() (string, error) {
return "foo", nil
}
readPassword = func() (string, error) {
return "bar", nil
}
username, password, err := prompt()
if err != nil {
t.Fatal(err)
}
if username != "foo" {
t.Fatalf("want %q but %q", "foo", username)
}
if password != "bar" {
t.Fatalf("want %q but %q", "bar", password)
}
}