11package command
22
33import (
4- "bufio"
54 "context"
65 "fmt"
7- "io"
86 "os"
97 "runtime"
108 "strings"
@@ -17,7 +15,6 @@ import (
1715 "github.com/docker/docker/api/types"
1816 registrytypes "github.com/docker/docker/api/types/registry"
1917 "github.com/docker/docker/registry"
20- "github.com/moby/term"
2118 "github.com/pkg/errors"
2219)
2320
@@ -43,7 +40,7 @@ func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInf
4340 default :
4441 }
4542
46- err = ConfigureAuth (cli , "" , "" , & authConfig , isDefaultRegistry )
43+ err = ConfigureAuth (ctx , cli , "" , "" , & authConfig , isDefaultRegistry )
4744 if err != nil {
4845 return "" , err
4946 }
@@ -89,7 +86,7 @@ func GetDefaultAuthConfig(cfg *configfile.ConfigFile, checkCredStore bool, serve
8986}
9087
9188// ConfigureAuth handles prompting of user's username and password if needed
92- func ConfigureAuth (cli Cli , flUser , flPassword string , authconfig * registrytypes.AuthConfig , isDefaultRegistry bool ) error {
89+ func ConfigureAuth (ctx context. Context , cli Cli , flUser , flPassword string , authconfig * registrytypes.AuthConfig , isDefaultRegistry bool ) error {
9390 // On Windows, force the use of the regular OS stdin stream.
9491 //
9592 // See:
@@ -124,9 +121,15 @@ func ConfigureAuth(cli Cli, flUser, flPassword string, authconfig *registrytypes
124121 fmt .Fprintln (cli .Out ())
125122 }
126123 }
127- promptWithDefault (cli .Out (), "Username" , authconfig .Username )
124+
125+ var prompt string
126+ if authconfig .Username == "" {
127+ prompt = "Username: "
128+ } else {
129+ prompt = fmt .Sprintf ("Username (%s): " , authconfig .Username )
130+ }
128131 var err error
129- flUser , err = readInput ( cli .In ())
132+ flUser , err = PromptForInput ( ctx , cli .In (), cli . Out (), prompt )
130133 if err != nil {
131134 return err
132135 }
@@ -138,16 +141,8 @@ func ConfigureAuth(cli Cli, flUser, flPassword string, authconfig *registrytypes
138141 return errors .Errorf ("Error: Non-null Username Required" )
139142 }
140143 if flPassword == "" {
141- oldState , err := term .SaveState (cli .In ().FD ())
142- if err != nil {
143- return err
144- }
145- fmt .Fprintf (cli .Out (), "Password: " )
146- _ = term .DisableEcho (cli .In ().FD (), oldState )
147- defer func () {
148- _ = term .RestoreTerminal (cli .In ().FD (), oldState )
149- }()
150- flPassword , err = readInput (cli .In ())
144+ var err error
145+ flPassword , err = PromptForInput (ctx , cli .In (), cli .Out (), "Password: " , WithHideUserInput (true ))
151146 if err != nil {
152147 return err
153148 }
@@ -163,25 +158,6 @@ func ConfigureAuth(cli Cli, flUser, flPassword string, authconfig *registrytypes
163158 return nil
164159}
165160
166- // readInput reads, and returns user input from in. It tries to return a
167- // single line, not including the end-of-line bytes, and trims leading
168- // and trailing whitespace.
169- func readInput (in io.Reader ) (string , error ) {
170- line , _ , err := bufio .NewReader (in ).ReadLine ()
171- if err != nil {
172- return "" , errors .Wrap (err , "error while reading input" )
173- }
174- return strings .TrimSpace (string (line )), nil
175- }
176-
177- func promptWithDefault (out io.Writer , prompt string , configDefault string ) {
178- if configDefault == "" {
179- fmt .Fprintf (out , "%s: " , prompt )
180- } else {
181- fmt .Fprintf (out , "%s (%s): " , prompt , configDefault )
182- }
183- }
184-
185161// RetrieveAuthTokenFromImage retrieves an encoded auth token given a complete
186162// image. The auth configuration is serialized as a base64url encoded RFC4648,
187163// section 5) JSON string for sending through the X-Registry-Auth header.
0 commit comments