summaryrefslogtreecommitdiff
path: root/libpod
diff options
context:
space:
mode:
authorbaude <bbaude@redhat.com>2018-01-02 16:29:43 -0600
committerAtomic Bot <atomic-devel@projectatomic.io>2018-01-08 19:12:17 +0000
commit7b08aa78e4ede4c54fda6cd9917bb62e18d0d634 (patch)
tree1c480d9dfb8db3268dc3fdcca827792278a3ae47 /libpod
parent6baf6e461de6e560cc48d35239e7c392bec4481c (diff)
downloadpodman-7b08aa78e4ede4c54fda6cd9917bb62e18d0d634.tar.gz
podman-7b08aa78e4ede4c54fda6cd9917bb62e18d0d634.tar.bz2
podman-7b08aa78e4ede4c54fda6cd9917bb62e18d0d634.zip
Shortcut for most recent container
It is desirable to have a shortcut for the most recently created container. We can now use "**latest" to represent the most recent container instead of its container ID or name. For example: Signed-off-by: baude <bbaude@redhat.com> Closes: #179 Approved by: baude
Diffstat (limited to 'libpod')
-rw-r--r--libpod/runtime_ctr.go20
1 files changed, 19 insertions, 1 deletions
diff --git a/libpod/runtime_ctr.go b/libpod/runtime_ctr.go
index 914d457a0..0f39ead35 100644
--- a/libpod/runtime_ctr.go
+++ b/libpod/runtime_ctr.go
@@ -3,6 +3,7 @@ package libpod
import (
"os"
"path/filepath"
+ "time"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
@@ -204,7 +205,6 @@ func (r *Runtime) LookupContainer(idOrName string) (*Container, error) {
if !r.valid {
return nil, ErrRuntimeStopped
}
-
return r.state.LookupContainer(idOrName)
}
@@ -268,3 +268,21 @@ func (r *Runtime) GetContainersByList(containers []string) ([]*Container, error)
}
return ctrs, nil
}
+
+// GetLatestContainer returns a container object of the latest created container.
+func (r *Runtime) GetLatestContainer() (*Container, error) {
+ var lastCreatedIndex int
+ var lastCreatedTime time.Time
+ ctrs, err := r.GetAllContainers()
+ if err != nil {
+ return nil, errors.Wrapf(err, "unable to find latest container")
+ }
+ for containerIndex, ctr := range ctrs {
+ createdTime := ctr.config.CreatedTime
+ if createdTime.After(lastCreatedTime) {
+ lastCreatedTime = createdTime
+ lastCreatedIndex = containerIndex
+ }
+ }
+ return ctrs[lastCreatedIndex], nil
+}