github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/image_pull.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"github.com/containerd/nerdctl/pkg/api/types"
    21  	"github.com/containerd/nerdctl/pkg/clientutil"
    22  	"github.com/containerd/nerdctl/pkg/cmd/image"
    23  	"github.com/containerd/nerdctl/pkg/imgutil"
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  func newPullCommand() *cobra.Command {
    28  	var pullCommand = &cobra.Command{
    29  		Use:           "pull [flags] NAME[:TAG]",
    30  		Short:         "Pull an image from a registry. Optionally specify \"ipfs://\" or \"ipns://\" scheme to pull image from IPFS.",
    31  		Args:          IsExactArgs(1),
    32  		RunE:          pullAction,
    33  		SilenceUsage:  true,
    34  		SilenceErrors: true,
    35  	}
    36  	pullCommand.Flags().String("unpack", "auto", "Unpack the image for the current single platform (auto/true/false)")
    37  	pullCommand.RegisterFlagCompletionFunc("unpack", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    38  		return []string{"auto", "true", "false"}, cobra.ShellCompDirectiveNoFileComp
    39  	})
    40  
    41  	// #region platform flags
    42  	// platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64"
    43  	pullCommand.Flags().StringSlice("platform", nil, "Pull content for a specific platform")
    44  	pullCommand.RegisterFlagCompletionFunc("platform", shellCompletePlatforms)
    45  	pullCommand.Flags().Bool("all-platforms", false, "Pull content for all platforms")
    46  	// #endregion
    47  
    48  	// #region verify flags
    49  	pullCommand.Flags().String("verify", "none", "Verify the image (none|cosign|notation)")
    50  	pullCommand.RegisterFlagCompletionFunc("verify", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    51  		return []string{"none", "cosign", "notation"}, cobra.ShellCompDirectiveNoFileComp
    52  	})
    53  	pullCommand.Flags().String("cosign-key", "", "Path to the public key file, KMS, URI or Kubernetes Secret for --verify=cosign")
    54  	pullCommand.Flags().String("cosign-certificate-identity", "", "The identity expected in a valid Fulcio certificate for --verify=cosign. Valid values include email address, DNS names, IP addresses, and URIs. Either --cosign-certificate-identity or --cosign-certificate-identity-regexp must be set for keyless flows")
    55  	pullCommand.Flags().String("cosign-certificate-identity-regexp", "", "A regular expression alternative to --cosign-certificate-identity for --verify=cosign. Accepts the Go regular expression syntax described at https://golang.org/s/re2syntax. Either --cosign-certificate-identity or --cosign-certificate-identity-regexp must be set for keyless flows")
    56  	pullCommand.Flags().String("cosign-certificate-oidc-issuer", "", "The OIDC issuer expected in a valid Fulcio certificate for --verify=cosign,, e.g. https://token.actions.githubusercontent.com or https://oauth2.sigstore.dev/auth. Either --cosign-certificate-oidc-issuer or --cosign-certificate-oidc-issuer-regexp must be set for keyless flows")
    57  	pullCommand.Flags().String("cosign-certificate-oidc-issuer-regexp", "", "A regular expression alternative to --certificate-oidc-issuer for --verify=cosign,. Accepts the Go regular expression syntax described at https://golang.org/s/re2syntax. Either --cosign-certificate-oidc-issuer or --cosign-certificate-oidc-issuer-regexp must be set for keyless flows")
    58  	// #endregion
    59  
    60  	// #region socipull flags
    61  	pullCommand.Flags().String("soci-index-digest", "", "Specify a particular index digest for SOCI. If left empty, SOCI will automatically use the index determined by the selection policy.")
    62  	// #endregion
    63  
    64  	pullCommand.Flags().BoolP("quiet", "q", false, "Suppress verbose output")
    65  
    66  	pullCommand.Flags().String("ipfs-address", "", "multiaddr of IPFS API (default uses $IPFS_PATH env variable if defined or local directory ~/.ipfs)")
    67  
    68  	return pullCommand
    69  }
    70  
    71  func processPullCommandFlags(cmd *cobra.Command) (types.ImagePullOptions, error) {
    72  	globalOptions, err := processRootCmdFlags(cmd)
    73  	if err != nil {
    74  		return types.ImagePullOptions{}, err
    75  	}
    76  	allPlatforms, err := cmd.Flags().GetBool("all-platforms")
    77  	if err != nil {
    78  		return types.ImagePullOptions{}, err
    79  	}
    80  	platform, err := cmd.Flags().GetStringSlice("platform")
    81  	if err != nil {
    82  		return types.ImagePullOptions{}, err
    83  	}
    84  
    85  	unpackStr, err := cmd.Flags().GetString("unpack")
    86  	if err != nil {
    87  		return types.ImagePullOptions{}, err
    88  	}
    89  
    90  	quiet, err := cmd.Flags().GetBool("quiet")
    91  	if err != nil {
    92  		return types.ImagePullOptions{}, err
    93  	}
    94  	ipfsAddressStr, err := cmd.Flags().GetString("ipfs-address")
    95  	if err != nil {
    96  		return types.ImagePullOptions{}, err
    97  	}
    98  
    99  	sociIndexDigest, err := cmd.Flags().GetString("soci-index-digest")
   100  	if err != nil {
   101  		return types.ImagePullOptions{}, err
   102  	}
   103  
   104  	verifyOptions, err := processImageVerifyOptions(cmd)
   105  	if err != nil {
   106  		return types.ImagePullOptions{}, err
   107  	}
   108  	return types.ImagePullOptions{
   109  		GOptions:      globalOptions,
   110  		VerifyOptions: verifyOptions,
   111  		AllPlatforms:  allPlatforms,
   112  		Platform:      platform,
   113  		Unpack:        unpackStr,
   114  		Quiet:         quiet,
   115  		IPFSAddress:   ipfsAddressStr,
   116  		RFlags: imgutil.RemoteSnapshotterFlags{
   117  			SociIndexDigest: sociIndexDigest,
   118  		},
   119  		Stdout: cmd.OutOrStdout(),
   120  		Stderr: cmd.OutOrStderr(),
   121  	}, nil
   122  }
   123  
   124  func pullAction(cmd *cobra.Command, args []string) error {
   125  	options, err := processPullCommandFlags(cmd)
   126  	if err != nil {
   127  		return err
   128  	}
   129  
   130  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
   131  	if err != nil {
   132  		return err
   133  	}
   134  	defer cancel()
   135  
   136  	return image.Pull(ctx, client, args[0], options)
   137  }