2016-09-08 13:11:39 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-05-12 07:18:48 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
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"
|
|
|
|
)
|
|
|
|
|
2018-04-27 15:11:42 -04:00
|
|
|
// PullOptions defines what and how to pull
|
|
|
|
type PullOptions struct {
|
2018-03-08 08:35:17 -05:00
|
|
|
remote string
|
|
|
|
all bool
|
|
|
|
platform string
|
2018-12-19 07:48:41 -05:00
|
|
|
quiet bool
|
2018-03-08 08:35:17 -05:00
|
|
|
untrusted bool
|
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 {
|
2018-04-27 15:11:42 -04:00
|
|
|
var opts PullOptions
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "pull [OPTIONS] NAME[:TAG|@DIGEST]",
|
2022-03-29 18:11:09 -04:00
|
|
|
Short: "Download an image from a registry",
|
2016-09-08 13:11:39 -04:00
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.remote = args[0]
|
2018-04-27 15:11:42 -04:00
|
|
|
return RunPull(dockerCli, opts)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2022-03-30 03:37:08 -04:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"category-top": "5",
|
|
|
|
},
|
2022-05-12 07:18:48 -04:00
|
|
|
ValidArgsFunction: completion.NoComplete,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
flags.BoolVarP(&opts.all, "all-tags", "a", false, "Download all tagged images in the repository")
|
2018-12-19 07:48:41 -05:00
|
|
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress verbose output")
|
2017-08-02 20:31:32 -04:00
|
|
|
|
|
|
|
command.AddPlatformFlag(flags, &opts.platform)
|
2018-03-08 14:56:56 -05:00
|
|
|
command.AddTrustVerificationFlags(flags, &opts.untrusted, dockerCli.ContentTrustEnabled())
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2018-04-27 15:11:42 -04:00
|
|
|
// RunPull performs a pull against the engine based on the specified options
|
|
|
|
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)
|
2018-12-19 07:48:41 -05:00
|
|
|
if tagged, ok := distributionRef.(reference.Tagged); ok && !opts.quiet {
|
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()
|
2018-03-06 05:15:18 -05:00
|
|
|
imgRefAndAuth, err := trust.GetImageReferencesAndAuth(ctx, nil, AuthResolver(cli), distributionRef.String())
|
2017-10-10 15:57:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-06 14:27:27 -05:00
|
|
|
// Check if reference has a digest
|
|
|
|
_, isCanonical := distributionRef.(reference.Canonical)
|
2018-03-08 14:56:56 -05:00
|
|
|
if !opts.untrusted && !isCanonical {
|
2018-12-19 07:48:41 -05:00
|
|
|
err = trustedPull(ctx, cli, imgRefAndAuth, opts)
|
2016-09-08 13:11:39 -04:00
|
|
|
} else {
|
2018-12-19 07:48:41 -05:00
|
|
|
err = imagePullPrivileged(ctx, cli, imgRefAndAuth, opts)
|
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
|
|
|
|
}
|
2018-12-19 07:48:41 -05:00
|
|
|
fmt.Fprintln(cli.Out(), imgRefAndAuth.Reference().String())
|
2016-09-08 13:11:39 -04:00
|
|
|
return nil
|
|
|
|
}
|