summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/podman/wait.go15
-rw-r--r--completions/bash/podman6
-rw-r--r--docs/podman-wait.1.md6
-rw-r--r--libpod/runtime_ctr.go5
-rw-r--r--test/podman_wait.bats7
5 files changed, 34 insertions, 5 deletions
diff --git a/cmd/podman/wait.go b/cmd/podman/wait.go
index 27cfecac9..2b2a07738 100644
--- a/cmd/podman/wait.go
+++ b/cmd/podman/wait.go
@@ -14,11 +14,12 @@ var (
Block until one or more containers stop and then print their exit codes
`
-
+ waitFlags = []cli.Flag{LatestFlag}
waitCommand = cli.Command{
Name: "wait",
Usage: "Block on one or more containers",
Description: waitDescription,
+ Flags: waitFlags,
Action: waitCmd,
ArgsUsage: "CONTAINER-NAME [CONTAINER-NAME ...]",
}
@@ -26,7 +27,7 @@ var (
func waitCmd(c *cli.Context) error {
args := c.Args()
- if len(args) < 1 {
+ if len(args) < 1 && !c.Bool("latest") {
return errors.Errorf("you must provide at least one container name or id")
}
@@ -41,7 +42,15 @@ func waitCmd(c *cli.Context) error {
}
var lastError error
- for _, container := range c.Args() {
+ if c.Bool("latest") {
+ latestCtr, err := runtime.GetLatestContainer()
+ if err != nil {
+ return errors.Wrapf(err, "unable to wait on latest container")
+ }
+ args = append(args, latestCtr.ID())
+ }
+
+ for _, container := range args {
ctr, err := runtime.LookupContainer(container)
if err != nil {
return errors.Wrapf(err, "unable to find container %s", container)
diff --git a/completions/bash/podman b/completions/bash/podman
index a552f19d1..8447ab436 100644
--- a/completions/bash/podman
+++ b/completions/bash/podman
@@ -1469,7 +1469,11 @@ _podman_unpause() {
_podman_wait() {
local options_with_args=""
- local boolean_options="--help -h"
+ local boolean_options="
+ --help
+ -h
+ -l
+ --latest"
_complete_ "$options_with_args" "$boolean_options"
}
diff --git a/docs/podman-wait.1.md b/docs/podman-wait.1.md
index 5bdefbd5f..31735b7b5 100644
--- a/docs/podman-wait.1.md
+++ b/docs/podman-wait.1.md
@@ -21,10 +21,16 @@ After the container stops, the container's return code is printed.
**--help, -h**
Print usage statement
+**--latest, -l**
+ Instead of providing the container name or ID, use the last created container. If you use methods other than Podman
+to run containers such as CRI-O, the last started container could be from either of those methods.
+
## EXAMPLES
podman wait mywebserver
+ podman wait --latest
+
podman wait 860a4b23
podman wait mywebserver myftpserver
diff --git a/libpod/runtime_ctr.go b/libpod/runtime_ctr.go
index 0f39ead35..66dcb2f95 100644
--- a/libpod/runtime_ctr.go
+++ b/libpod/runtime_ctr.go
@@ -271,12 +271,15 @@ func (r *Runtime) GetContainersByList(containers []string) ([]*Container, error)
// GetLatestContainer returns a container object of the latest created container.
func (r *Runtime) GetLatestContainer() (*Container, error) {
- var lastCreatedIndex int
+ lastCreatedIndex := -1
var lastCreatedTime time.Time
ctrs, err := r.GetAllContainers()
if err != nil {
return nil, errors.Wrapf(err, "unable to find latest container")
}
+ if len(ctrs) == 0 {
+ return nil, ErrNoSuchCtr
+ }
for containerIndex, ctr := range ctrs {
createdTime := ctr.config.CreatedTime
if createdTime.After(lastCreatedTime) {
diff --git a/test/podman_wait.bats b/test/podman_wait.bats
index 3109ce91e..9d6c22734 100644
--- a/test/podman_wait.bats
+++ b/test/podman_wait.bats
@@ -34,3 +34,10 @@ function teardown() {
run bash -c ${PODMAN_BINARY} ${PODMAN_OPTIONS} wait $ctr_id
[ "$status" -eq 0 ]
}
+
+@test "wait on the latest container" {
+ ${PODMAN_BINARY} ${PODMAN_OPTIONS} run -d ${ALPINE} sleep 5
+ run bash -c ${PODMAN_BINARY} ${PODMAN_OPTIONS} wait -l
+ echo "$output"
+ [ "$status" -eq 0 ]
+}