From 7b08aa78e4ede4c54fda6cd9917bb62e18d0d634 Mon Sep 17 00:00:00 2001 From: baude Date: Tue, 2 Jan 2018 16:29:43 -0600 Subject: 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 Closes: #179 Approved by: baude --- libpod/runtime_ctr.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'libpod/runtime_ctr.go') 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 +} -- cgit v1.2.3-54-g00ecf