From d23ab524c320ff7ba9a2dd5ab2c4bb9c8534f7fe Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 19 Oct 2024 11:46:21 +0200 Subject: [PATCH] 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 (cherry picked from commit 3a8485085d94c85dae8078ee36310baa69a21217) Signed-off-by: Sebastiaan van Stijn --- cli/command/registry.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/cli/command/registry.go b/cli/command/registry.go index 3ec07e801d..642667a11f 100644 --- a/cli/command/registry.go +++ b/cli/command/registry.go @@ -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 // and the user can hit enter without inputting a username to use that default // 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. // // See: @@ -144,38 +144,41 @@ func PromptUserForCredentials(ctx context.Context, cli Cli, argUser, argPassword } else { prompt = fmt.Sprintf("Username (%s): ", defaultUsername) } + + var err error argUser, err = PromptForInput(ctx, cli.In(), cli.Out(), prompt) if err != nil { - return authConfig, err + return registrytypes.AuthConfig{}, err } if argUser == "" { argUser = defaultUsername } } if argUser == "" { - return authConfig, errors.Errorf("Error: Non-null Username Required") + return registrytypes.AuthConfig{}, errors.Errorf("Error: Non-null Username Required") } if argPassword == "" { restoreInput, err := DisableInputEcho(cli.In()) if err != nil { - return authConfig, err + return registrytypes.AuthConfig{}, err } defer restoreInput() argPassword, err = PromptForInput(ctx, cli.In(), cli.Out(), "Password: ") if err != nil { - return authConfig, err + return registrytypes.AuthConfig{}, err } fmt.Fprint(cli.Out(), "\n") if argPassword == "" { - return authConfig, errors.Errorf("Error: Password Required") + return registrytypes.AuthConfig{}, errors.Errorf("Error: Password Required") } } - authConfig.Username = argUser - authConfig.Password = argPassword - authConfig.ServerAddress = serverAddress - return authConfig, nil + return registrytypes.AuthConfig{ + Username: argUser, + Password: argPassword, + ServerAddress: serverAddress, + }, nil } // RetrieveAuthTokenFromImage retrieves an encoded auth token given a complete