github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/pods/pause.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  	podPauseDescription = `The pod name or ID can be used.
    16  
    17    All running containers within each specified pod will then be paused.`
    18  	pauseCommand = &cobra.Command{
    19  		Use:   "pause [flags] POD [POD...]",
    20  		Short: "Pause one or more pods",
    21  		Long:  podPauseDescription,
    22  		RunE:  pause,
    23  		Args: func(cmd *cobra.Command, args []string) error {
    24  			return parse.CheckAllLatestAndCIDFile(cmd, args, false, false)
    25  		},
    26  		Example: `podman pod pause podID1 podID2
    27    podman pod pause --latest
    28    podman pod pause --all`,
    29  	}
    30  )
    31  
    32  var (
    33  	pauseOptions entities.PodPauseOptions
    34  )
    35  
    36  func init() {
    37  	registry.Commands = append(registry.Commands, registry.CliCommand{
    38  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    39  		Command: pauseCommand,
    40  		Parent:  podCmd,
    41  	})
    42  	flags := pauseCommand.Flags()
    43  	flags.BoolVarP(&pauseOptions.All, "all", "a", false, "Pause all running pods")
    44  	flags.BoolVarP(&pauseOptions.Latest, "latest", "l", false, "Act on the latest pod podman is aware of")
    45  	if registry.IsRemote() {
    46  		_ = flags.MarkHidden("latest")
    47  	}
    48  }
    49  func pause(cmd *cobra.Command, args []string) error {
    50  	var (
    51  		errs utils.OutputErrors
    52  	)
    53  	responses, err := registry.ContainerEngine().PodPause(context.Background(), args, pauseOptions)
    54  	if err != nil {
    55  		return err
    56  	}
    57  	// in the cli, first we print out all the successful attempts
    58  	for _, r := range responses {
    59  		if len(r.Errs) == 0 {
    60  			fmt.Println(r.Id)
    61  		} else {
    62  			errs = append(errs, r.Errs...)
    63  		}
    64  	}
    65  	return errs.PrintErrors()
    66  }