github.com/rentongzhang/docker@v1.8.2-rc1/api/client/logout.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  
     6  	Cli "github.com/docker/docker/cli"
     7  	flag "github.com/docker/docker/pkg/mflag"
     8  	"github.com/docker/docker/registry"
     9  )
    10  
    11  // CmdLogout logs a user out from a Docker registry.
    12  //
    13  // If no server is specified, the user will be logged out from the registry's index server.
    14  //
    15  // Usage: docker logout [SERVER]
    16  func (cli *DockerCli) CmdLogout(args ...string) error {
    17  	cmd := Cli.Subcmd("logout", []string{"[SERVER]"}, "Log out from a Docker registry, if no server is\nspecified \""+registry.IndexServer+"\" is the default.", true)
    18  	cmd.Require(flag.Max, 1)
    19  
    20  	cmd.ParseFlags(args, true)
    21  
    22  	serverAddress := registry.IndexServer
    23  	if len(cmd.Args()) > 0 {
    24  		serverAddress = cmd.Arg(0)
    25  	}
    26  
    27  	if _, ok := cli.configFile.AuthConfigs[serverAddress]; !ok {
    28  		fmt.Fprintf(cli.out, "Not logged in to %s\n", serverAddress)
    29  	} else {
    30  		fmt.Fprintf(cli.out, "Remove login credentials for %s\n", serverAddress)
    31  		delete(cli.configFile.AuthConfigs, serverAddress)
    32  
    33  		if err := cli.configFile.Save(); err != nil {
    34  			return fmt.Errorf("Failed to save docker config: %v", err)
    35  		}
    36  	}
    37  	return nil
    38  }