mirror of
https://github.com/containers/podman.git
synced 2025-10-17 19:24:04 +08:00
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:
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user