github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/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]"}, Cli.DockerCommands["logout"].Description+".\nIf no server is specified \""+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  		return nil
    30  	}
    31  
    32  	fmt.Fprintf(cli.out, "Remove login credentials for %s\n", serverAddress)
    33  	delete(cli.configFile.AuthConfigs, serverAddress)
    34  	if err := cli.configFile.Save(); err != nil {
    35  		return fmt.Errorf("Failed to save docker config: %v", err)
    36  	}
    37  
    38  	return nil
    39  }