github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/image_save.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  	"fmt"
    21  	"os"
    22  
    23  	"github.com/containerd/nerdctl/pkg/api/types"
    24  	"github.com/containerd/nerdctl/pkg/clientutil"
    25  	"github.com/containerd/nerdctl/pkg/cmd/image"
    26  	"github.com/mattn/go-isatty"
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  func newSaveCommand() *cobra.Command {
    31  	var saveCommand = &cobra.Command{
    32  		Use:               "save",
    33  		Args:              cobra.MinimumNArgs(1),
    34  		Short:             "Save one or more images to a tar archive (streamed to STDOUT by default)",
    35  		Long:              "The archive implements both Docker Image Spec v1.2 and OCI Image Spec v1.0.",
    36  		RunE:              saveAction,
    37  		ValidArgsFunction: saveShellComplete,
    38  		SilenceUsage:      true,
    39  		SilenceErrors:     true,
    40  	}
    41  	saveCommand.Flags().StringP("output", "o", "", "Write to a file, instead of STDOUT")
    42  
    43  	// #region platform flags
    44  	// platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64"
    45  	saveCommand.Flags().StringSlice("platform", []string{}, "Export content for a specific platform")
    46  	saveCommand.RegisterFlagCompletionFunc("platform", shellCompletePlatforms)
    47  	saveCommand.Flags().Bool("all-platforms", false, "Export content for all platforms")
    48  	// #endregion
    49  
    50  	return saveCommand
    51  }
    52  
    53  func processImageSaveOptions(cmd *cobra.Command) (types.ImageSaveOptions, error) {
    54  	globalOptions, err := processRootCmdFlags(cmd)
    55  	if err != nil {
    56  		return types.ImageSaveOptions{}, err
    57  	}
    58  
    59  	allPlatforms, err := cmd.Flags().GetBool("all-platforms")
    60  	if err != nil {
    61  		return types.ImageSaveOptions{}, err
    62  	}
    63  	platform, err := cmd.Flags().GetStringSlice("platform")
    64  	if err != nil {
    65  		return types.ImageSaveOptions{}, err
    66  	}
    67  
    68  	return types.ImageSaveOptions{
    69  		GOptions:     globalOptions,
    70  		AllPlatforms: allPlatforms,
    71  		Platform:     platform,
    72  	}, err
    73  }
    74  
    75  func saveAction(cmd *cobra.Command, args []string) error {
    76  	options, err := processImageSaveOptions(cmd)
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	output := cmd.OutOrStdout()
    82  	outputPath, err := cmd.Flags().GetString("output")
    83  	if err != nil {
    84  		return err
    85  	} else if outputPath != "" {
    86  		f, err := os.OpenFile(outputPath, os.O_CREATE|os.O_WRONLY, 0644)
    87  		if err != nil {
    88  			return err
    89  		}
    90  		output = f
    91  		defer f.Close()
    92  	} else if out, ok := output.(*os.File); ok && isatty.IsTerminal(out.Fd()) {
    93  		return fmt.Errorf("cowardly refusing to save to a terminal. Use the -o flag or redirect")
    94  	}
    95  	options.Stdout = output
    96  
    97  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
    98  	if err != nil {
    99  		return err
   100  	}
   101  	defer cancel()
   102  
   103  	if err = image.Save(ctx, client, args, options); err != nil && outputPath != "" {
   104  		os.Remove(outputPath)
   105  	}
   106  	return err
   107  }
   108  
   109  func saveShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
   110  	// show image names
   111  	return shellCompleteImageNames(cmd)
   112  }