github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/cli/command/image/pull.go (about) 1 package image 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 "github.com/distribution/reference" 9 "github.com/khulnasoft/cli/cli" 10 "github.com/khulnasoft/cli/cli/command" 11 "github.com/khulnasoft/cli/cli/command/completion" 12 "github.com/khulnasoft/cli/cli/trust" 13 "github.com/pkg/errors" 14 "github.com/spf13/cobra" 15 ) 16 17 // PullOptions defines what and how to pull 18 type PullOptions struct { 19 remote string 20 all bool 21 platform string 22 quiet bool 23 untrusted bool 24 } 25 26 // NewPullCommand creates a new `docker pull` command 27 func NewPullCommand(dockerCli command.Cli) *cobra.Command { 28 var opts PullOptions 29 30 cmd := &cobra.Command{ 31 Use: "pull [OPTIONS] NAME[:TAG|@DIGEST]", 32 Short: "Download an image from a registry", 33 Args: cli.ExactArgs(1), 34 RunE: func(cmd *cobra.Command, args []string) error { 35 opts.remote = args[0] 36 return RunPull(cmd.Context(), dockerCli, opts) 37 }, 38 Annotations: map[string]string{ 39 "category-top": "5", 40 "aliases": "docker image pull, docker pull", 41 }, 42 ValidArgsFunction: completion.NoComplete, 43 } 44 45 flags := cmd.Flags() 46 47 flags.BoolVarP(&opts.all, "all-tags", "a", false, "Download all tagged images in the repository") 48 flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress verbose output") 49 50 command.AddPlatformFlag(flags, &opts.platform) 51 command.AddTrustVerificationFlags(flags, &opts.untrusted, dockerCli.ContentTrustEnabled()) 52 53 return cmd 54 } 55 56 // RunPull performs a pull against the engine based on the specified options 57 func RunPull(ctx context.Context, dockerCLI command.Cli, opts PullOptions) error { 58 distributionRef, err := reference.ParseNormalizedNamed(opts.remote) 59 switch { 60 case err != nil: 61 return err 62 case opts.all && !reference.IsNameOnly(distributionRef): 63 return errors.New("tag can't be used with --all-tags/-a") 64 case !opts.all && reference.IsNameOnly(distributionRef): 65 distributionRef = reference.TagNameOnly(distributionRef) 66 if tagged, ok := distributionRef.(reference.Tagged); ok && !opts.quiet { 67 fmt.Fprintf(dockerCLI.Out(), "Using default tag: %s\n", tagged.Tag()) 68 } 69 } 70 71 imgRefAndAuth, err := trust.GetImageReferencesAndAuth(ctx, AuthResolver(dockerCLI), distributionRef.String()) 72 if err != nil { 73 return err 74 } 75 76 // Check if reference has a digest 77 _, isCanonical := distributionRef.(reference.Canonical) 78 if !opts.untrusted && !isCanonical { 79 err = trustedPull(ctx, dockerCLI, imgRefAndAuth, opts) 80 } else { 81 err = imagePullPrivileged(ctx, dockerCLI, imgRefAndAuth, opts) 82 } 83 if err != nil { 84 if strings.Contains(err.Error(), "when fetching 'plugin'") { 85 return errors.New(err.Error() + " - Use `docker plugin install`") 86 } 87 return err 88 } 89 fmt.Fprintln(dockerCLI.Out(), imgRefAndAuth.Reference().String()) 90 return nil 91 }