Merge pull request #132 from docker/login-prompt

Prompt if username is empty on login
This commit is contained in:
Chris Crone 2020-11-20 15:20:43 +01:00 коммит произвёл GitHub
Родитель cfe11a4a1d 932cb83f18
Коммит 5c39f4672e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 33 добавлений и 19 удалений

Просмотреть файл

@ -36,15 +36,19 @@ const (
func newLoginCmd(streams command.Streams, store credentials.Store, hubClient *hub.Client) *cobra.Command {
cmd := &cobra.Command{
Use: loginName + " USERNAME",
Use: loginName + " [USERNAME]",
Short: "Login to the Hub",
Args: cli.ExactArgs(1),
Args: cli.RequiresMaxArgs(1),
DisableFlagsInUseLine: true,
PreRun: func(cmd *cobra.Command, args []string) {
metrics.Send("root", loginName)
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := login.RunLogin(cmd.Context(), streams, hubClient, store, args[0]); err != nil {
username := ""
if len(args) > 0 {
username = args[0]
}
if err := login.RunLogin(cmd.Context(), streams, hubClient, store, username); err != nil {
return err
}
fmt.Fprintln(streams.Out(), ansi.Info("Login Succeeded"))

Просмотреть файл

@ -36,7 +36,14 @@ import (
)
// RunLogin logs the user and asks for the 2FA code if needed
func RunLogin(ctx context.Context, streams command.Streams, hubClient *hub.Client, store credentials.Store, username string) error {
func RunLogin(ctx context.Context, streams command.Streams, hubClient *hub.Client, store credentials.Store, candidateUsername string) error {
username := candidateUsername
if username == "" {
var err error
if username, err = readClearText(ctx, streams, "Username: "); err != nil {
return err
}
}
password, err := readPassword(streams)
if err != nil {
return err
@ -62,24 +69,27 @@ func RunLogin(ctx context.Context, streams command.Streams, hubClient *hub.Clien
// VerifyTwoFactorCode run 2FA login
func VerifyTwoFactorCode(ctx context.Context, streams command.Streams, hubClient *hub.Client, username string, password string) (string, string, error) {
return hubClient.Login(username, password, func() (string, error) {
userIn := make(chan string, 1)
go func() {
fmt.Fprint(streams.Out(), ansi.Info("2FA required, please provide the 6 digit code: "))
reader := bufio.NewReader(streams.In())
input, _ := reader.ReadString('\n')
userIn <- strings.TrimSpace(input)
}()
input := ""
select {
case <-ctx.Done():
return "", errors.New("canceled")
case input = <-userIn:
}
return input, nil
return readClearText(ctx, streams, "2FA required, please provide the 6 digit code: ")
})
}
func readClearText(ctx context.Context, streams command.Streams, prompt string) (string, error) {
userIn := make(chan string, 1)
go func() {
fmt.Fprint(streams.Out(), ansi.Info(prompt))
reader := bufio.NewReader(streams.In())
input, _ := reader.ReadString('\n')
userIn <- strings.TrimSpace(input)
}()
input := ""
select {
case <-ctx.Done():
return "", errors.New("canceled")
case input = <-userIn:
}
return input, nil
}
func readPassword(streams command.Streams) (string, error) {
in := streams.In()
// On Windows, force the use of the regular OS stdin stream. Fixes #14336/#14210