github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/images/load.go (about)

     1  package images
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     8  	"os"
     9  
    10  	"github.com/containers/image/v5/docker/reference"
    11  	"github.com/containers/libpod/cmd/podmanV2/parse"
    12  	"github.com/containers/libpod/cmd/podmanV2/registry"
    13  	"github.com/containers/libpod/pkg/domain/entities"
    14  	"github.com/containers/libpod/pkg/util"
    15  	"github.com/pkg/errors"
    16  	"github.com/spf13/cobra"
    17  	"golang.org/x/crypto/ssh/terminal"
    18  )
    19  
    20  var (
    21  	loadDescription = "Loads an image from a locally stored archive (tar file) into container storage."
    22  	loadCommand     = &cobra.Command{
    23  		Use:               "load [flags] [NAME[:TAG]]",
    24  		Short:             "Load an image from container archive",
    25  		Long:              loadDescription,
    26  		RunE:              load,
    27  		Args:              cobra.MaximumNArgs(1),
    28  		PersistentPreRunE: preRunE,
    29  	}
    30  )
    31  
    32  var (
    33  	loadOpts entities.ImageLoadOptions
    34  )
    35  
    36  func init() {
    37  	registry.Commands = append(registry.Commands, registry.CliCommand{
    38  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    39  		Command: loadCommand,
    40  	})
    41  
    42  	loadCommand.SetHelpTemplate(registry.HelpTemplate())
    43  	loadCommand.SetUsageTemplate(registry.UsageTemplate())
    44  	flags := loadCommand.Flags()
    45  	flags.StringVarP(&loadOpts.Input, "input", "i", "", "Read from specified archive file (default: stdin)")
    46  	flags.BoolVarP(&loadOpts.Quiet, "quiet", "q", false, "Suppress the output")
    47  	flags.StringVar(&loadOpts.SignaturePolicy, "signature-policy", "", "Pathname of signature policy file")
    48  	if registry.IsRemote() {
    49  		_ = flags.MarkHidden("signature-policy")
    50  	}
    51  
    52  }
    53  
    54  func load(cmd *cobra.Command, args []string) error {
    55  	if len(args) > 0 {
    56  		ref, err := reference.Parse(args[0])
    57  		if err != nil {
    58  			return err
    59  		}
    60  		if t, ok := ref.(reference.Tagged); ok {
    61  			loadOpts.Tag = t.Tag()
    62  		} else {
    63  			loadOpts.Tag = "latest"
    64  		}
    65  		if r, ok := ref.(reference.Named); ok {
    66  			fmt.Println(r.Name())
    67  			loadOpts.Name = r.Name()
    68  		}
    69  	}
    70  	if len(loadOpts.Input) > 0 {
    71  		if err := parse.ValidateFileName(loadOpts.Input); err != nil {
    72  			return err
    73  		}
    74  	} else {
    75  		if terminal.IsTerminal(int(os.Stdin.Fd())) {
    76  			return errors.Errorf("cannot read from terminal. Use command-line redirection or the --input flag.")
    77  		}
    78  		outFile, err := ioutil.TempFile(util.Tmpdir(), "podman")
    79  		if err != nil {
    80  			return errors.Errorf("error creating file %v", err)
    81  		}
    82  		defer os.Remove(outFile.Name())
    83  		defer outFile.Close()
    84  
    85  		_, err = io.Copy(outFile, os.Stdin)
    86  		if err != nil {
    87  			return errors.Errorf("error copying file %v", err)
    88  		}
    89  		loadOpts.Input = outFile.Name()
    90  	}
    91  	response, err := registry.ImageEngine().Load(context.Background(), loadOpts)
    92  	if err != nil {
    93  		return err
    94  	}
    95  	fmt.Println("Loaded image: " + response.Name)
    96  	return nil
    97  }