mirror of https://github.com/docker/docs.git
Migrate pull command to cobra
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
parent
a05536ff42
commit
fa67b984f8
|
|
@ -11,7 +11,6 @@ func (cli *DockerCli) Command(name string) func(...string) error {
|
||||||
"login": cli.CmdLogin,
|
"login": cli.CmdLogin,
|
||||||
"logout": cli.CmdLogout,
|
"logout": cli.CmdLogout,
|
||||||
"ps": cli.CmdPs,
|
"ps": cli.CmdPs,
|
||||||
"pull": cli.CmdPull,
|
|
||||||
"push": cli.CmdPush,
|
"push": cli.CmdPush,
|
||||||
"update": cli.CmdUpdate,
|
"update": cli.CmdUpdate,
|
||||||
}[name]
|
}[name]
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,86 @@
|
||||||
|
package image
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/client"
|
||||||
|
"github.com/docker/docker/cli"
|
||||||
|
"github.com/docker/docker/reference"
|
||||||
|
"github.com/docker/docker/registry"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
type pullOptions struct {
|
||||||
|
remote string
|
||||||
|
all bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPullCommand creates a new `docker pull` command
|
||||||
|
func NewPullCommand(dockerCli *client.DockerCli) *cobra.Command {
|
||||||
|
var opts pullOptions
|
||||||
|
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "pull [OPTIONS] NAME[:TAG|@DIGEST]",
|
||||||
|
Short: "Pull an image or a repository from a registry",
|
||||||
|
Args: cli.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
opts.remote = args[0]
|
||||||
|
return runPull(dockerCli, opts)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
flags := cmd.Flags()
|
||||||
|
|
||||||
|
flags.BoolVarP(&opts.all, "all-tags", "a", false, "Download all tagged images in the repository")
|
||||||
|
client.AddTrustedFlags(flags, true)
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func runPull(dockerCli *client.DockerCli, opts pullOptions) error {
|
||||||
|
|
||||||
|
distributionRef, err := reference.ParseNamed(opts.remote)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if opts.all && !reference.IsNameOnly(distributionRef) {
|
||||||
|
return errors.New("tag can't be used with --all-tags/-a")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !opts.all && reference.IsNameOnly(distributionRef) {
|
||||||
|
distributionRef = reference.WithDefaultTag(distributionRef)
|
||||||
|
fmt.Fprintf(dockerCli.Out(), "Using default tag: %s\n", reference.DefaultTag)
|
||||||
|
}
|
||||||
|
|
||||||
|
var tag string
|
||||||
|
switch x := distributionRef.(type) {
|
||||||
|
case reference.Canonical:
|
||||||
|
tag = x.Digest().String()
|
||||||
|
case reference.NamedTagged:
|
||||||
|
tag = x.Tag()
|
||||||
|
}
|
||||||
|
|
||||||
|
registryRef := registry.ParseReference(tag)
|
||||||
|
|
||||||
|
// Resolve the Repository name from fqn to RepositoryInfo
|
||||||
|
repoInfo, err := registry.ParseRepositoryInfo(distributionRef)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
authConfig := dockerCli.ResolveAuthConfig(ctx, repoInfo.Index)
|
||||||
|
requestPrivilege := dockerCli.RegistryAuthenticationPrivilegedFunc(repoInfo.Index, "pull")
|
||||||
|
|
||||||
|
if client.IsTrusted() && !registryRef.HasDigest() {
|
||||||
|
// Check if tag is digest
|
||||||
|
return dockerCli.TrustedPull(ctx, repoInfo, registryRef, authConfig, requestPrivilege)
|
||||||
|
}
|
||||||
|
|
||||||
|
return dockerCli.ImagePullPrivileged(ctx, authConfig, distributionRef.String(), requestPrivilege, opts.all)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,74 +1,14 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
Cli "github.com/docker/docker/cli"
|
|
||||||
"github.com/docker/docker/pkg/jsonmessage"
|
"github.com/docker/docker/pkg/jsonmessage"
|
||||||
flag "github.com/docker/docker/pkg/mflag"
|
|
||||||
"github.com/docker/docker/reference"
|
|
||||||
"github.com/docker/docker/registry"
|
|
||||||
"github.com/docker/engine-api/types"
|
"github.com/docker/engine-api/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdPull pulls an image or a repository from the registry.
|
// ImagePullPrivileged pulls the image and displays it to the output
|
||||||
//
|
func (cli *DockerCli) ImagePullPrivileged(ctx context.Context, authConfig types.AuthConfig, ref string, requestPrivilege types.RequestPrivilegeFunc, all bool) error {
|
||||||
// Usage: docker pull [OPTIONS] IMAGENAME[:TAG|@DIGEST]
|
|
||||||
func (cli *DockerCli) CmdPull(args ...string) error {
|
|
||||||
cmd := Cli.Subcmd("pull", []string{"NAME[:TAG|@DIGEST]"}, Cli.DockerCommands["pull"].Description, true)
|
|
||||||
allTags := cmd.Bool([]string{"a", "-all-tags"}, false, "Download all tagged images in the repository")
|
|
||||||
addTrustedFlags(cmd, true)
|
|
||||||
cmd.Require(flag.Exact, 1)
|
|
||||||
|
|
||||||
cmd.ParseFlags(args, true)
|
|
||||||
remote := cmd.Arg(0)
|
|
||||||
|
|
||||||
distributionRef, err := reference.ParseNamed(remote)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if *allTags && !reference.IsNameOnly(distributionRef) {
|
|
||||||
return errors.New("tag can't be used with --all-tags/-a")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !*allTags && reference.IsNameOnly(distributionRef) {
|
|
||||||
distributionRef = reference.WithDefaultTag(distributionRef)
|
|
||||||
fmt.Fprintf(cli.out, "Using default tag: %s\n", reference.DefaultTag)
|
|
||||||
}
|
|
||||||
|
|
||||||
var tag string
|
|
||||||
switch x := distributionRef.(type) {
|
|
||||||
case reference.Canonical:
|
|
||||||
tag = x.Digest().String()
|
|
||||||
case reference.NamedTagged:
|
|
||||||
tag = x.Tag()
|
|
||||||
}
|
|
||||||
|
|
||||||
registryRef := registry.ParseReference(tag)
|
|
||||||
|
|
||||||
// Resolve the Repository name from fqn to RepositoryInfo
|
|
||||||
repoInfo, err := registry.ParseRepositoryInfo(distributionRef)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
authConfig := cli.ResolveAuthConfig(ctx, repoInfo.Index)
|
|
||||||
requestPrivilege := cli.RegistryAuthenticationPrivilegedFunc(repoInfo.Index, "pull")
|
|
||||||
|
|
||||||
if IsTrusted() && !registryRef.HasDigest() {
|
|
||||||
// Check if tag is digest
|
|
||||||
return cli.trustedPull(ctx, repoInfo, registryRef, authConfig, requestPrivilege)
|
|
||||||
}
|
|
||||||
|
|
||||||
return cli.imagePullPrivileged(ctx, authConfig, distributionRef.String(), requestPrivilege, *allTags)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *DockerCli) imagePullPrivileged(ctx context.Context, authConfig types.AuthConfig, ref string, requestPrivilege types.RequestPrivilegeFunc, all bool) error {
|
|
||||||
|
|
||||||
encodedAuth, err := EncodeAuthToBase64(authConfig)
|
encodedAuth, err := EncodeAuthToBase64(authConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ func addTrustedFlags(fs *flag.FlagSet, verify bool) {
|
||||||
fs.BoolVar(&untrusted, []string{"-disable-content-trust"}, !trusted, message)
|
fs.BoolVar(&untrusted, []string{"-disable-content-trust"}, !trusted, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddTrustedFlags adds the trust flags to a FlagSet
|
// AddTrustedFlags adds content trust flags to the current command flagset
|
||||||
func AddTrustedFlags(fs *pflag.FlagSet, verify bool) {
|
func AddTrustedFlags(fs *pflag.FlagSet, verify bool) {
|
||||||
trusted, message := setupTrustedFlag(verify)
|
trusted, message := setupTrustedFlag(verify)
|
||||||
fs.BoolVar(&untrusted, "disable-content-trust", !trusted, message)
|
fs.BoolVar(&untrusted, "disable-content-trust", !trusted, message)
|
||||||
|
|
@ -314,7 +314,8 @@ func notaryError(repoName string, err error) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *DockerCli) trustedPull(ctx context.Context, repoInfo *registry.RepositoryInfo, ref registry.Reference, authConfig types.AuthConfig, requestPrivilege types.RequestPrivilegeFunc) error {
|
// TrustedPull handles content trust pulling of an image
|
||||||
|
func (cli *DockerCli) TrustedPull(ctx context.Context, repoInfo *registry.RepositoryInfo, ref registry.Reference, authConfig types.AuthConfig, requestPrivilege types.RequestPrivilegeFunc) error {
|
||||||
var refs []target
|
var refs []target
|
||||||
|
|
||||||
notaryRepo, err := cli.getNotaryRepository(repoInfo, authConfig, "pull")
|
notaryRepo, err := cli.getNotaryRepository(repoInfo, authConfig, "pull")
|
||||||
|
|
@ -376,7 +377,7 @@ func (cli *DockerCli) trustedPull(ctx context.Context, repoInfo *registry.Reposi
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := cli.imagePullPrivileged(ctx, authConfig, ref.String(), requestPrivilege, false); err != nil {
|
if err := cli.ImagePullPrivileged(ctx, authConfig, ref.String(), requestPrivilege, false); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
|
||||||
image.NewLoadCommand(dockerCli),
|
image.NewLoadCommand(dockerCli),
|
||||||
image.NewRemoveCommand(dockerCli),
|
image.NewRemoveCommand(dockerCli),
|
||||||
image.NewSaveCommand(dockerCli),
|
image.NewSaveCommand(dockerCli),
|
||||||
|
image.NewPullCommand(dockerCli),
|
||||||
image.NewSearchCommand(dockerCli),
|
image.NewSearchCommand(dockerCli),
|
||||||
image.NewImportCommand(dockerCli),
|
image.NewImportCommand(dockerCli),
|
||||||
image.NewTagCommand(dockerCli),
|
image.NewTagCommand(dockerCli),
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@ var DockerCommandUsage = []Command{
|
||||||
{"login", "Log in to a Docker registry"},
|
{"login", "Log in to a Docker registry"},
|
||||||
{"logout", "Log out from a Docker registry"},
|
{"logout", "Log out from a Docker registry"},
|
||||||
{"ps", "List containers"},
|
{"ps", "List containers"},
|
||||||
{"pull", "Pull an image or a repository from a registry"},
|
|
||||||
{"push", "Push an image or a repository to a registry"},
|
{"push", "Push an image or a repository to a registry"},
|
||||||
{"update", "Update configuration of one or more containers"},
|
{"update", "Update configuration of one or more containers"},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue