Combine the CheckAllLatest CID and PodID functions

These two functions were doing the exact same thing just
with cidfile and pod-id-file separately. Combine the functionality
to one function to remove repetative code.
Fix the TODO in cmd/podman/validate/args.go

[NO NEW TESTS NEEDED]

Signed-off-by: Urvashi Mohnani <umohnani@redhat.com>
This commit is contained in:
Urvashi Mohnani
2022-05-25 16:50:16 -04:00
parent e11feb2309
commit 426a07e700
20 changed files with 51 additions and 96 deletions

View File

@ -31,7 +31,7 @@ var (
Long: checkpointDescription, Long: checkpointDescription,
RunE: checkpoint, RunE: checkpoint,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, false, false) return validate.CheckAllLatestAndIDFile(cmd, args, false, "")
}, },
ValidArgsFunction: common.AutocompleteContainersRunning, ValidArgsFunction: common.AutocompleteContainersRunning,
Example: `podman container checkpoint --keep ctrID Example: `podman container checkpoint --keep ctrID

View File

@ -27,7 +27,7 @@ var (
Long: cleanupDescription, Long: cleanupDescription,
RunE: cleanup, RunE: cleanup,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, false, false) return validate.CheckAllLatestAndIDFile(cmd, args, false, "")
}, },
ValidArgsFunction: common.AutocompleteContainersExited, ValidArgsFunction: common.AutocompleteContainersExited,
Example: `podman container cleanup --latest Example: `podman container cleanup --latest

View File

@ -21,7 +21,7 @@ var (
Long: initDescription, Long: initDescription,
RunE: initContainer, RunE: initContainer,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, false, false) return validate.CheckAllLatestAndIDFile(cmd, args, false, "")
}, },
ValidArgsFunction: common.AutocompleteContainersCreated, ValidArgsFunction: common.AutocompleteContainersCreated,
Example: `podman init --latest Example: `podman init --latest

View File

@ -25,7 +25,7 @@ var (
Long: killDescription, Long: killDescription,
RunE: kill, RunE: kill,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, false, true) return validate.CheckAllLatestAndIDFile(cmd, args, false, "cidfile")
}, },
ValidArgsFunction: common.AutocompleteContainersRunning, ValidArgsFunction: common.AutocompleteContainersRunning,
Example: `podman kill mywebserver Example: `podman kill mywebserver
@ -35,7 +35,7 @@ var (
containerKillCommand = &cobra.Command{ containerKillCommand = &cobra.Command{
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, false, true) return validate.CheckAllLatestAndIDFile(cmd, args, false, "cidfile")
}, },
Use: killCommand.Use, Use: killCommand.Use,
Short: killCommand.Short, Short: killCommand.Short,

View File

@ -33,7 +33,7 @@ var (
Long: mountDescription, Long: mountDescription,
RunE: mount, RunE: mount,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, true, false) return validate.CheckAllLatestAndIDFile(cmd, args, true, "")
}, },
ValidArgsFunction: common.AutocompleteContainers, ValidArgsFunction: common.AutocompleteContainers,
} }

View File

@ -23,7 +23,7 @@ var (
Long: portDescription, Long: portDescription,
RunE: port, RunE: port,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, true, false) return validate.CheckAllLatestAndIDFile(cmd, args, true, "")
}, },
ValidArgsFunction: common.AutocompleteContainerOneArg, ValidArgsFunction: common.AutocompleteContainerOneArg,
Example: `podman port --all Example: `podman port --all
@ -37,7 +37,7 @@ var (
Long: portDescription, Long: portDescription,
RunE: portCommand.RunE, RunE: portCommand.RunE,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, true, false) return validate.CheckAllLatestAndIDFile(cmd, args, true, "")
}, },
ValidArgsFunction: portCommand.ValidArgsFunction, ValidArgsFunction: portCommand.ValidArgsFunction,
Example: `podman container port --all Example: `podman container port --all

View File

@ -26,7 +26,7 @@ var (
Long: restartDescription, Long: restartDescription,
RunE: restart, RunE: restart,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, false, false) return validate.CheckAllLatestAndIDFile(cmd, args, false, "")
}, },
ValidArgsFunction: common.AutocompleteContainers, ValidArgsFunction: common.AutocompleteContainers,
Example: `podman restart ctrID Example: `podman restart ctrID

View File

@ -28,7 +28,7 @@ var (
Long: restoreDescription, Long: restoreDescription,
RunE: restore, RunE: restore,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, true, false) return validate.CheckAllLatestAndIDFile(cmd, args, true, "")
}, },
ValidArgsFunction: common.AutocompleteContainersAndImages, ValidArgsFunction: common.AutocompleteContainersAndImages,
Example: `podman container restore ctrID Example: `podman container restore ctrID

View File

@ -28,7 +28,7 @@ var (
Long: rmDescription, Long: rmDescription,
RunE: rm, RunE: rm,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, false, true) return validate.CheckAllLatestAndIDFile(cmd, args, false, "cidfile")
}, },
ValidArgsFunction: common.AutocompleteContainers, ValidArgsFunction: common.AutocompleteContainers,
Example: `podman rm imageID Example: `podman rm imageID

View File

@ -26,7 +26,7 @@ var (
Long: stopDescription, Long: stopDescription,
RunE: stop, RunE: stop,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, false, true) return validate.CheckAllLatestAndIDFile(cmd, args, false, "cidfile")
}, },
ValidArgsFunction: common.AutocompleteContainersRunning, ValidArgsFunction: common.AutocompleteContainersRunning,
Example: `podman stop ctrID Example: `podman stop ctrID
@ -40,7 +40,7 @@ var (
Long: stopCommand.Long, Long: stopCommand.Long,
RunE: stopCommand.RunE, RunE: stopCommand.RunE,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, false, true) return validate.CheckAllLatestAndIDFile(cmd, args, false, "cidfile")
}, },
ValidArgsFunction: stopCommand.ValidArgsFunction, ValidArgsFunction: stopCommand.ValidArgsFunction,
Example: `podman container stop ctrID Example: `podman container stop ctrID

View File

@ -27,7 +27,7 @@ var (
Long: description, Long: description,
RunE: unmount, RunE: unmount,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, false, false) return validate.CheckAllLatestAndIDFile(cmd, args, false, "")
}, },
ValidArgsFunction: common.AutocompleteContainers, ValidArgsFunction: common.AutocompleteContainers,
Example: `podman unmount ctrID Example: `podman unmount ctrID
@ -43,7 +43,7 @@ var (
Long: unmountCommand.Long, Long: unmountCommand.Long,
RunE: unmountCommand.RunE, RunE: unmountCommand.RunE,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, false, false) return validate.CheckAllLatestAndIDFile(cmd, args, false, "")
}, },
ValidArgsFunction: common.AutocompleteContainers, ValidArgsFunction: common.AutocompleteContainers,
Example: `podman container unmount ctrID Example: `podman container unmount ctrID

View File

@ -21,7 +21,7 @@ var (
Long: networkReloadDescription, Long: networkReloadDescription,
RunE: networkReload, RunE: networkReload,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, false, false) return validate.CheckAllLatestAndIDFile(cmd, args, false, "")
}, },
ValidArgsFunction: common.AutocompleteContainers, ValidArgsFunction: common.AutocompleteContainers,
Example: `podman network reload --latest Example: `podman network reload --latest

View File

@ -22,7 +22,7 @@ var (
Long: podKillDescription, Long: podKillDescription,
RunE: kill, RunE: kill,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, false, false) return validate.CheckAllLatestAndIDFile(cmd, args, false, "")
}, },
ValidArgsFunction: common.AutocompletePodsRunning, ValidArgsFunction: common.AutocompletePodsRunning,
Example: `podman pod kill podID Example: `podman pod kill podID

View File

@ -22,7 +22,7 @@ var (
Long: podPauseDescription, Long: podPauseDescription,
RunE: pause, RunE: pause,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, false, false) return validate.CheckAllLatestAndIDFile(cmd, args, false, "")
}, },
ValidArgsFunction: common.AutocompletePodsRunning, ValidArgsFunction: common.AutocompletePodsRunning,
Example: `podman pod pause podID1 podID2 Example: `podman pod pause podID1 podID2

View File

@ -22,7 +22,7 @@ var (
Long: podRestartDescription, Long: podRestartDescription,
RunE: restart, RunE: restart,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, false, false) return validate.CheckAllLatestAndIDFile(cmd, args, false, "")
}, },
ValidArgsFunction: common.AutocompletePods, ValidArgsFunction: common.AutocompletePods,
Example: `podman pod restart podID1 podID2 Example: `podman pod restart podID1 podID2

View File

@ -35,7 +35,7 @@ var (
Long: podRmDescription, Long: podRmDescription,
RunE: rm, RunE: rm,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndPodIDFile(cmd, args, false, true) return validate.CheckAllLatestAndIDFile(cmd, args, false, "pod-id-file")
}, },
ValidArgsFunction: common.AutocompletePods, ValidArgsFunction: common.AutocompletePods,
Example: `podman pod rm mywebserverpod Example: `podman pod rm mywebserverpod

View File

@ -31,7 +31,7 @@ var (
Long: podStartDescription, Long: podStartDescription,
RunE: start, RunE: start,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndPodIDFile(cmd, args, false, true) return validate.CheckAllLatestAndIDFile(cmd, args, false, "pod-id-file")
}, },
ValidArgsFunction: common.AutocompletePods, ValidArgsFunction: common.AutocompletePods,
Example: `podman pod start podID Example: `podman pod start podID

View File

@ -36,7 +36,7 @@ var (
Long: podStopDescription, Long: podStopDescription,
RunE: stop, RunE: stop,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndPodIDFile(cmd, args, false, true) return validate.CheckAllLatestAndIDFile(cmd, args, false, "pod-id-file")
}, },
ValidArgsFunction: common.AutocompletePodsRunning, ValidArgsFunction: common.AutocompletePodsRunning,
Example: `podman pod stop mywebserverpod Example: `podman pod stop mywebserverpod

View File

@ -22,7 +22,7 @@ var (
Long: podUnpauseDescription, Long: podUnpauseDescription,
RunE: unpause, RunE: unpause,
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
return validate.CheckAllLatestAndCIDFile(cmd, args, false, false) return validate.CheckAllLatestAndIDFile(cmd, args, false, "")
}, },
// TODO have a function which shows only pods which could be unpaused // TODO have a function which shows only pods which could be unpaused
// for now show all // for now show all

View File

@ -50,34 +50,44 @@ func IDOrLatestArgs(cmd *cobra.Command, args []string) error {
return nil return nil
} }
// TODO: the two functions CheckAllLatestAndCIDFile and CheckAllLatestAndPodIDFile are almost identical. // CheckAllLatestAndCIDFile checks that --all and --latest are used correctly for containers and pods
// It may be worth looking into generalizing the two a bit more and share code but time is scarce and // If idFileFlag is set is set, also checks for the --cidfile or --pod-id-file flag.
// we only live once. // Note: this has been deprecated, use CheckAllLatestAndIDFile instead
// CheckAllLatestAndCIDFile checks that --all and --latest are used correctly.
// If cidfile is set, also check for the --cidfile flag.
func CheckAllLatestAndCIDFile(c *cobra.Command, args []string, ignoreArgLen bool, cidfile bool) error { func CheckAllLatestAndCIDFile(c *cobra.Command, args []string, ignoreArgLen bool, cidfile bool) error {
return CheckAllLatestAndIDFile(c, args, ignoreArgLen, "cidfile")
}
// CheckAllLatestAndPodIDFile checks that --all and --latest are used correctly.
// If withIDFile is set, also check for the --pod-id-file flag.
// Note: this has been deprecated, use CheckAllLatestAndIDFile instead
func CheckAllLatestAndPodIDFile(c *cobra.Command, args []string, ignoreArgLen bool, withIDFile bool) error {
return CheckAllLatestAndIDFile(c, args, ignoreArgLen, "pod-id-file")
}
// CheckAllLatestAndIDFile checks that --all and --latest are used correctly for containers and pods
// If idFileFlag is set is set, also checks for the --cidfile or --pod-id-file flag.
func CheckAllLatestAndIDFile(c *cobra.Command, args []string, ignoreArgLen bool, idFileFlag string) error {
var specifiedLatest bool var specifiedLatest bool
argLen := len(args) argLen := len(args)
if !registry.IsRemote() { if !registry.IsRemote() {
specifiedLatest, _ = c.Flags().GetBool("latest") specifiedLatest, _ = c.Flags().GetBool("latest")
if c.Flags().Lookup("all") == nil || c.Flags().Lookup("latest") == nil { if c.Flags().Lookup("all") == nil || c.Flags().Lookup("latest") == nil {
if !cidfile { if idFileFlag == "" {
return errors.New("unable to lookup values for 'latest' or 'all'") return errors.New("unable to lookup values for 'latest' or 'all'")
} else if c.Flags().Lookup("cidfile") == nil { } else if c.Flags().Lookup(idFileFlag) == nil {
return errors.New("unable to lookup values for 'latest', 'all' or 'cidfile'") return errors.Errorf("unable to lookup values for 'latest', 'all', or '%s'", idFileFlag)
} }
} }
} }
specifiedAll, _ := c.Flags().GetBool("all") specifiedAll, _ := c.Flags().GetBool("all")
specifiedCIDFile := false specifiedIDFile := false
if cid, _ := c.Flags().GetStringArray("cidfile"); len(cid) > 0 { if cid, _ := c.Flags().GetStringArray(idFileFlag); len(cid) > 0 {
specifiedCIDFile = true specifiedIDFile = true
} }
if specifiedCIDFile && (specifiedAll || specifiedLatest) { if specifiedIDFile && (specifiedAll || specifiedLatest) {
return errors.Errorf("--all, --latest and --cidfile cannot be used together") return errors.Errorf("--all, --latest, and --%s cannot be used together", idFileFlag)
} else if specifiedAll && specifiedLatest { } else if specifiedAll && specifiedLatest {
return errors.Errorf("--all and --latest cannot be used together") return errors.Errorf("--all and --latest cannot be used together")
} }
@ -93,71 +103,16 @@ func CheckAllLatestAndCIDFile(c *cobra.Command, args []string, ignoreArgLen bool
if argLen > 0 { if argLen > 0 {
if specifiedLatest { if specifiedLatest {
return errors.Errorf("--latest and containers cannot be used together") return errors.Errorf("--latest and containers cannot be used together")
} else if cidfile && (specifiedLatest || specifiedCIDFile) { } else if idFileFlag != "" && (specifiedLatest || specifiedIDFile) {
return errors.Errorf("no arguments are needed with --latest or --cidfile") return errors.Errorf("no arguments are needed with --latest or --%s", idFileFlag)
} }
} }
if specifiedCIDFile { if specifiedIDFile {
return nil return nil
} }
if argLen < 1 && !specifiedAll && !specifiedLatest && !specifiedCIDFile { if argLen < 1 && !specifiedAll && !specifiedLatest && !specifiedIDFile {
return errors.Errorf("you must provide at least one name or id")
}
return nil
}
// CheckAllLatestAndPodIDFile checks that --all and --latest are used correctly.
// If withIDFile is set, also check for the --pod-id-file flag.
func CheckAllLatestAndPodIDFile(c *cobra.Command, args []string, ignoreArgLen bool, withIDFile bool) error {
var specifiedLatest bool
argLen := len(args)
if !registry.IsRemote() {
// remote clients have no latest flag
specifiedLatest, _ = c.Flags().GetBool("latest")
if c.Flags().Lookup("all") == nil || c.Flags().Lookup("latest") == nil {
if !withIDFile {
return errors.New("unable to lookup values for 'latest' or 'all'")
} else if c.Flags().Lookup("pod-id-file") == nil {
return errors.New("unable to lookup values for 'latest', 'all' or 'pod-id-file'")
}
}
}
specifiedAll, _ := c.Flags().GetBool("all")
specifiedPodIDFile := false
if pid, _ := c.Flags().GetStringArray("pod-id-file"); len(pid) > 0 {
specifiedPodIDFile = true
}
if specifiedPodIDFile && (specifiedAll || specifiedLatest) {
return errors.Errorf("--all, --latest and --pod-id-file cannot be used together")
} else if specifiedAll && specifiedLatest {
return errors.Errorf("--all and --latest cannot be used together")
}
if (argLen > 0) && specifiedAll {
return errors.Errorf("no arguments are needed with --all")
}
if ignoreArgLen {
return nil
}
if argLen > 0 {
if specifiedLatest {
return errors.Errorf("--latest and pods cannot be used together")
} else if withIDFile && (specifiedLatest || specifiedPodIDFile) {
return errors.Errorf("no arguments are needed with --latest or --pod-id-file")
}
}
if specifiedPodIDFile {
return nil
}
if argLen < 1 && !specifiedAll && !specifiedLatest && !specifiedPodIDFile {
return errors.Errorf("you must provide at least one name or id") return errors.Errorf("you must provide at least one name or id")
} }
return nil return nil