logout.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package registry
  2. import (
  3. "fmt"
  4. "golang.org/x/net/context"
  5. "github.com/docker/docker/api/client"
  6. "github.com/docker/docker/cli"
  7. "github.com/spf13/cobra"
  8. )
  9. // NewLogoutCommand creates a new `docker login` command
  10. func NewLogoutCommand(dockerCli *client.DockerCli) *cobra.Command {
  11. cmd := &cobra.Command{
  12. Use: "logout [SERVER]",
  13. Short: "Log out from a Docker registry.",
  14. Long: "Log out from a Docker registry.\nIf no server is specified, the default is defined by the daemon.",
  15. Args: cli.RequiresMaxArgs(1),
  16. RunE: func(cmd *cobra.Command, args []string) error {
  17. var serverAddress string
  18. if len(args) > 0 {
  19. serverAddress = args[0]
  20. }
  21. return runLogout(dockerCli, serverAddress)
  22. },
  23. }
  24. return cmd
  25. }
  26. func runLogout(dockerCli *client.DockerCli, serverAddress string) error {
  27. ctx := context.Background()
  28. if serverAddress == "" {
  29. serverAddress = dockerCli.ElectAuthServer(ctx)
  30. }
  31. // check if we're logged in based on the records in the config file
  32. // which means it couldn't have user/pass cause they may be in the creds store
  33. if _, ok := dockerCli.ConfigFile().AuthConfigs[serverAddress]; !ok {
  34. fmt.Fprintf(dockerCli.Out(), "Not logged in to %s\n", serverAddress)
  35. return nil
  36. }
  37. fmt.Fprintf(dockerCli.Out(), "Remove login credentials for %s\n", serverAddress)
  38. if err := client.EraseCredentials(dockerCli.ConfigFile(), serverAddress); err != nil {
  39. fmt.Fprintf(dockerCli.Err(), "WARNING: could not erase credentials: %v\n", err)
  40. }
  41. return nil
  42. }