2016-09-08 13:11:39 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-09-26 12:53:21 -04:00
|
|
|
"github.com/docker/cli/cli/trust"
|
2017-01-11 16:54:52 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2017-03-27 21:21:59 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
2017-03-27 21:21:59 -04:00
|
|
|
"golang.org/x/net/context"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type pullOptions struct {
|
|
|
|
remote string
|
|
|
|
all bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPullCommand creates a new `docker pull` command
|
2017-03-30 20:21:14 -04:00
|
|
|
func NewPullCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-08 13:11:39 -04:00
|
|
|
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")
|
2017-01-17 09:46:07 -05:00
|
|
|
command.AddTrustVerificationFlags(flags)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-09-26 12:53:21 -04:00
|
|
|
func runPull(cli command.Cli, opts pullOptions) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
imgRefAndAuth, err := trust.GetImageReferencesAndAuth(ctx, AuthResolver(cli), opts.remote)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-26 12:53:21 -04:00
|
|
|
|
|
|
|
distributionRef := imgRefAndAuth.Reference()
|
2016-09-08 13:11:39 -04:00
|
|
|
if opts.all && !reference.IsNameOnly(distributionRef) {
|
|
|
|
return errors.New("tag can't be used with --all-tags/-a")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !opts.all && reference.IsNameOnly(distributionRef) {
|
2017-01-25 19:54:18 -05:00
|
|
|
distributionRef = reference.TagNameOnly(distributionRef)
|
|
|
|
if tagged, ok := distributionRef.(reference.Tagged); ok {
|
2017-09-26 12:53:21 -04:00
|
|
|
fmt.Fprintf(cli.Out(), "Using default tag: %s\n", tagged.Tag())
|
2017-01-25 19:54:18 -05:00
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2016-12-06 14:27:27 -05:00
|
|
|
// Check if reference has a digest
|
|
|
|
_, isCanonical := distributionRef.(reference.Canonical)
|
|
|
|
if command.IsTrusted() && !isCanonical {
|
2017-09-26 12:53:21 -04:00
|
|
|
err = trustedPull(ctx, cli, imgRefAndAuth)
|
2016-09-08 13:11:39 -04:00
|
|
|
} else {
|
2017-09-26 12:53:21 -04:00
|
|
|
err = imagePullPrivileged(ctx, cli, imgRefAndAuth, opts.all)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
2017-01-10 22:27:55 -05:00
|
|
|
if strings.Contains(err.Error(), "when fetching 'plugin'") {
|
2016-09-08 13:11:39 -04:00
|
|
|
return errors.New(err.Error() + " - Use `docker plugin install`")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|