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 {
|
2017-08-02 20:31:32 -04:00
|
|
|
remote string
|
|
|
|
all bool
|
|
|
|
platform string
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-08-02 20:31:32 -04:00
|
|
|
|
|
|
|
command.AddPlatformFlag(flags, &opts.platform)
|
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 {
|
2017-10-10 15:57:16 -04:00
|
|
|
distributionRef, err := reference.ParseNormalizedNamed(opts.remote)
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
2016-09-08 13:11:39 -04:00
|
|
|
return err
|
2017-10-10 15:57:16 -04:00
|
|
|
case opts.all && !reference.IsNameOnly(distributionRef):
|
2016-09-08 13:11:39 -04:00
|
|
|
return errors.New("tag can't be used with --all-tags/-a")
|
2017-10-10 15:57:16 -04:00
|
|
|
case !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
|
|
|
}
|
|
|
|
|
2017-10-10 15:57:16 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
imgRefAndAuth, err := trust.GetImageReferencesAndAuth(ctx, AuthResolver(cli), distributionRef.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-06 14:27:27 -05:00
|
|
|
// Check if reference has a digest
|
|
|
|
_, isCanonical := distributionRef.(reference.Canonical)
|
|
|
|
if command.IsTrusted() && !isCanonical {
|
2017-08-02 20:31:32 -04:00
|
|
|
err = trustedPull(ctx, cli, imgRefAndAuth, opts.platform)
|
2016-09-08 13:11:39 -04:00
|
|
|
} else {
|
2017-08-02 20:31:32 -04:00
|
|
|
err = imagePullPrivileged(ctx, cli, imgRefAndAuth, opts.all, opts.platform)
|
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
|
|
|
|
}
|