Skip to content

Commit 0ab0eca

Browse files
authored
Merge pull request #5550 from thaJeztah/login_minor_refactor
cli/command: PromptUserForCredentials: assorted minor improvements and (linting) fixes
2 parents abb8e9b + 4b7a1e4 commit 0ab0eca

1 file changed

Lines changed: 46 additions & 28 deletions

File tree

cli/command/registry.go

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,24 @@ import (
1818
"github.com/pkg/errors"
1919
)
2020

21-
const patSuggest = "You can log in with your password or a Personal Access " +
22-
"Token (PAT). Using a limited-scope PAT grants better security and is required " +
23-
"for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/"
21+
const (
22+
registerSuggest = "Log in with your Docker ID or email address to push and pull images from Docker Hub. " +
23+
"If you don't have a Docker ID, head over to https://hub.docker.com/ to create one."
24+
patSuggest = "You can log in with your password or a Personal Access " +
25+
"Token (PAT). Using a limited-scope PAT grants better security and is required " +
26+
"for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/"
27+
)
2428

2529
// RegistryAuthenticationPrivilegedFunc returns a RequestPrivilegeFunc from the specified registry index info
2630
// for the given command.
2731
func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInfo, cmdName string) registrytypes.RequestAuthConfig {
2832
return func(ctx context.Context) (string, error) {
29-
fmt.Fprintf(cli.Out(), "\nLogin prior to %s:\n", cmdName)
33+
_, _ = fmt.Fprintf(cli.Out(), "\nLogin prior to %s:\n", cmdName)
3034
indexServer := registry.GetAuthConfigKey(index)
3135
isDefaultRegistry := indexServer == registry.IndexServer
3236
authConfig, err := GetDefaultAuthConfig(cli.ConfigFile(), true, indexServer, isDefaultRegistry)
3337
if err != nil {
34-
fmt.Fprintf(cli.Err(), "Unable to retrieve stored credentials for %s, error: %s.\n", indexServer, err)
38+
_, _ = fmt.Fprintf(cli.Err(), "Unable to retrieve stored credentials for %s, error: %s.\n", indexServer, err)
3539
}
3640

3741
select {
@@ -111,7 +115,7 @@ func ConfigureAuth(ctx context.Context, cli Cli, flUser, flPassword string, auth
111115
// If defaultUsername is not empty, the username prompt includes that username
112116
// and the user can hit enter without inputting a username to use that default
113117
// username.
114-
func PromptUserForCredentials(ctx context.Context, cli Cli, argUser, argPassword, defaultUsername, serverAddress string) (authConfig registrytypes.AuthConfig, err error) {
118+
func PromptUserForCredentials(ctx context.Context, cli Cli, argUser, argPassword, defaultUsername, serverAddress string) (registrytypes.AuthConfig, error) {
115119
// On Windows, force the use of the regular OS stdin stream.
116120
//
117121
// See:
@@ -124,57 +128,71 @@ func PromptUserForCredentials(ctx context.Context, cli Cli, argUser, argPassword
124128
cli.SetIn(streams.NewIn(os.Stdin))
125129
}
126130

127-
isDefaultRegistry := serverAddress == registry.IndexServer
128-
defaultUsername = strings.TrimSpace(defaultUsername)
129-
130-
if argUser = strings.TrimSpace(argUser); argUser == "" {
131-
if isDefaultRegistry {
132-
// if this is a default registry (docker hub), then display the following message.
133-
fmt.Fprintln(cli.Out(), "Log in with your Docker ID or email address 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.")
131+
argUser = strings.TrimSpace(argUser)
132+
if argUser == "" {
133+
if serverAddress == registry.IndexServer {
134+
// When signing in to the default (Docker Hub) registry, we display
135+
// hints for creating an account, and (if hints are enabled), using
136+
// a token instead of a password.
137+
_, _ = fmt.Fprintln(cli.Out(), registerSuggest)
134138
if hints.Enabled() {
135-
fmt.Fprintln(cli.Out(), patSuggest)
136-
fmt.Fprintln(cli.Out())
139+
_, _ = fmt.Fprintln(cli.Out(), patSuggest)
140+
_, _ = fmt.Fprintln(cli.Out())
137141
}
138142
}
139143

140144
var prompt string
145+
defaultUsername = strings.TrimSpace(defaultUsername)
141146
if defaultUsername == "" {
142147
prompt = "Username: "
143148
} else {
144149
prompt = fmt.Sprintf("Username (%s): ", defaultUsername)
145150
}
151+
152+
var err error
146153
argUser, err = PromptForInput(ctx, cli.In(), cli.Out(), prompt)
147154
if err != nil {
148-
return authConfig, err
155+
return registrytypes.AuthConfig{}, err
149156
}
150157
if argUser == "" {
151158
argUser = defaultUsername
152159
}
160+
if argUser == "" {
161+
return registrytypes.AuthConfig{}, errors.Errorf("Error: Non-null Username Required")
162+
}
153163
}
154-
if argUser == "" {
155-
return authConfig, errors.Errorf("Error: Non-null Username Required")
156-
}
164+
165+
argPassword = strings.TrimSpace(argPassword)
157166
if argPassword == "" {
158167
restoreInput, err := DisableInputEcho(cli.In())
159168
if err != nil {
160-
return authConfig, err
169+
return registrytypes.AuthConfig{}, err
161170
}
162-
defer restoreInput()
171+
defer func() {
172+
if err := restoreInput(); err != nil {
173+
// TODO(thaJeztah): we should consider printing instructions how
174+
// to restore this manually (other than restarting the shell).
175+
// e.g., 'run stty echo' when in a Linux or macOS shell, but
176+
// PowerShell and CMD.exe may need different instructions.
177+
_, _ = fmt.Fprintln(cli.Err(), "Error: failed to restore terminal state to echo input:", err)
178+
}
179+
}()
163180

164181
argPassword, err = PromptForInput(ctx, cli.In(), cli.Out(), "Password: ")
165182
if err != nil {
166-
return authConfig, err
183+
return registrytypes.AuthConfig{}, err
167184
}
168-
fmt.Fprint(cli.Out(), "\n")
185+
_, _ = fmt.Fprintln(cli.Out())
169186
if argPassword == "" {
170-
return authConfig, errors.Errorf("Error: Password Required")
187+
return registrytypes.AuthConfig{}, errors.Errorf("Error: Password Required")
171188
}
172189
}
173190

174-
authConfig.Username = argUser
175-
authConfig.Password = argPassword
176-
authConfig.ServerAddress = serverAddress
177-
return authConfig, nil
191+
return registrytypes.AuthConfig{
192+
Username: argUser,
193+
Password: argPassword,
194+
ServerAddress: serverAddress,
195+
}, nil
178196
}
179197

180198
// RetrieveAuthTokenFromImage retrieves an encoded auth token given a complete

0 commit comments

Comments
 (0)