More cleanup for failures on missing commands.

Currently in podman if a user specifies a command that does not exist
the tool shows the help information.  This patch changes it to show
information like:

$ ./bin/podman foobar
Error: unrecognized command 'podman foobar'
Try 'podman --help' for more information.
$ ./bin/podman volume foobar
Error: unrecognized command `podman volume foobar`
Try 'podman volume --help' for more information.
$ ./bin/podman container foobar
Error: unrecognized command `podman container foobar`
Try 'podman container --help' for more information.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
Daniel J Walsh 2019-03-07 16:44:39 -05:00
parent 94e89fc6ca
commit 181f327d57
No known key found for this signature in database
GPG key ID: A2DF901DABE2C028
11 changed files with 22 additions and 5 deletions

View file

@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"github.com/spf13/cobra"
"os"
"strings"
@ -14,6 +13,7 @@ import (
"github.com/containers/storage"
"github.com/fatih/camelcase"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
@ -67,6 +67,16 @@ func noSubArgs(c *cobra.Command, args []string) error {
return nil
}
func commandRunE() func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return errors.Errorf("unrecognized command `%s %s`\nTry '%s --help' for more information.", cmd.CommandPath(), args[0], cmd.CommandPath())
} else {
return errors.Errorf("missing command '%s COMMAND'\nTry '%s --help' for more information.", cmd.CommandPath(), cmd.CommandPath())
}
}
}
// getAllOrLatestContainers tries to return the correct list of containers
// depending if --all, --latest or <container-id> is used.
// It requires the Context (c) and the Runtime (runtime). As different
@ -537,7 +547,7 @@ Description:
// This blocks the desplaying of the global options. The main podman
// command should not use this.
func UsageTemplate() string {
return `Usage:{{if .Runnable}}
return `Usage:{{if (and .Runnable (not .HasAvailableSubCommands))}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}

View file

@ -15,6 +15,7 @@ var (
Short: "Manage Containers",
Long: containerDescription,
TraverseChildren: true,
RunE: commandRunE(),
},
}

View file

@ -12,6 +12,7 @@ var (
Use: "generate",
Short: "Generated structured data",
Long: generateDescription,
RunE: commandRunE(),
}
)

View file

@ -11,6 +11,7 @@ var healthcheckCommand = cliconfig.PodmanCommand{
Use: "healthcheck",
Short: "Manage Healthcheck",
Long: healthcheckDescription,
RunE: commandRunE(),
},
}

View file

@ -14,6 +14,7 @@ var (
Use: "image",
Short: "Manage images",
Long: imageDescription,
RunE: commandRunE(),
},
}
imagesSubCommand cliconfig.ImagesValues

View file

@ -82,9 +82,7 @@ var cmdsNotRequiringRootless = map[*cobra.Command]bool{
var rootCmd = &cobra.Command{
Use: "podman",
Long: "manage pods and images",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
RunE: commandRunE(),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return before(cmd, args)
},

View file

@ -12,6 +12,7 @@ var (
Use: "play",
Short: "Play a pod",
Long: playDescription,
RunE: commandRunE(),
}
)

View file

@ -13,6 +13,7 @@ var podCommand = cliconfig.PodmanCommand{
Use: "pod",
Short: "Manage pods",
Long: podDescription,
RunE: commandRunE(),
},
}

View file

@ -13,6 +13,7 @@ var (
Use: "system",
Short: "Manage podman",
Long: systemDescription,
RunE: commandRunE(),
},
}
)

View file

@ -14,6 +14,7 @@ var (
Use: "trust",
Short: "Manage container image trust policy",
Long: trustDescription,
RunE: commandRunE(),
},
}
)

View file

@ -12,6 +12,7 @@ var volumeCommand = cliconfig.PodmanCommand{
Use: "volume",
Short: "Manage volumes",
Long: volumeDescription,
RunE: commandRunE(),
},
}
var volumeSubcommands = []*cobra.Command{