mirror of https://github.com/docker/cli.git
cli/command: ConfigureAuth: fix terminal state not being restored on error
ConfigureAuth used the readInput() utility to read the username and password. However, this utility did not return errors it encountered, but instead did an os.Exit(1). A result of this was that the terminal was not restored if an error happened. When reading the password, the terminal is configured to disable echo (i.e. characters are not printed), and failing to restore the previous state means that the terminal is now "non-functional". This patch: - changes readInput() to return errors it encounters - uses a defer() to restore terminal state Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
b799ab94b8
commit
d0ec8fa5cf
|
@ -113,7 +113,11 @@ func ConfigureAuth(cli Cli, flUser, flPassword string, authconfig *registrytypes
|
||||||
fmt.Fprintln(cli.Out(), "Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.")
|
fmt.Fprintln(cli.Out(), "Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.")
|
||||||
}
|
}
|
||||||
promptWithDefault(cli.Out(), "Username", authconfig.Username)
|
promptWithDefault(cli.Out(), "Username", authconfig.Username)
|
||||||
flUser = readInput(cli.In(), cli.Out())
|
var err error
|
||||||
|
flUser, err = readInput(cli.In())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
flUser = strings.TrimSpace(flUser)
|
flUser = strings.TrimSpace(flUser)
|
||||||
if flUser == "" {
|
if flUser == "" {
|
||||||
flUser = authconfig.Username
|
flUser = authconfig.Username
|
||||||
|
@ -128,12 +132,15 @@ func ConfigureAuth(cli Cli, flUser, flPassword string, authconfig *registrytypes
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Fprintf(cli.Out(), "Password: ")
|
fmt.Fprintf(cli.Out(), "Password: ")
|
||||||
term.DisableEcho(cli.In().FD(), oldState)
|
_ = term.DisableEcho(cli.In().FD(), oldState)
|
||||||
|
defer func() {
|
||||||
flPassword = readInput(cli.In(), cli.Out())
|
_ = term.RestoreTerminal(cli.In().FD(), oldState)
|
||||||
|
}()
|
||||||
|
flPassword, err = readInput(cli.In())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
fmt.Fprint(cli.Out(), "\n")
|
fmt.Fprint(cli.Out(), "\n")
|
||||||
|
|
||||||
term.RestoreTerminal(cli.In().FD(), oldState)
|
|
||||||
if flPassword == "" {
|
if flPassword == "" {
|
||||||
return errors.Errorf("Error: Password Required")
|
return errors.Errorf("Error: Password Required")
|
||||||
}
|
}
|
||||||
|
@ -145,14 +152,14 @@ func ConfigureAuth(cli Cli, flUser, flPassword string, authconfig *registrytypes
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readInput(in io.Reader, out io.Writer) string {
|
// readInput reads, and returns user input from in. It tries to return a
|
||||||
reader := bufio.NewReader(in)
|
// single line, not including the end-of-line bytes.
|
||||||
line, _, err := reader.ReadLine()
|
func readInput(in io.Reader) (string, error) {
|
||||||
|
line, _, err := bufio.NewReader(in).ReadLine()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(out, err.Error())
|
return "", errors.Wrap(err, "error while reading input")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
return string(line)
|
return string(line), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func promptWithDefault(out io.Writer, prompt string, configDefault string) {
|
func promptWithDefault(out io.Writer, prompt string, configDefault string) {
|
||||||
|
|
Loading…
Reference in New Issue