diff --git a/cli/command/registry/logout.go b/cli/command/registry/logout.go index ac84139f7efc..30d0cc9e50f5 100644 --- a/cli/command/registry/logout.go +++ b/cli/command/registry/logout.go @@ -31,24 +31,24 @@ func NewLogoutCommand(dockerCli command.Cli) *cobra.Command { func runLogout(dockerCli command.Cli, serverAddress string) error { ctx := context.Background() - var isDefaultRegistry bool + var ( + loggedIn bool // is set later, when checking for credentials + regsToTry []string // is set based on the type of registry + ) + + // differentiate between default und private registry if serverAddress == "" { + // if no server address given, handle case for default registry serverAddress = command.ElectAuthServer(ctx, dockerCli) - isDefaultRegistry = true - } + regsToTry = []string{serverAddress} + } else { + // if server address given, handle case for private registry + hostnameAddress := registry.ConvertToHostname(serverAddress) - var ( - loggedIn bool - regsToLogout []string - hostnameAddress = serverAddress - regsToTry = []string{serverAddress} - ) - if !isDefaultRegistry { - hostnameAddress = registry.ConvertToHostname(serverAddress) // the tries below are kept for backward compatibility where a user could have // saved the registry in one of the following format. - regsToTry = append(regsToTry, hostnameAddress, "http://"+hostnameAddress, "https://"+hostnameAddress) + regsToTry = []string{hostnameAddress, "http://" + hostnameAddress, "https://" + hostnameAddress} } // check if we're logged in based on the records in the config file @@ -56,21 +56,19 @@ func runLogout(dockerCli command.Cli, serverAddress string) error { for _, s := range regsToTry { if _, ok := dockerCli.ConfigFile().AuthConfigs[s]; ok { loggedIn = true - regsToLogout = append(regsToLogout, s) + + // remove credentials, for found auth config + fmt.Fprintf(dockerCli.Out(), "Removing login credentials for %s\n", serverAddress) + if err := dockerCli.ConfigFile().GetCredentialsStore(s).Erase(s); err != nil { + fmt.Fprintf(dockerCli.Err(), "WARNING: could not erase credentials: %v\n", err) + } } } if !loggedIn { - fmt.Fprintf(dockerCli.Out(), "Not logged in to %s\n", hostnameAddress) + fmt.Fprintf(dockerCli.Out(), "Not logged in to %s\n", serverAddress) return nil } - fmt.Fprintf(dockerCli.Out(), "Removing login credentials for %s\n", hostnameAddress) - for _, r := range regsToLogout { - if err := dockerCli.ConfigFile().GetCredentialsStore(r).Erase(r); err != nil { - fmt.Fprintf(dockerCli.Err(), "WARNING: could not erase credentials: %v\n", err) - } - } - return nil }