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",
|
cli: use custom annotation for aliases
Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.
This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.
As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.
Before this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
...
With this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-28 04:52:25 -04:00
|
|
|
"aliases": "docker image pull, docker pull",
|
2022-03-30 03:37:08 -04:00
|
|
|
},
|
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()
|
2023-03-15 11:21:02 -04:00
|
|
|
imgRefAndAuth, err := trust.GetImageReferencesAndAuth(ctx, 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
|
|
|
|
}
|