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

     1  package images
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/containers/libpod/cmd/podmanV2/parse"
     8  	"github.com/containers/libpod/cmd/podmanV2/registry"
     9  	"github.com/containers/libpod/pkg/domain/entities"
    10  	"github.com/hashicorp/go-multierror"
    11  	"github.com/pkg/errors"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  var (
    16  	importDescription = `Create a container image from the contents of the specified tarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz).
    17  
    18    Note remote tar balls can be specified, via web address.
    19    Optionally tag the image. You can specify the instructions using the --change option.`
    20  	importCommand = &cobra.Command{
    21  		Use:               "import [flags] PATH [REFERENCE]",
    22  		Short:             "Import a tarball to create a filesystem image",
    23  		Long:              importDescription,
    24  		RunE:              importCon,
    25  		PersistentPreRunE: preRunE,
    26  		Example: `podman import http://example.com/ctr.tar url-image
    27    cat ctr.tar | podman -q import --message "importing the ctr.tar tarball" - image-imported
    28    cat ctr.tar | podman import -`,
    29  	}
    30  )
    31  
    32  var (
    33  	importOpts entities.ImageImportOptions
    34  )
    35  
    36  func init() {
    37  	registry.Commands = append(registry.Commands, registry.CliCommand{
    38  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    39  		Command: importCommand,
    40  	})
    41  
    42  	importCommand.SetHelpTemplate(registry.HelpTemplate())
    43  	importCommand.SetUsageTemplate(registry.UsageTemplate())
    44  	flags := importCommand.Flags()
    45  	flags.StringArrayVarP(&importOpts.Changes, "change", "c", []string{}, "Apply the following possible instructions to the created image (default []): CMD | ENTRYPOINT | ENV | EXPOSE | LABEL | STOPSIGNAL | USER | VOLUME | WORKDIR")
    46  	flags.StringVarP(&importOpts.Message, "message", "m", "", "Set commit message for imported image")
    47  	flags.BoolVarP(&importOpts.Quiet, "quiet", "q", false, "Suppress output")
    48  }
    49  
    50  func importCon(cmd *cobra.Command, args []string) error {
    51  	var (
    52  		source    string
    53  		reference string
    54  	)
    55  	switch len(args) {
    56  	case 0:
    57  		return errors.Errorf("need to give the path to the tarball, or must specify a tarball of '-' for stdin")
    58  	case 1:
    59  		source = args[0]
    60  	case 2:
    61  		source = args[0]
    62  		// TODO when save is merged, we need to process reference
    63  		// like it is done in there or we end up with docker.io prepends
    64  		// instead of the localhost ones
    65  		reference = args[1]
    66  	default:
    67  		return errors.Errorf("too many arguments. Usage TARBALL [REFERENCE]")
    68  	}
    69  	errFileName := parse.ValidateFileName(source)
    70  	errURL := parse.ValidURL(source)
    71  	if errURL == nil {
    72  		importOpts.SourceIsURL = true
    73  	}
    74  	if errFileName != nil && errURL != nil {
    75  		return multierror.Append(errFileName, errURL)
    76  	}
    77  
    78  	importOpts.Source = source
    79  	importOpts.Reference = reference
    80  
    81  	response, err := registry.ImageEngine().Import(context.Background(), importOpts)
    82  	if err != nil {
    83  		return err
    84  	}
    85  	fmt.Println(response.Id)
    86  	return nil
    87  }