Skip to content

Commit 3a84850

Browse files
committed
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>
1 parent 8a7c5ae commit 3a84850

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

cli/command/registry.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func ConfigureAuth(ctx context.Context, cli Cli, flUser, flPassword string, auth
110110
// If defaultUsername is not empty, the username prompt includes that username
111111
// and the user can hit enter without inputting a username to use that default
112112
// username.
113-
func PromptUserForCredentials(ctx context.Context, cli Cli, argUser, argPassword, defaultUsername, serverAddress string) (authConfig registrytypes.AuthConfig, err error) {
113+
func PromptUserForCredentials(ctx context.Context, cli Cli, argUser, argPassword, defaultUsername, serverAddress string) (registrytypes.AuthConfig, error) {
114114
// On Windows, force the use of the regular OS stdin stream.
115115
//
116116
// See:
@@ -142,38 +142,41 @@ func PromptUserForCredentials(ctx context.Context, cli Cli, argUser, argPassword
142142
} else {
143143
prompt = fmt.Sprintf("Username (%s): ", defaultUsername)
144144
}
145+
146+
var err error
145147
argUser, err = PromptForInput(ctx, cli.In(), cli.Out(), prompt)
146148
if err != nil {
147-
return authConfig, err
149+
return registrytypes.AuthConfig{}, err
148150
}
149151
if argUser == "" {
150152
argUser = defaultUsername
151153
}
152154
}
153155
if argUser == "" {
154-
return authConfig, errors.Errorf("Error: Non-null Username Required")
156+
return registrytypes.AuthConfig{}, errors.Errorf("Error: Non-null Username Required")
155157
}
156158
if argPassword == "" {
157159
restoreInput, err := DisableInputEcho(cli.In())
158160
if err != nil {
159-
return authConfig, err
161+
return registrytypes.AuthConfig{}, err
160162
}
161163
defer restoreInput()
162164

163165
argPassword, err = PromptForInput(ctx, cli.In(), cli.Out(), "Password: ")
164166
if err != nil {
165-
return authConfig, err
167+
return registrytypes.AuthConfig{}, err
166168
}
167169
fmt.Fprint(cli.Out(), "\n")
168170
if argPassword == "" {
169-
return authConfig, errors.Errorf("Error: Password Required")
171+
return registrytypes.AuthConfig{}, errors.Errorf("Error: Password Required")
170172
}
171173
}
172174

173-
authConfig.Username = argUser
174-
authConfig.Password = argPassword
175-
authConfig.ServerAddress = serverAddress
176-
return authConfig, nil
175+
return registrytypes.AuthConfig{
176+
Username: argUser,
177+
Password: argPassword,
178+
ServerAddress: serverAddress,
179+
}, nil
177180
}
178181

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

0 commit comments

Comments
 (0)