2016-06-10 06:04:29 -04:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-06-10 06:04:29 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type loginOptions struct {
|
|
|
|
serverAddress string
|
|
|
|
user string
|
|
|
|
password string
|
|
|
|
email string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLoginCommand creates a new `docker login` command
|
2016-09-08 13:11:39 -04:00
|
|
|
func NewLoginCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-10 06:04:29 -04:00
|
|
|
var opts loginOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "login [OPTIONS] [SERVER]",
|
2016-10-31 23:07:31 -04:00
|
|
|
Short: "Log in to a Docker registry",
|
2016-06-10 06:04:29 -04:00
|
|
|
Long: "Log in to a Docker registry.\nIf no server is specified, the default is defined by the daemon.",
|
|
|
|
Args: cli.RequiresMaxArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) > 0 {
|
|
|
|
opts.serverAddress = args[0]
|
|
|
|
}
|
|
|
|
return runLogin(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.StringVarP(&opts.user, "username", "u", "", "Username")
|
|
|
|
flags.StringVarP(&opts.password, "password", "p", "", "Password")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runLogin(dockerCli *command.DockerCli, opts loginOptions) error {
|
2016-06-10 06:04:29 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
clnt := dockerCli.Client()
|
|
|
|
|
2016-05-30 06:47:49 -04:00
|
|
|
var (
|
|
|
|
serverAddress string
|
2016-09-09 15:38:00 -04:00
|
|
|
authServer = command.ElectAuthServer(ctx, dockerCli)
|
2016-05-30 06:47:49 -04:00
|
|
|
)
|
2016-06-10 06:04:29 -04:00
|
|
|
if opts.serverAddress != "" {
|
|
|
|
serverAddress = opts.serverAddress
|
|
|
|
} else {
|
2016-05-30 06:47:49 -04:00
|
|
|
serverAddress = authServer
|
2016-06-10 06:04:29 -04:00
|
|
|
}
|
2016-05-30 06:47:49 -04:00
|
|
|
|
|
|
|
isDefaultRegistry := serverAddress == authServer
|
|
|
|
|
2016-09-09 15:38:00 -04:00
|
|
|
authConfig, err := command.ConfigureAuth(dockerCli, opts.user, opts.password, serverAddress, isDefaultRegistry)
|
2016-06-10 06:04:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
response, err := clnt.RegistryLogin(ctx, authConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if response.IdentityToken != "" {
|
|
|
|
authConfig.Password = ""
|
|
|
|
authConfig.IdentityToken = response.IdentityToken
|
|
|
|
}
|
2016-08-18 17:23:10 -04:00
|
|
|
if err := dockerCli.CredentialsStore(serverAddress).Store(authConfig); err != nil {
|
2016-06-10 06:04:29 -04:00
|
|
|
return fmt.Errorf("Error saving credentials: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Status != "" {
|
|
|
|
fmt.Fprintln(dockerCli.Out(), response.Status)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|