github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/image_load.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/spf13/cobra"
    24  )
    25  
    26  func newLoadCommand() *cobra.Command {
    27  	var loadCommand = &cobra.Command{
    28  		Use:           "load",
    29  		Args:          cobra.NoArgs,
    30  		Short:         "Load an image from a tar archive or STDIN",
    31  		Long:          "Supports both Docker Image Spec v1.2 and OCI Image Spec v1.0.",
    32  		RunE:          loadAction,
    33  		SilenceUsage:  true,
    34  		SilenceErrors: true,
    35  	}
    36  
    37  	loadCommand.Flags().StringP("input", "i", "", "Read from tar archive file, instead of STDIN")
    38  
    39  	// #region platform flags
    40  	// platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64"
    41  	loadCommand.Flags().StringSlice("platform", []string{}, "Import content for a specific platform")
    42  	loadCommand.RegisterFlagCompletionFunc("platform", shellCompletePlatforms)
    43  	loadCommand.Flags().Bool("all-platforms", false, "Import content for all platforms")
    44  	// #endregion
    45  
    46  	return loadCommand
    47  }
    48  
    49  func processLoadCommandFlags(cmd *cobra.Command) (types.ImageLoadOptions, error) {
    50  	input, err := cmd.Flags().GetString("input")
    51  	if err != nil {
    52  		return types.ImageLoadOptions{}, err
    53  	}
    54  	globalOptions, err := processRootCmdFlags(cmd)
    55  	if err != nil {
    56  		return types.ImageLoadOptions{}, err
    57  	}
    58  	allPlatforms, err := cmd.Flags().GetBool("all-platforms")
    59  	if err != nil {
    60  		return types.ImageLoadOptions{}, err
    61  	}
    62  	platform, err := cmd.Flags().GetStringSlice("platform")
    63  	if err != nil {
    64  		return types.ImageLoadOptions{}, err
    65  	}
    66  	return types.ImageLoadOptions{
    67  		GOptions:     globalOptions,
    68  		Input:        input,
    69  		Platform:     platform,
    70  		AllPlatforms: allPlatforms,
    71  		Stdout:       cmd.OutOrStdout(),
    72  		Stdin:        cmd.InOrStdin(),
    73  	}, nil
    74  }
    75  
    76  func loadAction(cmd *cobra.Command, _ []string) error {
    77  	options, err := processLoadCommandFlags(cmd)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
    83  	if err != nil {
    84  		return err
    85  	}
    86  	defer cancel()
    87  
    88  	return image.Load(ctx, client, options)
    89  }