Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions cli/command/registry/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,46 +31,44 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you move this var declaration closer to where it is actually used?

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
// which means it couldn't have user/pass cause they may be in the creds store
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
}