Skip to content

Commit

Permalink
implement with self-implemented scanf
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <jinzha1@microsoft.com>
  • Loading branch information
qweeah committed Mar 30, 2023
1 parent d17cdfe commit 84c1f24
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
29 changes: 18 additions & 11 deletions cmd/oras/root/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ limitations under the License.
package root

import (
"bufio"
"errors"
"fmt"
"io"
"os"
"strings"

Expand Down Expand Up @@ -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
}
9 changes: 5 additions & 4 deletions test/e2e/suite/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,33 @@ 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()
})

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).
Expand Down

0 comments on commit 84c1f24

Please sign in to comment.