2015-03-24 23:57:23 -04:00
package client
import (
"bufio"
"fmt"
"io"
"os"
2015-11-05 17:19:48 -05:00
"runtime"
2015-03-24 23:57:23 -04:00
"strings"
2015-05-05 00:18:28 -04:00
Cli "github.com/docker/docker/cli"
2015-03-24 23:57:23 -04:00
flag "github.com/docker/docker/pkg/mflag"
"github.com/docker/docker/pkg/term"
"github.com/docker/docker/registry"
2016-01-04 19:05:26 -05:00
"github.com/docker/engine-api/client"
"github.com/docker/engine-api/types"
2015-03-24 23:57:23 -04:00
)
2015-03-25 13:34:41 -04:00
// CmdLogin logs in or registers a user to a Docker registry service.
//
// If no server is specified, the user will be logged into or registered to the registry's index server.
//
// Usage: docker login SERVER
2015-03-24 23:57:23 -04:00
func ( cli * DockerCli ) CmdLogin ( args ... string ) error {
2015-10-08 08:46:21 -04:00
cmd := Cli . Subcmd ( "login" , [ ] string { "[SERVER]" } , Cli . DockerCommands [ "login" ] . Description + ".\nIf no server is specified \"" + registry . IndexServer + "\" is the default." , true )
2015-03-24 23:57:23 -04:00
cmd . Require ( flag . Max , 1 )
var username , password , email string
cmd . StringVar ( & username , [ ] string { "u" , "-username" } , "" , "Username" )
cmd . StringVar ( & password , [ ] string { "p" , "-password" } , "" , "Password" )
cmd . StringVar ( & email , [ ] string { "e" , "-email" } , "" , "Email" )
2015-03-28 21:22:46 -04:00
cmd . ParseFlags ( args , true )
2015-03-24 23:57:23 -04:00
2015-11-05 17:19:48 -05:00
// On Windows, force the use of the regular OS stdin stream. Fixes #14336/#14210
if runtime . GOOS == "windows" {
cli . in = os . Stdin
}
2015-07-21 15:40:36 -04:00
serverAddress := registry . IndexServer
2015-03-24 23:57:23 -04:00
if len ( cmd . Args ( ) ) > 0 {
serverAddress = cmd . Arg ( 0 )
}
promptDefault := func ( prompt string , configDefault string ) {
if configDefault == "" {
fmt . Fprintf ( cli . out , "%s: " , prompt )
} else {
fmt . Fprintf ( cli . out , "%s (%s): " , prompt , configDefault )
}
}
readInput := func ( in io . Reader , out io . Writer ) string {
reader := bufio . NewReader ( in )
line , _ , err := reader . ReadLine ( )
if err != nil {
fmt . Fprintln ( out , err . Error ( ) )
os . Exit ( 1 )
}
return string ( line )
}
2015-04-01 18:39:37 -04:00
authconfig , ok := cli . configFile . AuthConfigs [ serverAddress ]
2015-03-24 23:57:23 -04:00
if ! ok {
2015-12-11 23:11:42 -05:00
authconfig = types . AuthConfig { }
2015-03-24 23:57:23 -04:00
}
if username == "" {
promptDefault ( "Username" , authconfig . Username )
username = readInput ( cli . in , cli . out )
2015-09-09 07:29:19 -04:00
username = strings . TrimSpace ( username )
2015-03-24 23:57:23 -04:00
if username == "" {
username = authconfig . Username
}
}
// Assume that a different username means they may not want to use
// the password or email from the config file, so prompt them
if username != authconfig . Username {
if password == "" {
oldState , err := term . SaveState ( cli . inFd )
if err != nil {
return err
}
fmt . Fprintf ( cli . out , "Password: " )
term . DisableEcho ( cli . inFd , oldState )
password = readInput ( cli . in , cli . out )
fmt . Fprint ( cli . out , "\n" )
term . RestoreTerminal ( cli . inFd , oldState )
if password == "" {
return fmt . Errorf ( "Error : Password Required" )
}
}
if email == "" {
promptDefault ( "Email" , authconfig . Email )
email = readInput ( cli . in , cli . out )
if email == "" {
email = authconfig . Email
}
}
} else {
// However, if they don't override the username use the
// password or email from the cmd line if specified. IOW, allow
// then to change/override them. And if not specified, just
// use what's in the config file
if password == "" {
password = authconfig . Password
}
if email == "" {
email = authconfig . Email
}
}
authconfig . Username = username
authconfig . Password = password
authconfig . Email = email
authconfig . ServerAddress = serverAddress
2015-04-01 18:39:37 -04:00
cli . configFile . AuthConfigs [ serverAddress ] = authconfig
2015-03-24 23:57:23 -04:00
2015-12-03 19:12:35 -05:00
auth := cli . configFile . AuthConfigs [ serverAddress ]
response , err := cli . client . RegistryLogin ( auth )
2015-03-24 23:57:23 -04:00
if err != nil {
2016-01-04 19:05:26 -05:00
if client . IsErrUnauthorized ( err ) {
2015-12-03 19:12:35 -05:00
delete ( cli . configFile . AuthConfigs , serverAddress )
if err2 := cli . configFile . Save ( ) ; err2 != nil {
fmt . Fprintf ( cli . out , "WARNING: could not save config file: %v\n" , err2 )
}
}
2015-03-24 23:57:23 -04:00
return err
}
2015-04-01 18:39:37 -04:00
if err := cli . configFile . Save ( ) ; err != nil {
return fmt . Errorf ( "Error saving config file: %v" , err )
}
fmt . Fprintf ( cli . out , "WARNING: login credentials saved in %s\n" , cli . configFile . Filename ( ) )
2015-03-24 23:57:23 -04:00
if response . Status != "" {
fmt . Fprintf ( cli . out , "%s\n" , response . Status )
}
return nil
}