This patch deprecates exported image commands and moves the implementation details to an unexported function. Commands that are affected include: - image.NewBuildCommand - image.NewPullCommand - image.NewPushCommand - image.NewImagesCommand - image.NewImageCommand - image.NewHistoryCommand - image.NewImportCommand - image.NewLoadCommand - image.NewRemoveCommand - image.NewSaveCommand - image.NewTagCommand - image.NewPruneCommand Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
109 lines
3.3 KiB
Go
109 lines
3.3 KiB
Go
package image
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/distribution/reference"
|
|
"github.com/docker/cli/cli"
|
|
"github.com/docker/cli/cli/command"
|
|
"github.com/docker/cli/cli/command/completion"
|
|
"github.com/docker/cli/cli/trust"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// PullOptions defines what and how to pull
|
|
type PullOptions = pullOptions
|
|
|
|
// pullOptions defines what and how to pull.
|
|
type pullOptions struct {
|
|
remote string
|
|
all bool
|
|
platform string
|
|
quiet bool
|
|
untrusted bool
|
|
}
|
|
|
|
// NewPullCommand creates a new `docker pull` command
|
|
//
|
|
// Deprecated: Do not import commands directly. They will be removed in a future release.
|
|
func NewPullCommand(dockerCLI command.Cli) *cobra.Command {
|
|
return newPullCommand(dockerCLI)
|
|
}
|
|
|
|
// newPullCommand creates a new `docker pull` command
|
|
func newPullCommand(dockerCLI command.Cli) *cobra.Command {
|
|
var opts pullOptions
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "pull [OPTIONS] NAME[:TAG|@DIGEST]",
|
|
Short: "Download an image from a registry",
|
|
Args: cli.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
opts.remote = args[0]
|
|
return runPull(cmd.Context(), dockerCLI, opts)
|
|
},
|
|
Annotations: map[string]string{
|
|
"category-top": "5",
|
|
"aliases": "docker image pull, docker pull",
|
|
},
|
|
ValidArgsFunction: completion.NoComplete,
|
|
}
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVarP(&opts.all, "all-tags", "a", false, "Download all tagged images in the repository")
|
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress verbose output")
|
|
|
|
addPlatformFlag(flags, &opts.platform)
|
|
flags.BoolVar(&opts.untrusted, "disable-content-trust", !dockerCLI.ContentTrustEnabled(), "Skip image verification")
|
|
|
|
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms)
|
|
|
|
return cmd
|
|
}
|
|
|
|
// RunPull performs a pull against the engine based on the specified options
|
|
func RunPull(ctx context.Context, dockerCLI command.Cli, opts PullOptions) error {
|
|
return runPull(ctx, dockerCLI, opts)
|
|
}
|
|
|
|
// runPull performs a pull against the engine based on the specified options
|
|
func runPull(ctx context.Context, dockerCLI command.Cli, opts pullOptions) error {
|
|
distributionRef, err := reference.ParseNormalizedNamed(opts.remote)
|
|
switch {
|
|
case err != nil:
|
|
return err
|
|
case opts.all && !reference.IsNameOnly(distributionRef):
|
|
return errors.New("tag can't be used with --all-tags/-a")
|
|
case !opts.all && reference.IsNameOnly(distributionRef):
|
|
distributionRef = reference.TagNameOnly(distributionRef)
|
|
if tagged, ok := distributionRef.(reference.Tagged); ok && !opts.quiet {
|
|
_, _ = fmt.Fprintln(dockerCLI.Out(), "Using default tag:", tagged.Tag())
|
|
}
|
|
}
|
|
|
|
imgRefAndAuth, err := trust.GetImageReferencesAndAuth(ctx, AuthResolver(dockerCLI), distributionRef.String())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Check if reference has a digest
|
|
_, isCanonical := distributionRef.(reference.Canonical)
|
|
if !opts.untrusted && !isCanonical {
|
|
err = trustedPull(ctx, dockerCLI, imgRefAndAuth, opts)
|
|
} else {
|
|
err = imagePullPrivileged(ctx, dockerCLI, imgRefAndAuth, opts)
|
|
}
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "when fetching 'plugin'") {
|
|
return errors.New(err.Error() + " - Use `docker plugin install`")
|
|
}
|
|
return err
|
|
}
|
|
_, _ = fmt.Fprintln(dockerCLI.Out(), imgRefAndAuth.Reference().String())
|
|
return nil
|
|
}
|