Use the new checkAllAndLatest() function

Instead of duplicating the same code in multiple commands this uses the
newly added function checkAllAndLatest() instead.

Signed-off-by: Adrian Reber <areber@redhat.com>
This commit is contained in:
Adrian Reber 2018-10-16 12:04:45 +00:00 committed by Adrian Reber
parent 215cf7b898
commit fea37b387c
Failed to extract signature
5 changed files with 21 additions and 49 deletions

View File

@ -44,33 +44,25 @@ func cleanupCmd(c *cli.Context) error {
} }
defer runtime.Shutdown(false) defer runtime.Shutdown(false)
args := c.Args() if err := checkAllAndLatest(c); err != nil {
return err
ctx := getContext() }
var lastError error var lastError error
var cleanupContainers []*libpod.Container var cleanupContainers []*libpod.Container
if c.Bool("all") { if c.Bool("all") {
if c.Bool("lastest") {
return errors.New("--all and --latest cannot be used together")
}
if len(args) != 0 {
return errors.New("--all and explicit container IDs cannot be used together")
}
cleanupContainers, err = runtime.GetContainers() cleanupContainers, err = runtime.GetContainers()
if err != nil { if err != nil {
return errors.Wrapf(err, "unable to get container list") return errors.Wrapf(err, "unable to get container list")
} }
} else if c.Bool("latest") { } else if c.Bool("latest") {
if len(args) != 0 {
return errors.New("--latest and explicit container IDs cannot be used together")
}
lastCtr, err := runtime.GetLatestContainer() lastCtr, err := runtime.GetLatestContainer()
if err != nil { if err != nil {
return errors.Wrapf(err, "unable to get latest container") return errors.Wrapf(err, "unable to get latest container")
} }
cleanupContainers = append(cleanupContainers, lastCtr) cleanupContainers = append(cleanupContainers, lastCtr)
} else { } else {
args := c.Args()
for _, i := range args { for _, i := range args {
container, err := runtime.LookupContainer(i) container, err := runtime.LookupContainer(i)
if err != nil { if err != nil {
@ -81,6 +73,9 @@ func cleanupCmd(c *cli.Context) error {
cleanupContainers = append(cleanupContainers, container) cleanupContainers = append(cleanupContainers, container)
} }
} }
ctx := getContext()
for _, ctr := range cleanupContainers { for _, ctr := range cleanupContainers {
if err = ctr.Cleanup(ctx); err != nil { if err = ctr.Cleanup(ctx); err != nil {
if lastError != nil { if lastError != nil {

View File

@ -41,19 +41,10 @@ var (
// killCmd kills one or more containers with a signal // killCmd kills one or more containers with a signal
func killCmd(c *cli.Context) error { func killCmd(c *cli.Context) error {
args := c.Args() if err := checkAllAndLatest(c); err != nil {
if (!c.Bool("all") && !c.Bool("latest")) && len(args) == 0 { return err
return errors.Errorf("you must specify one or more containers to kill")
}
if (c.Bool("all") || c.Bool("latest")) && len(args) > 0 {
return errors.Errorf("you cannot specify any containers to kill with --latest or --all")
}
if c.Bool("all") && c.Bool("latest") {
return errors.Errorf("--all and --latest cannot be used together")
}
if len(args) < 1 && !c.Bool("all") && !c.Bool("latest") {
return errors.Errorf("you must provide at least one container name or id")
} }
if err := validateFlags(c, killFlags); err != nil { if err := validateFlags(c, killFlags); err != nil {
return err return err
} }
@ -96,6 +87,7 @@ func killCmd(c *cli.Context) error {
} }
containers = append(containers, lastCtr) containers = append(containers, lastCtr)
} else { } else {
args := c.Args()
for _, i := range args { for _, i := range args {
container, err := runtime.LookupContainer(i) container, err := runtime.LookupContainer(i)
if err != nil { if err != nil {

View File

@ -63,13 +63,8 @@ func rmCmd(c *cli.Context) error {
} }
defer runtime.Shutdown(false) defer runtime.Shutdown(false)
args := c.Args() if err := checkAllAndLatest(c); err != nil {
if c.Bool("latest") && c.Bool("all") { return err
return errors.Errorf("--all and --latest cannot be used together")
}
if len(args) == 0 && !c.Bool("all") && !c.Bool("latest") {
return errors.Errorf("specify one or more containers to remove")
} }
if c.Bool("all") { if c.Bool("all") {
@ -84,6 +79,7 @@ func rmCmd(c *cli.Context) error {
} }
delContainers = append(delContainers, lastCtr) delContainers = append(delContainers, lastCtr)
} else { } else {
args := c.Args()
for _, i := range args { for _, i := range args {
container, err := runtime.LookupContainer(i) container, err := runtime.LookupContainer(i)
if err != nil { if err != nil {

View File

@ -44,16 +44,11 @@ var (
) )
func stopCmd(c *cli.Context) error { func stopCmd(c *cli.Context) error {
args := c.Args()
if (c.Bool("all") || c.Bool("latest")) && len(args) > 0 { if err := checkAllAndLatest(c); err != nil {
return errors.Errorf("no arguments are needed with --all or --latest") return err
}
if c.Bool("all") && c.Bool("latest") {
return errors.Errorf("--all and --latest cannot be used together")
}
if len(args) < 1 && !c.Bool("all") && !c.Bool("latest") {
return errors.Errorf("you must provide at least one container name or id")
} }
if err := validateFlags(c, stopFlags); err != nil { if err := validateFlags(c, stopFlags); err != nil {
return err return err
} }
@ -86,6 +81,7 @@ func stopCmd(c *cli.Context) error {
} }
containers = append(containers, lastCtr) containers = append(containers, lastCtr)
} else { } else {
args := c.Args()
for _, i := range args { for _, i := range args {
container, err := runtime.LookupContainer(i) container, err := runtime.LookupContainer(i)
if err != nil { if err != nil {

View File

@ -160,15 +160,8 @@ func (f *RawTtyFormatter) Format(entry *logrus.Entry) ([]byte, error) {
} }
func checkMutuallyExclusiveFlags(c *cli.Context) error { func checkMutuallyExclusiveFlags(c *cli.Context) error {
argLen := len(c.Args()) if err := checkAllAndLatest(c); err != nil {
if (c.Bool("all") || c.Bool("latest")) && argLen > 0 { return err
return errors.Errorf("no arguments are needed with --all or --latest")
}
if c.Bool("all") && c.Bool("latest") {
return errors.Errorf("--all and --latest cannot be used together")
}
if argLen < 1 && !c.Bool("all") && !c.Bool("latest") {
return errors.Errorf("you must provide at least one pod name or id")
} }
if err := validateFlags(c, startFlags); err != nil { if err := validateFlags(c, startFlags); err != nil {
return err return err