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

     1  package main
     2  
     3  import (
     4  	"github.com/containers/libpod/cmd/podman/cliconfig"
     5  	"github.com/containers/libpod/pkg/adapter"
     6  	"github.com/opentracing/opentracing-go"
     7  	"github.com/pkg/errors"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  var (
    12  	initCommand     cliconfig.InitValues
    13  	initDescription = `Initialize one or more containers, creating the OCI spec and mounts for inspection. Container names or IDs can be used.`
    14  
    15  	_initCommand = &cobra.Command{
    16  		Use:   "init [flags] CONTAINER [CONTAINER...]",
    17  		Short: "Initialize one or more containers",
    18  		Long:  initDescription,
    19  		RunE: func(cmd *cobra.Command, args []string) error {
    20  			initCommand.InputArgs = args
    21  			initCommand.GlobalFlags = MainGlobalOpts
    22  			initCommand.Remote = remoteclient
    23  			return initCmd(&initCommand)
    24  		},
    25  		Args: func(cmd *cobra.Command, args []string) error {
    26  			return checkAllLatestAndCIDFile(cmd, args, false, false)
    27  		},
    28  		Example: `podman init --latest
    29    podman init 3c45ef19d893
    30    podman init test1`,
    31  	}
    32  )
    33  
    34  func init() {
    35  	initCommand.Command = _initCommand
    36  	initCommand.SetHelpTemplate(HelpTemplate())
    37  	initCommand.SetUsageTemplate(UsageTemplate())
    38  	flags := initCommand.Flags()
    39  	flags.BoolVarP(&initCommand.All, "all", "a", false, "Initialize all containers")
    40  	flags.BoolVarP(&initCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
    41  	markFlagHiddenForRemoteClient("latest", flags)
    42  }
    43  
    44  // initCmd initializes a container
    45  func initCmd(c *cliconfig.InitValues) error {
    46  	if c.Bool("trace") {
    47  		span, _ := opentracing.StartSpanFromContext(Ctx, "initCmd")
    48  		defer span.Finish()
    49  	}
    50  
    51  	ctx := getContext()
    52  
    53  	runtime, err := adapter.GetRuntime(ctx, &c.PodmanCommand)
    54  	if err != nil {
    55  		return errors.Wrapf(err, "could not get runtime")
    56  	}
    57  	defer runtime.DeferredShutdown(false)
    58  
    59  	ok, failures, err := runtime.InitContainers(ctx, c)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	return printCmdResults(ok, failures)
    64  }