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

     1  package main
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/containers/libpod/cmd/podman/cliconfig"
     7  	"github.com/containers/libpod/pkg/adapter"
     8  	"github.com/pkg/errors"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  var (
    13  	waitCommand cliconfig.WaitValues
    14  
    15  	waitDescription = `Block until one or more containers stop and then print their exit codes.
    16  `
    17  	_waitCommand = &cobra.Command{
    18  		Use:   "wait [flags] CONTAINER [CONTAINER...]",
    19  		Short: "Block on one or more containers",
    20  		Long:  waitDescription,
    21  		RunE: func(cmd *cobra.Command, args []string) error {
    22  			waitCommand.InputArgs = args
    23  			waitCommand.GlobalFlags = MainGlobalOpts
    24  			waitCommand.Remote = remoteclient
    25  			return waitCmd(&waitCommand)
    26  		},
    27  		Example: `podman wait --latest
    28    podman wait --interval 5000 ctrID
    29    podman wait ctrID1 ctrID2`,
    30  	}
    31  )
    32  
    33  func init() {
    34  	waitCommand.Command = _waitCommand
    35  	waitCommand.SetHelpTemplate(HelpTemplate())
    36  	waitCommand.SetUsageTemplate(UsageTemplate())
    37  	flags := waitCommand.Flags()
    38  	flags.UintVarP(&waitCommand.Interval, "interval", "i", 250, "Milliseconds to wait before polling for completion")
    39  	flags.BoolVarP(&waitCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
    40  	markFlagHiddenForRemoteClient("latest", flags)
    41  }
    42  
    43  func waitCmd(c *cliconfig.WaitValues) error {
    44  	args := c.InputArgs
    45  	if len(args) < 1 && !c.Latest {
    46  		return errors.Errorf("you must provide at least one container name or id")
    47  	}
    48  
    49  	if c.Interval == 0 {
    50  		return errors.Errorf("interval must be greater then 0")
    51  	}
    52  	interval := time.Duration(c.Interval) * time.Millisecond
    53  
    54  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    55  	if err != nil {
    56  		return errors.Wrapf(err, "error creating runtime")
    57  	}
    58  	defer runtime.DeferredShutdown(false)
    59  
    60  	ok, failures, err := runtime.WaitOnContainers(getContext(), c, interval)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	return printCmdResults(ok, failures)
    65  }