From d8f099bb5a38b85bdea2bf6d99e0dbd0457ab666 Mon Sep 17 00:00:00 2001 From: umohnani8 Date: Wed, 13 Dec 2017 11:23:01 -0500 Subject: Check for mutually exclusive flags Error out if more than one mutually exclusive flags are passed in to kpod ps Signed-off-by: umohnani8 Closes: #128 Approved by: rhatdan --- cmd/kpod/ps.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'cmd') diff --git a/cmd/kpod/ps.go b/cmd/kpod/ps.go index 4148edf68..89fed991f 100644 --- a/cmd/kpod/ps.go +++ b/cmd/kpod/ps.go @@ -143,9 +143,8 @@ func psCmd(c *cli.Context) error { return err } - // latest, and last are mutually exclusive. - if c.Int("last") >= 0 && c.Bool("latest") { - return errors.Errorf("last and latest are mutually exclusive") + if err := checkFlagsPassed(c); err != nil { + return errors.Wrapf(err, "error with flags passed") } runtime, err := getRuntime(c) @@ -212,6 +211,32 @@ func psCmd(c *cli.Context) error { return generatePsOutput(outputContainers, opts) } +// checkFlagsPassed checks if mutually exclusive flags are passed together +func checkFlagsPassed(c *cli.Context) error { + // latest, and last are mutually exclusive. + if c.Int("last") >= 0 && c.Bool("latest") { + return errors.Errorf("last and latest are mutually exclusive") + } + // quiet, size, namespace, and format with Go template are mutually exclusive + flags := 0 + if c.Bool("quiet") { + flags++ + } + if c.Bool("size") { + flags++ + } + if c.Bool("namespace") { + flags++ + } + if c.IsSet("format") && c.String("format") != formats.JSONString { + flags++ + } + if flags > 1 { + return errors.Errorf("quiet, size, namespace, and format with Go template are mutually exclusive") + } + return nil +} + func generateContainerFilterFuncs(filter, filterValue string, runtime *libpod.Runtime) (func(container *libpod.Container) bool, error) { switch filter { case "id": -- cgit v1.2.3-54-g00ecf