2016-06-10 06:04:29 -04:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-05-30 06:47:49 -04:00
|
|
|
"github.com/docker/docker/registry"
|
2016-06-10 06:04:29 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewLogoutCommand creates a new `docker login` command
|
2016-09-08 13:11:39 -04:00
|
|
|
func NewLogoutCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-10 06:04:29 -04:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "logout [SERVER]",
|
2016-10-31 23:07:31 -04:00
|
|
|
Short: "Log out from a Docker registry",
|
2016-06-10 06:04:29 -04:00
|
|
|
Long: "Log out from a Docker registry.\nIf no server is specified, the default is defined by the daemon.",
|
|
|
|
Args: cli.RequiresMaxArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
var serverAddress string
|
|
|
|
if len(args) > 0 {
|
|
|
|
serverAddress = args[0]
|
|
|
|
}
|
|
|
|
return runLogout(dockerCli, serverAddress)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runLogout(dockerCli *command.DockerCli, serverAddress string) error {
|
2016-06-10 06:04:29 -04:00
|
|
|
ctx := context.Background()
|
2016-05-30 06:47:49 -04:00
|
|
|
var isDefaultRegistry bool
|
2016-06-10 06:04:29 -04:00
|
|
|
|
|
|
|
if serverAddress == "" {
|
2016-09-09 15:38:00 -04:00
|
|
|
serverAddress = command.ElectAuthServer(ctx, dockerCli)
|
2016-05-30 06:47:49 -04:00
|
|
|
isDefaultRegistry = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
loggedIn bool
|
|
|
|
regsToLogout []string
|
|
|
|
hostnameAddress = serverAddress
|
|
|
|
regsToTry = []string{serverAddress}
|
|
|
|
)
|
|
|
|
if !isDefaultRegistry {
|
|
|
|
hostnameAddress = registry.ConvertToHostname(serverAddress)
|
2016-10-29 03:03:26 -04:00
|
|
|
// the tries below are kept for backward compatibility where a user could have
|
2016-05-30 06:47:49 -04:00
|
|
|
// saved the registry in one of the following format.
|
|
|
|
regsToTry = append(regsToTry, hostnameAddress, "http://"+hostnameAddress, "https://"+hostnameAddress)
|
2016-06-10 06:04:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2016-05-30 06:47:49 -04:00
|
|
|
for _, s := range regsToTry {
|
|
|
|
if _, ok := dockerCli.ConfigFile().AuthConfigs[s]; ok {
|
|
|
|
loggedIn = true
|
|
|
|
regsToLogout = append(regsToLogout, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !loggedIn {
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "Not logged in to %s\n", hostnameAddress)
|
2016-06-10 06:04:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-30 06:47:49 -04:00
|
|
|
fmt.Fprintf(dockerCli.Out(), "Removing login credentials for %s\n", hostnameAddress)
|
|
|
|
for _, r := range regsToLogout {
|
2016-09-09 15:24:10 -04:00
|
|
|
if err := dockerCli.CredentialsStore().Erase(r); err != nil {
|
2016-05-30 06:47:49 -04:00
|
|
|
fmt.Fprintf(dockerCli.Err(), "WARNING: could not erase credentials: %v\n", err)
|
|
|
|
}
|
2016-06-10 06:04:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|