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

     1  package pods
     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/cmd/podmanV2/utils"
    10  	"github.com/containers/libpod/pkg/domain/entities"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  var (
    15  	podStartDescription = `The pod name or ID can be used.
    16  
    17    All containers defined in the pod will be started.`
    18  	startCommand = &cobra.Command{
    19  		Use:   "start [flags] POD [POD...]",
    20  		Short: "Start one or more pods",
    21  		Long:  podStartDescription,
    22  		RunE:  start,
    23  		Args: func(cmd *cobra.Command, args []string) error {
    24  			return parse.CheckAllLatestAndCIDFile(cmd, args, false, false)
    25  		},
    26  		Example: `podman pod start podID
    27    podman pod start --latest
    28    podman pod start --all`,
    29  	}
    30  )
    31  
    32  var (
    33  	startOptions = entities.PodStartOptions{}
    34  )
    35  
    36  func init() {
    37  	registry.Commands = append(registry.Commands, registry.CliCommand{
    38  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    39  		Command: startCommand,
    40  		Parent:  podCmd,
    41  	})
    42  
    43  	flags := startCommand.Flags()
    44  	flags.BoolVarP(&startOptions.All, "all", "a", false, "Restart all running pods")
    45  	flags.BoolVarP(&startOptions.Latest, "latest", "l", false, "Restart the latest pod podman is aware of")
    46  	if registry.IsRemote() {
    47  		_ = flags.MarkHidden("latest")
    48  	}
    49  }
    50  
    51  func start(cmd *cobra.Command, args []string) error {
    52  	var (
    53  		errs utils.OutputErrors
    54  	)
    55  	responses, err := registry.ContainerEngine().PodStart(context.Background(), args, startOptions)
    56  	if err != nil {
    57  		return err
    58  	}
    59  	// in the cli, first we print out all the successful attempts
    60  	for _, r := range responses {
    61  		if len(r.Errs) == 0 {
    62  			fmt.Println(r.Id)
    63  		} else {
    64  			errs = append(errs, r.Errs...)
    65  		}
    66  	}
    67  	return errs.PrintErrors()
    68  }