github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/load.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io" 6 "io/ioutil" 7 "os" 8 "strings" 9 10 "github.com/containers/libpod/cmd/podman/cliconfig" 11 "github.com/containers/libpod/cmd/podman/shared/parse" 12 "github.com/containers/libpod/pkg/adapter" 13 "github.com/containers/libpod/pkg/util" 14 "github.com/pkg/errors" 15 "github.com/spf13/cobra" 16 "golang.org/x/crypto/ssh/terminal" 17 ) 18 19 var ( 20 loadCommand cliconfig.LoadValues 21 22 loadDescription = "Loads an image from a locally stored archive (tar file) into container storage." 23 24 _loadCommand = &cobra.Command{ 25 Use: "load [flags] [NAME[:TAG]]", 26 Short: "Load an image from container archive", 27 Long: loadDescription, 28 RunE: func(cmd *cobra.Command, args []string) error { 29 loadCommand.InputArgs = args 30 loadCommand.GlobalFlags = MainGlobalOpts 31 loadCommand.Remote = remoteclient 32 return loadCmd(&loadCommand) 33 }, 34 } 35 ) 36 37 func init() { 38 loadCommand.Command = _loadCommand 39 loadCommand.SetHelpTemplate(HelpTemplate()) 40 loadCommand.SetUsageTemplate(UsageTemplate()) 41 flags := loadCommand.Flags() 42 flags.StringVarP(&loadCommand.Input, "input", "i", "", "Read from specified archive file (default: stdin)") 43 flags.BoolVarP(&loadCommand.Quiet, "quiet", "q", false, "Suppress the output") 44 // Disabled flags for the remote client 45 if !remote { 46 flags.StringVar(&loadCommand.SignaturePolicy, "signature-policy", "", "Pathname of signature policy file (not usually used)") 47 markFlagHidden(flags, "signature-policy") 48 } 49 } 50 51 // loadCmd gets the image/file to be loaded from the command line 52 // and calls loadImage to load the image to containers-storage 53 func loadCmd(c *cliconfig.LoadValues) error { 54 55 args := c.InputArgs 56 var imageName string 57 58 if len(args) == 1 { 59 imageName = args[0] 60 } 61 if len(args) > 1 { 62 return errors.New("too many arguments. Requires exactly 1") 63 } 64 65 runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand) 66 if err != nil { 67 return errors.Wrapf(err, "could not get runtime") 68 } 69 defer runtime.DeferredShutdown(false) 70 71 if len(c.Input) > 0 { 72 if err := parse.ValidateFileName(c.Input); err != nil { 73 return err 74 } 75 } else { 76 if terminal.IsTerminal(int(os.Stdin.Fd())) { 77 return errors.Errorf("cannot read from terminal. Use command-line redirection or the --input flag.") 78 } 79 outFile, err := ioutil.TempFile(util.Tmpdir(), "podman") 80 if err != nil { 81 return errors.Errorf("error creating file %v", err) 82 } 83 defer os.Remove(outFile.Name()) 84 defer outFile.Close() 85 86 _, err = io.Copy(outFile, os.Stdin) 87 if err != nil { 88 return errors.Errorf("error copying file %v", err) 89 } 90 91 c.Input = outFile.Name() 92 } 93 94 names, err := runtime.LoadImage(getContext(), imageName, c) 95 if err != nil { 96 return err 97 } 98 if len(imageName) > 0 { 99 split := strings.Split(names, ",") 100 newImage, err := runtime.NewImageFromLocal(split[0]) 101 if err != nil { 102 return err 103 } 104 if err := newImage.TagImage(imageName); err != nil { 105 return errors.Wrapf(err, "error adding '%s' to image %q", imageName, newImage.InputName) 106 } 107 } 108 fmt.Println("Loaded image(s): " + names) 109 return nil 110 }