6 "golang.org/x/net/context"
8 "github.com/docker/cli/cli"
9 "github.com/docker/cli/cli/command"
10 "github.com/docker/docker/registry"
11 "github.com/spf13/cobra"
14 // NewLogoutCommand creates a new `docker logout` command
15 func NewLogoutCommand(dockerCli command.Cli) *cobra.Command {
16 cmd := &cobra.Command{
17 Use: "logout [SERVER]",
18 Short: "Log out from a Docker registry",
19 Long: "Log out from a Docker registry.\nIf no server is specified, the default is defined by the daemon.",
20 Args: cli.RequiresMaxArgs(1),
21 RunE: func(cmd *cobra.Command, args []string) error {
22 var serverAddress string
24 serverAddress = args[0]
26 return runLogout(dockerCli, serverAddress)
33 func runLogout(dockerCli command.Cli, serverAddress string) error {
34 ctx := context.Background()
35 var isDefaultRegistry bool
37 if serverAddress == "" {
38 serverAddress = command.ElectAuthServer(ctx, dockerCli)
39 isDefaultRegistry = true
45 hostnameAddress = serverAddress
46 regsToTry = []string{serverAddress}
48 if !isDefaultRegistry {
49 hostnameAddress = registry.ConvertToHostname(serverAddress)
50 // the tries below are kept for backward compatibility where a user could have
51 // saved the registry in one of the following format.
52 regsToTry = append(regsToTry, hostnameAddress, "http://"+hostnameAddress, "https://"+hostnameAddress)
55 // check if we're logged in based on the records in the config file
56 // which means it couldn't have user/pass cause they may be in the creds store
57 for _, s := range regsToTry {
58 if _, ok := dockerCli.ConfigFile().AuthConfigs[s]; ok {
60 regsToLogout = append(regsToLogout, s)
65 fmt.Fprintf(dockerCli.Out(), "Not logged in to %s\n", hostnameAddress)
69 fmt.Fprintf(dockerCli.Out(), "Removing login credentials for %s\n", hostnameAddress)
70 for _, r := range regsToLogout {
71 if err := dockerCli.CredentialsStore(r).Erase(r); err != nil {
72 fmt.Fprintf(dockerCli.Err(), "WARNING: could not erase credentials: %v\n", err)