cli/command: PromptUserForCredentials: remove named output variables

This function has multiple conditional branches, which makes it harder
to see at a glance whether authConfig may be partially populated. This
patch instead returns a fresh instance for error returns to prevent any
confusion.

It also removes the named output variables, as they're now no longer used,
and the returned types should already be descriptive enough to understand
what's returned.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 3a8485085d)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2024-10-19 11:46:21 +02:00
parent eb986ae71b
commit d23ab524c3
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 13 additions and 10 deletions

View File

@ -112,7 +112,7 @@ func ConfigureAuth(ctx context.Context, cli Cli, flUser, flPassword string, auth
// If defaultUsername is not empty, the username prompt includes that username // If defaultUsername is not empty, the username prompt includes that username
// and the user can hit enter without inputting a username to use that default // and the user can hit enter without inputting a username to use that default
// username. // username.
func PromptUserForCredentials(ctx context.Context, cli Cli, argUser, argPassword, defaultUsername, serverAddress string) (authConfig registrytypes.AuthConfig, err error) { func PromptUserForCredentials(ctx context.Context, cli Cli, argUser, argPassword, defaultUsername, serverAddress string) (registrytypes.AuthConfig, error) {
// On Windows, force the use of the regular OS stdin stream. // On Windows, force the use of the regular OS stdin stream.
// //
// See: // See:
@ -144,38 +144,41 @@ func PromptUserForCredentials(ctx context.Context, cli Cli, argUser, argPassword
} else { } else {
prompt = fmt.Sprintf("Username (%s): ", defaultUsername) prompt = fmt.Sprintf("Username (%s): ", defaultUsername)
} }
var err error
argUser, err = PromptForInput(ctx, cli.In(), cli.Out(), prompt) argUser, err = PromptForInput(ctx, cli.In(), cli.Out(), prompt)
if err != nil { if err != nil {
return authConfig, err return registrytypes.AuthConfig{}, err
} }
if argUser == "" { if argUser == "" {
argUser = defaultUsername argUser = defaultUsername
} }
} }
if argUser == "" { if argUser == "" {
return authConfig, errors.Errorf("Error: Non-null Username Required") return registrytypes.AuthConfig{}, errors.Errorf("Error: Non-null Username Required")
} }
if argPassword == "" { if argPassword == "" {
restoreInput, err := DisableInputEcho(cli.In()) restoreInput, err := DisableInputEcho(cli.In())
if err != nil { if err != nil {
return authConfig, err return registrytypes.AuthConfig{}, err
} }
defer restoreInput() defer restoreInput()
argPassword, err = PromptForInput(ctx, cli.In(), cli.Out(), "Password: ") argPassword, err = PromptForInput(ctx, cli.In(), cli.Out(), "Password: ")
if err != nil { if err != nil {
return authConfig, err return registrytypes.AuthConfig{}, err
} }
fmt.Fprint(cli.Out(), "\n") fmt.Fprint(cli.Out(), "\n")
if argPassword == "" { if argPassword == "" {
return authConfig, errors.Errorf("Error: Password Required") return registrytypes.AuthConfig{}, errors.Errorf("Error: Password Required")
} }
} }
authConfig.Username = argUser return registrytypes.AuthConfig{
authConfig.Password = argPassword Username: argUser,
authConfig.ServerAddress = serverAddress Password: argPassword,
return authConfig, nil ServerAddress: serverAddress,
}, nil
} }
// RetrieveAuthTokenFromImage retrieves an encoded auth token given a complete // RetrieveAuthTokenFromImage retrieves an encoded auth token given a complete