From 328250e7bef56b0b69dbea124c1170abd289900f Mon Sep 17 00:00:00 2001 From: baude Date: Wed, 20 Feb 2019 13:46:41 -0600 Subject: Improve command line validation Use the checkallandlatest function to validate flag usage as part of the cobra command args validation. Signed-off-by: baude --- cmd/podman/common.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'cmd/podman/common.go') diff --git a/cmd/podman/common.go b/cmd/podman/common.go index fed07de7c..e297f3921 100644 --- a/cmd/podman/common.go +++ b/cmd/podman/common.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "github.com/spf13/cobra" "os" "strings" @@ -36,16 +37,24 @@ func shortID(id string) string { } // checkAllAndLatest checks that --all and --latest are used correctly -func checkAllAndLatest(c *cliconfig.PodmanCommand) error { - argLen := len(c.InputArgs) - if (c.Bool("all") || c.Bool("latest")) && argLen > 0 { - return errors.Errorf("no arguments are needed with --all or --latest") +func checkAllAndLatest(c *cobra.Command, args []string, ignoreArgLen bool) error { + argLen := len(args) + if c.Flags().Lookup("all") == nil || c.Flags().Lookup("latest") == nil { + return errors.New("unable to lookup values for 'latest' or 'all'") } - if c.Bool("all") && c.Bool("latest") { + all, _ := c.Flags().GetBool("all") + latest, _ := c.Flags().GetBool("latest") + if all && 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 ignoreArgLen { + return nil + } + if (all || latest) && argLen > 0 { + return errors.Errorf("no arguments are needed with --all or --latest") + } + if argLen < 1 && !all && !latest { + return errors.Errorf("you must provide at least one name or id") } return nil } -- cgit v1.2.3-54-g00ecf