github.com/kobeld/docker@v1.12.0-rc1/api/client/registry/logout.go (about)

     1  package registry
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/net/context"
     7  
     8  	"github.com/docker/docker/api/client"
     9  	"github.com/docker/docker/cli"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  // NewLogoutCommand creates a new `docker login` command
    14  func NewLogoutCommand(dockerCli *client.DockerCli) *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 *client.DockerCli, serverAddress string) error {
    33  	ctx := context.Background()
    34  
    35  	if serverAddress == "" {
    36  		serverAddress = dockerCli.ElectAuthServer(ctx)
    37  	}
    38  
    39  	// check if we're logged in based on the records in the config file
    40  	// which means it couldn't have user/pass cause they may be in the creds store
    41  	if _, ok := dockerCli.ConfigFile().AuthConfigs[serverAddress]; !ok {
    42  		fmt.Fprintf(dockerCli.Out(), "Not logged in to %s\n", serverAddress)
    43  		return nil
    44  	}
    45  
    46  	fmt.Fprintf(dockerCli.Out(), "Remove login credentials for %s\n", serverAddress)
    47  	if err := client.EraseCredentials(dockerCli.ConfigFile(), serverAddress); err != nil {
    48  		fmt.Fprintf(dockerCli.Out(), "WARNING: could not erase credentials: %v\n", err)
    49  	}
    50  
    51  	return nil
    52  }