mirror of
https://github.com/containers/podman.git
synced 2025-06-21 17:38:12 +08:00
Merge pull request #2452 from edsantiago/no_more_args
Command-line input validation: reject unused args
This commit is contained in:
@ -59,6 +59,14 @@ func checkAllAndLatest(c *cobra.Command, args []string, ignoreArgLen bool) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// noSubArgs checks that there are no further positional parameters
|
||||||
|
func noSubArgs(c *cobra.Command, args []string) error {
|
||||||
|
if len(args) > 0 {
|
||||||
|
return errors.Errorf("`%s` takes no arguments", c.CommandPath())
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// getAllOrLatestContainers tries to return the correct list of containers
|
// getAllOrLatestContainers tries to return the correct list of containers
|
||||||
// depending if --all, --latest or <container-id> is used.
|
// depending if --all, --latest or <container-id> is used.
|
||||||
// It requires the Context (c) and the Runtime (runtime). As different
|
// It requires the Context (c) and the Runtime (runtime). As different
|
||||||
|
@ -21,6 +21,7 @@ var (
|
|||||||
`
|
`
|
||||||
_pruneContainersCommand = &cobra.Command{
|
_pruneContainersCommand = &cobra.Command{
|
||||||
Use: "prune",
|
Use: "prune",
|
||||||
|
Args: noSubArgs,
|
||||||
Short: "Remove all stopped containers",
|
Short: "Remove all stopped containers",
|
||||||
Long: pruneContainersDescription,
|
Long: pruneContainersDescription,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
@ -18,6 +18,7 @@ var (
|
|||||||
`
|
`
|
||||||
_pruneImagesCommand = &cobra.Command{
|
_pruneImagesCommand = &cobra.Command{
|
||||||
Use: "prune",
|
Use: "prune",
|
||||||
|
Args: noSubArgs,
|
||||||
Short: "Remove unused images",
|
Short: "Remove unused images",
|
||||||
Long: pruneImagesDescription,
|
Long: pruneImagesDescription,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
@ -19,6 +19,7 @@ var (
|
|||||||
infoDescription = "Display podman system information"
|
infoDescription = "Display podman system information"
|
||||||
_infoCommand = &cobra.Command{
|
_infoCommand = &cobra.Command{
|
||||||
Use: "info",
|
Use: "info",
|
||||||
|
Args: noSubArgs,
|
||||||
Long: infoDescription,
|
Long: infoDescription,
|
||||||
Short: `Display information pertaining to the host, current storage stats, and build of podman. Useful for the user and when reporting issues.`,
|
Short: `Display information pertaining to the host, current storage stats, and build of podman. Useful for the user and when reporting issues.`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
@ -24,6 +24,7 @@ var (
|
|||||||
|
|
||||||
_podCreateCommand = &cobra.Command{
|
_podCreateCommand = &cobra.Command{
|
||||||
Use: "create",
|
Use: "create",
|
||||||
|
Args: noSubArgs,
|
||||||
Short: "Create a new empty pod",
|
Short: "Create a new empty pod",
|
||||||
Long: podCreateDescription,
|
Long: podCreateDescription,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
@ -59,9 +60,6 @@ func podCreateCmd(c *cliconfig.PodCreateValues) error {
|
|||||||
podIdFile *os.File
|
podIdFile *os.File
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(c.InputArgs) > 0 {
|
|
||||||
return errors.New("podman pod create does not accept any arguments")
|
|
||||||
}
|
|
||||||
runtime, err := adapter.GetRuntime(&c.PodmanCommand)
|
runtime, err := adapter.GetRuntime(&c.PodmanCommand)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "error creating libpod runtime")
|
return errors.Wrapf(err, "error creating libpod runtime")
|
||||||
|
@ -121,6 +121,7 @@ var (
|
|||||||
_podPsCommand = &cobra.Command{
|
_podPsCommand = &cobra.Command{
|
||||||
Use: "ps",
|
Use: "ps",
|
||||||
Aliases: []string{"ls", "list"},
|
Aliases: []string{"ls", "list"},
|
||||||
|
Args: noSubArgs,
|
||||||
Short: "List pods",
|
Short: "List pods",
|
||||||
Long: podPsDescription,
|
Long: podPsDescription,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
@ -160,10 +161,6 @@ func podPsCmd(c *cliconfig.PodPsValues) error {
|
|||||||
}
|
}
|
||||||
defer runtime.Shutdown(false)
|
defer runtime.Shutdown(false)
|
||||||
|
|
||||||
if len(c.InputArgs) > 0 {
|
|
||||||
return errors.Errorf("too many arguments, ps takes no arguments")
|
|
||||||
}
|
|
||||||
|
|
||||||
opts := podPsOptions{
|
opts := podPsOptions{
|
||||||
NoTrunc: c.NoTrunc,
|
NoTrunc: c.NoTrunc,
|
||||||
Quiet: c.Quiet,
|
Quiet: c.Quiet,
|
||||||
|
@ -159,6 +159,7 @@ var (
|
|||||||
psDescription = "Prints out information about the containers"
|
psDescription = "Prints out information about the containers"
|
||||||
_psCommand = cobra.Command{
|
_psCommand = cobra.Command{
|
||||||
Use: "ps",
|
Use: "ps",
|
||||||
|
Args: noSubArgs,
|
||||||
Short: "List containers",
|
Short: "List containers",
|
||||||
Long: psDescription,
|
Long: psDescription,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
@ -215,10 +216,6 @@ func psCmd(c *cliconfig.PsValues) error {
|
|||||||
|
|
||||||
defer runtime.Shutdown(false)
|
defer runtime.Shutdown(false)
|
||||||
|
|
||||||
if len(c.InputArgs) > 0 {
|
|
||||||
return errors.Errorf("too many arguments, ps takes no arguments")
|
|
||||||
}
|
|
||||||
|
|
||||||
opts := shared.PsOptions{
|
opts := shared.PsOptions{
|
||||||
All: c.All,
|
All: c.All,
|
||||||
Format: c.Format,
|
Format: c.Format,
|
||||||
|
@ -15,6 +15,7 @@ var (
|
|||||||
refreshDescription = "The refresh command resets the state of all containers to handle database changes after a Podman upgrade. All running containers will be restarted."
|
refreshDescription = "The refresh command resets the state of all containers to handle database changes after a Podman upgrade. All running containers will be restarted."
|
||||||
_refreshCommand = &cobra.Command{
|
_refreshCommand = &cobra.Command{
|
||||||
Use: "refresh",
|
Use: "refresh",
|
||||||
|
Args: noSubArgs,
|
||||||
Short: "Refresh container state",
|
Short: "Refresh container state",
|
||||||
Long: refreshDescription,
|
Long: refreshDescription,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
@ -32,10 +33,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func refreshCmd(c *cliconfig.RefreshValues) error {
|
func refreshCmd(c *cliconfig.RefreshValues) error {
|
||||||
if len(c.InputArgs) > 0 {
|
|
||||||
return errors.Errorf("refresh does not accept any arguments")
|
|
||||||
}
|
|
||||||
|
|
||||||
runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand)
|
runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "error creating libpod runtime")
|
return errors.Wrapf(err, "error creating libpod runtime")
|
||||||
|
@ -18,6 +18,7 @@ var (
|
|||||||
|
|
||||||
_renumberCommand = &cobra.Command{
|
_renumberCommand = &cobra.Command{
|
||||||
Use: "renumber",
|
Use: "renumber",
|
||||||
|
Args: noSubArgs,
|
||||||
Short: "Migrate lock numbers",
|
Short: "Migrate lock numbers",
|
||||||
Long: renumberDescription,
|
Long: renumberDescription,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
@ -49,6 +49,9 @@ func varlinkCmd(c *cliconfig.VarlinkValues) error {
|
|||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
return errors.Errorf("you must provide a varlink URI")
|
return errors.Errorf("you must provide a varlink URI")
|
||||||
}
|
}
|
||||||
|
if len(args) > 1 {
|
||||||
|
return errors.Errorf("too many arguments. Requires exactly 1")
|
||||||
|
}
|
||||||
timeout := time.Duration(c.Timeout) * time.Millisecond
|
timeout := time.Duration(c.Timeout) * time.Millisecond
|
||||||
|
|
||||||
// Create a single runtime for varlink
|
// Create a single runtime for varlink
|
||||||
|
@ -17,6 +17,7 @@ var (
|
|||||||
versionCommand cliconfig.VersionValues
|
versionCommand cliconfig.VersionValues
|
||||||
_versionCommand = &cobra.Command{
|
_versionCommand = &cobra.Command{
|
||||||
Use: "version",
|
Use: "version",
|
||||||
|
Args: noSubArgs,
|
||||||
Short: "Display the Podman Version Information",
|
Short: "Display the Podman Version Information",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
versionCommand.InputArgs = args
|
versionCommand.InputArgs = args
|
||||||
|
@ -49,6 +49,7 @@ and the output format can be changed to JSON or a user specified Go template.
|
|||||||
_volumeLsCommand = &cobra.Command{
|
_volumeLsCommand = &cobra.Command{
|
||||||
Use: "ls",
|
Use: "ls",
|
||||||
Aliases: []string{"list"},
|
Aliases: []string{"list"},
|
||||||
|
Args: noSubArgs,
|
||||||
Short: "List volumes",
|
Short: "List volumes",
|
||||||
Long: volumeLsDescription,
|
Long: volumeLsDescription,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
@ -76,10 +77,6 @@ func volumeLsCmd(c *cliconfig.VolumeLsValues) error {
|
|||||||
}
|
}
|
||||||
defer runtime.Shutdown(false)
|
defer runtime.Shutdown(false)
|
||||||
|
|
||||||
if len(c.InputArgs) > 0 {
|
|
||||||
return errors.Errorf("too many arguments, ls takes no arguments")
|
|
||||||
}
|
|
||||||
|
|
||||||
opts := volumeLsOptions{
|
opts := volumeLsOptions{
|
||||||
Quiet: c.Quiet,
|
Quiet: c.Quiet,
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@ using force.
|
|||||||
`
|
`
|
||||||
_volumePruneCommand = &cobra.Command{
|
_volumePruneCommand = &cobra.Command{
|
||||||
Use: "prune",
|
Use: "prune",
|
||||||
|
Args: noSubArgs,
|
||||||
Short: "Remove all unused volumes",
|
Short: "Remove all unused volumes",
|
||||||
Long: volumePruneDescription,
|
Long: volumePruneDescription,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
Reference in New Issue
Block a user