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
This commit is contained in:
baude
2018-01-02 16:29:43 -06:00
committed by Atomic Bot
parent 6baf6e461d
commit 7b08aa78e4
27 changed files with 257 additions and 48 deletions

View File

@ -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
}