github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/registry/logout.go (about) 1 package registry 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/docker/cli/cli" 8 "github.com/docker/cli/cli/command" 9 "github.com/docker/docker/registry" 10 "github.com/spf13/cobra" 11 ) 12 13 // NewLogoutCommand creates a new `docker logout` command 14 func NewLogoutCommand(dockerCli command.Cli) *cobra.Command { 15 cmd := &cobra.Command{ 16 Use: "logout [SERVER]", 17 Short: "Log out from a Docker registry", 18 Long: "Log out from a Docker registry.\nIf no server is specified, the default is defined by the daemon.", 19 Args: cli.RequiresMaxArgs(1), 20 RunE: func(cmd *cobra.Command, args []string) error { 21 var serverAddress string 22 if len(args) > 0 { 23 serverAddress = args[0] 24 } 25 return runLogout(dockerCli, serverAddress) 26 }, 27 } 28 29 return cmd 30 } 31 32 func runLogout(dockerCli command.Cli, serverAddress string) error { 33 ctx := context.Background() 34 var isDefaultRegistry bool 35 36 if serverAddress == "" { 37 serverAddress = command.ElectAuthServer(ctx, dockerCli) 38 isDefaultRegistry = true 39 } 40 41 var ( 42 regsToLogout = []string{serverAddress} 43 hostnameAddress = serverAddress 44 ) 45 if !isDefaultRegistry { 46 hostnameAddress = registry.ConvertToHostname(serverAddress) 47 // the tries below are kept for backward compatibility where a user could have 48 // saved the registry in one of the following format. 49 regsToLogout = append(regsToLogout, hostnameAddress, "http://"+hostnameAddress, "https://"+hostnameAddress) 50 } 51 52 fmt.Fprintf(dockerCli.Out(), "Removing login credentials for %s\n", hostnameAddress) 53 errs := make(map[string]error) 54 for _, r := range regsToLogout { 55 if err := dockerCli.ConfigFile().GetCredentialsStore(r).Erase(r); err != nil { 56 errs[r] = err 57 } 58 } 59 60 // if at least one removal succeeded, report success. Otherwise report errors 61 if len(errs) == len(regsToLogout) { 62 fmt.Fprintln(dockerCli.Err(), "WARNING: could not erase credentials:") 63 for k, v := range errs { 64 fmt.Fprintf(dockerCli.Err(), "%s: %s\n", k, v) 65 } 66 } 67 68 return nil 69 }