summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Reber <areber@redhat.com>2018-10-16 13:36:08 +0000
committerAdrian Reber <adrian@lisas.de>2018-10-23 17:01:30 +0200
commit215cf7b8984f687a79ce6055e350ee3e75d81b79 (patch)
treecee7b4e8abd1a84f498b6d2db389cd84580ef6bb
parent9d9493e41a2d9fb79221986507d1789265355d2d (diff)
downloadpodman-215cf7b8984f687a79ce6055e350ee3e75d81b79.tar.gz
podman-215cf7b8984f687a79ce6055e350ee3e75d81b79.tar.bz2
podman-215cf7b8984f687a79ce6055e350ee3e75d81b79.zip
Also factor out getAllOrLatestContainers() function
Just as the checkAllAndLatest() function the new code in getAllOrLatestContainers() is used in some commands and duplicated. This factors out this code to be used in other places without duplicating it. Signed-off-by: Adrian Reber <areber@redhat.com>
-rw-r--r--cmd/podman/common.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/cmd/podman/common.go b/cmd/podman/common.go
index 3401b1a93..f9e746b28 100644
--- a/cmd/podman/common.go
+++ b/cmd/podman/common.go
@@ -104,6 +104,58 @@ func checkAllAndLatest(c *cli.Context) error {
return nil
}
+// getAllOrLatestContainers tries to return the correct list of containers
+// depending if --all, --latest or <container-id> is used.
+// It requires the Context (c) and the Runtime (runtime). As different
+// commands are using different container state for the --all option
+// the desired state has to be specified in filterState. If no filter
+// is desired a -1 can be used to get all containers. For a better
+// error message, if the filter fails, a corresponding verb can be
+// specified which will then appear in the error message.
+func getAllOrLatestContainers(c *cli.Context, runtime *libpod.Runtime, filterState libpod.ContainerStatus, verb string) ([]*libpod.Container, error) {
+ var containers []*libpod.Container
+ var lastError error
+ var err error
+ if c.Bool("all") {
+ if filterState != -1 {
+ var filterFuncs []libpod.ContainerFilter
+ filterFuncs = append(filterFuncs, func(c *libpod.Container) bool {
+ state, _ := c.State()
+ return state == filterState
+ })
+ containers, err = runtime.GetContainers(filterFuncs...)
+ } else {
+ containers, err = runtime.GetContainers()
+ }
+ if err != nil {
+ return nil, errors.Wrapf(err, "unable to get %s containers", verb)
+ }
+ } else if c.Bool("latest") {
+ lastCtr, err := runtime.GetLatestContainer()
+ if err != nil {
+ return nil, errors.Wrapf(err, "unable to get latest container")
+ }
+ containers = append(containers, lastCtr)
+ } else {
+ args := c.Args()
+ for _, i := range args {
+ container, err := runtime.LookupContainer(i)
+ if err != nil {
+ if lastError != nil {
+ fmt.Fprintln(os.Stderr, lastError)
+ }
+ lastError = errors.Wrapf(err, "unable to find container %s", i)
+ }
+ if container != nil {
+ // This is here to make sure this does not return [<nil>] but only nil
+ containers = append(containers, container)
+ }
+ }
+ }
+
+ return containers, lastError
+}
+
// getContext returns a non-nil, empty context
func getContext() context.Context {
return context.TODO()