Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / docker / cli / cli / command / registry / logout.go
1 package registry
2
3 import (
4         "fmt"
5
6         "golang.org/x/net/context"
7
8         "github.com/docker/cli/cli"
9         "github.com/docker/cli/cli/command"
10         "github.com/docker/docker/registry"
11         "github.com/spf13/cobra"
12 )
13
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
23                         if len(args) > 0 {
24                                 serverAddress = args[0]
25                         }
26                         return runLogout(dockerCli, serverAddress)
27                 },
28         }
29
30         return cmd
31 }
32
33 func runLogout(dockerCli command.Cli, serverAddress string) error {
34         ctx := context.Background()
35         var isDefaultRegistry bool
36
37         if serverAddress == "" {
38                 serverAddress = command.ElectAuthServer(ctx, dockerCli)
39                 isDefaultRegistry = true
40         }
41
42         var (
43                 loggedIn        bool
44                 regsToLogout    []string
45                 hostnameAddress = serverAddress
46                 regsToTry       = []string{serverAddress}
47         )
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)
53         }
54
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 {
59                         loggedIn = true
60                         regsToLogout = append(regsToLogout, s)
61                 }
62         }
63
64         if !loggedIn {
65                 fmt.Fprintf(dockerCli.Out(), "Not logged in to %s\n", hostnameAddress)
66                 return nil
67         }
68
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)
73                 }
74         }
75
76         return nil
77 }