From 84c1f2498b8cc658ba74c1fa4b13ea05b441b5f7 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Wed, 29 Mar 2023 07:36:18 +0000 Subject: [PATCH] implement with self-implemented scanf Signed-off-by: Billy Zha --- cmd/oras/root/login.go | 29 ++++++++++++++++++----------- test/e2e/suite/auth/auth.go | 9 +++++---- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/cmd/oras/root/login.go b/cmd/oras/root/login.go index dba340e57..afabca976 100644 --- a/cmd/oras/root/login.go +++ b/cmd/oras/root/login.go @@ -16,9 +16,9 @@ limitations under the License. package root import ( - "bufio" "errors" "fmt" + "io" "os" "strings" @@ -134,27 +134,34 @@ func runLogin(opts loginOptions) (err error) { func readLine(prompt string, silent bool) (string, error) { fmt.Print(prompt) fd := int(os.Stdin.Fd()) - var line []byte - var err error + line := "" if silent && term.IsTerminal(fd) { - line, err = term.ReadPassword(fd) + bytes, err := term.ReadPassword(fd) if err != nil { return "", err } + line = string(bytes) } else { - reader := bufio.NewReader(os.Stdin) - var part []byte - for more := true; more; { - // read until no more - part, more, err = reader.ReadLine() + // implement per-byte scanf here since fmt.Fscanln skips all the + // newline before scanning + for b := [1]byte{}; ; { + fmt.Print(len(b)) + _, err := os.Stdin.Read(b[:]) if err != nil { + if err == io.EOF { + break + } return "", err } - line = append(line, part...) + s := string(b[:]) + if s == "\n" { + break + } + line += s } } if silent { fmt.Println() } - return string(line), nil + return line, nil } diff --git a/test/e2e/suite/auth/auth.go b/test/e2e/suite/auth/auth.go index af2b91e9c..c15f9c4f1 100644 --- a/test/e2e/suite/auth/auth.go +++ b/test/e2e/suite/auth/auth.go @@ -69,14 +69,14 @@ var _ = Describe("Common registry user", Ordered, func() { It("should fail if no password input", func() { ORAS("login", Host, "--registry-config", AuthConfigPath). WithTimeOut(20*time.Second). - MatchKeyWords("username: ", "password: "). + MatchKeyWords("Username: ", "Password: "). WithInput(strings.NewReader(fmt.Sprintf("%s\n", Username))).ExpectFailure().Exec() }) It("should fail if password is empty", func() { ORAS("login", Host, "--registry-config", AuthConfigPath). WithTimeOut(20*time.Second). - MatchKeyWords("username: ", "password: "). + MatchKeyWords("Username: ", "Password: "). MatchErrKeyWords("Error: password required"). WithInput(strings.NewReader(fmt.Sprintf("%s\n\n", Username))).ExpectFailure().Exec() }) @@ -84,17 +84,18 @@ var _ = Describe("Common registry user", Ordered, func() { It("should fail if no token input", func() { ORAS("login", Host, "--registry-config", AuthConfigPath). WithTimeOut(20*time.Second). - MatchKeyWords("username: ", "token: "). + MatchKeyWords("Username: ", "Token: "). WithInput(strings.NewReader("\n")).ExpectFailure().Exec() }) It("should fail if token is empty", func() { ORAS("login", Host, "--registry-config", AuthConfigPath). WithTimeOut(20*time.Second). - MatchKeyWords("username: ", "token: "). + MatchKeyWords("Username: ", "Token: "). MatchErrKeyWords("Error: token required"). WithInput(strings.NewReader("\n\n")).ExpectFailure().Exec() }) + It("should use prompted input", func() { ORAS("login", Host, "--registry-config", AuthConfigPath). WithTimeOut(20*time.Second).