github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/import.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/containers/libpod/cmd/podman/cliconfig"
     7  	"github.com/containers/libpod/cmd/podman/shared/parse"
     8  	"github.com/containers/libpod/pkg/adapter"
     9  	"github.com/hashicorp/go-multierror"
    10  	"github.com/pkg/errors"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  var (
    15  	importCommand cliconfig.ImportValues
    16  
    17  	importDescription = `Create a container image from the contents of the specified tarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz).
    18  
    19    Note remote tar balls can be specified, via web address.
    20    Optionally tag the image. You can specify the instructions using the --change option.`
    21  	_importCommand = &cobra.Command{
    22  		Use:   "import [flags] PATH [REFERENCE]",
    23  		Short: "Import a tarball to create a filesystem image",
    24  		Long:  importDescription,
    25  		RunE: func(cmd *cobra.Command, args []string) error {
    26  			importCommand.InputArgs = args
    27  			importCommand.GlobalFlags = MainGlobalOpts
    28  			importCommand.Remote = remoteclient
    29  			return importCmd(&importCommand)
    30  		},
    31  		Example: `podman import http://example.com/ctr.tar url-image
    32    cat ctr.tar | podman -q import --message "importing the ctr.tar tarball" - image-imported
    33    cat ctr.tar | podman import -`,
    34  	}
    35  )
    36  
    37  func init() {
    38  	importCommand.Command = _importCommand
    39  	importCommand.SetHelpTemplate(HelpTemplate())
    40  	importCommand.SetUsageTemplate(UsageTemplate())
    41  	flags := importCommand.Flags()
    42  	flags.StringArrayVarP(&importCommand.Change, "change", "c", []string{}, "Apply the following possible instructions to the created image (default []): CMD | ENTRYPOINT | ENV | EXPOSE | LABEL | STOPSIGNAL | USER | VOLUME | WORKDIR")
    43  	flags.StringVarP(&importCommand.Message, "message", "m", "", "Set commit message for imported image")
    44  	flags.BoolVarP(&importCommand.Quiet, "quiet", "q", false, "Suppress output")
    45  
    46  }
    47  
    48  func importCmd(c *cliconfig.ImportValues) error {
    49  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    50  	if err != nil {
    51  		return errors.Wrapf(err, "could not get runtime")
    52  	}
    53  	defer runtime.DeferredShutdown(false)
    54  
    55  	var (
    56  		source    string
    57  		reference string
    58  	)
    59  	args := c.InputArgs
    60  	switch len(args) {
    61  	case 0:
    62  		return errors.Errorf("need to give the path to the tarball, or must specify a tarball of '-' for stdin")
    63  	case 1:
    64  		source = args[0]
    65  	case 2:
    66  		source = args[0]
    67  		reference = args[1]
    68  	default:
    69  		return errors.Errorf("too many arguments. Usage TARBALL [REFERENCE]")
    70  	}
    71  
    72  	errFileName := parse.ValidateFileName(source)
    73  	errURL := parse.ValidURL(source)
    74  
    75  	if errFileName != nil && errURL != nil {
    76  		return multierror.Append(errFileName, errURL)
    77  	}
    78  
    79  	quiet := c.Quiet
    80  	if runtime.Remote {
    81  		quiet = false
    82  	}
    83  	iid, err := runtime.Import(getContext(), source, reference, importCommand.Change, c.String("message"), quiet)
    84  	if err == nil {
    85  		fmt.Println(iid)
    86  	}
    87  	return err
    88  }