mirror of
https://github.com/containers/podman.git
synced 2025-06-20 00:51:16 +08:00
Use the newly added getAllOrLatestContainers() function
This removes duplicate code paths which has been previously factored out as getAllOrLatestContainers(). Signed-off-by: Adrian Reber <areber@redhat.com>
This commit is contained in:

committed by
Adrian Reber

parent
fea37b387c
commit
c10ac01395
@ -5,7 +5,6 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/containers/libpod/cmd/podman/libpodruntime"
|
||||
"github.com/containers/libpod/libpod"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
@ -48,31 +47,7 @@ func cleanupCmd(c *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
var lastError error
|
||||
var cleanupContainers []*libpod.Container
|
||||
if c.Bool("all") {
|
||||
cleanupContainers, err = runtime.GetContainers()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to get container list")
|
||||
}
|
||||
} else if c.Bool("latest") {
|
||||
lastCtr, err := runtime.GetLatestContainer()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to get latest container")
|
||||
}
|
||||
cleanupContainers = append(cleanupContainers, lastCtr)
|
||||
} else {
|
||||
args := c.Args()
|
||||
for _, i := range args {
|
||||
container, err := runtime.LookupContainer(i)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
lastError = errors.Wrapf(err, "unable to find container %s", i)
|
||||
continue
|
||||
}
|
||||
cleanupContainers = append(cleanupContainers, container)
|
||||
}
|
||||
}
|
||||
cleanupContainers, lastError := getAllOrLatestContainers(c, runtime, -1, "all")
|
||||
|
||||
ctx := getContext()
|
||||
|
||||
|
@ -67,39 +67,7 @@ func killCmd(c *cli.Context) error {
|
||||
killSignal = uint(sysSignal)
|
||||
}
|
||||
|
||||
var filterFuncs []libpod.ContainerFilter
|
||||
var containers []*libpod.Container
|
||||
var lastError error
|
||||
if c.Bool("all") {
|
||||
// only get running containers
|
||||
filterFuncs = append(filterFuncs, func(c *libpod.Container) bool {
|
||||
state, _ := c.State()
|
||||
return state == libpod.ContainerStateRunning
|
||||
})
|
||||
containers, err = runtime.GetContainers(filterFuncs...)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to get running containers")
|
||||
}
|
||||
} else if c.Bool("latest") {
|
||||
lastCtr, err := runtime.GetLatestContainer()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to get last created 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)
|
||||
continue
|
||||
}
|
||||
containers = append(containers, container)
|
||||
}
|
||||
}
|
||||
containers, lastError := getAllOrLatestContainers(c, runtime, libpod.ContainerStateRunning, "running")
|
||||
|
||||
for _, ctr := range containers {
|
||||
if err := ctr.Kill(killSignal); err != nil {
|
||||
|
@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
rt "runtime"
|
||||
|
||||
"github.com/containers/libpod/cmd/podman/libpodruntime"
|
||||
@ -67,29 +66,7 @@ func rmCmd(c *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if c.Bool("all") {
|
||||
delContainers, err = runtime.GetContainers()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to get container list")
|
||||
}
|
||||
} else if c.Bool("latest") {
|
||||
lastCtr, err := runtime.GetLatestContainer()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to get latest container")
|
||||
}
|
||||
delContainers = append(delContainers, lastCtr)
|
||||
} else {
|
||||
args := c.Args()
|
||||
for _, i := range args {
|
||||
container, err := runtime.LookupContainer(i)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
lastError = errors.Wrapf(err, "unable to find container %s", i)
|
||||
continue
|
||||
}
|
||||
delContainers = append(delContainers, container)
|
||||
}
|
||||
}
|
||||
delContainers, lastError = getAllOrLatestContainers(c, runtime, -1, "all")
|
||||
|
||||
for _, container := range delContainers {
|
||||
f := func() error {
|
||||
|
@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
rt "runtime"
|
||||
|
||||
"github.com/containers/libpod/cmd/podman/libpodruntime"
|
||||
@ -60,40 +59,7 @@ func stopCmd(c *cli.Context) error {
|
||||
}
|
||||
defer runtime.Shutdown(false)
|
||||
|
||||
var filterFuncs []libpod.ContainerFilter
|
||||
var containers []*libpod.Container
|
||||
var lastError error
|
||||
|
||||
if c.Bool("all") {
|
||||
// only get running containers
|
||||
filterFuncs = append(filterFuncs, func(c *libpod.Container) bool {
|
||||
state, _ := c.State()
|
||||
return state == libpod.ContainerStateRunning
|
||||
})
|
||||
containers, err = runtime.GetContainers(filterFuncs...)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to get running containers")
|
||||
}
|
||||
} else if c.Bool("latest") {
|
||||
lastCtr, err := runtime.GetLatestContainer()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to get last created 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)
|
||||
continue
|
||||
}
|
||||
containers = append(containers, container)
|
||||
}
|
||||
}
|
||||
containers, lastError := getAllOrLatestContainers(c, runtime, libpod.ContainerStateRunning, "running")
|
||||
|
||||
var stopFuncs []workerInput
|
||||
for _, ctr := range containers {
|
||||
|
Reference in New Issue
Block a user