disable TUI for simpler commands (#1872)

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
Alex Goodman 2024-05-21 11:17:33 -04:00 committed by GitHub
parent 834793100e
commit 2b1ca9b07f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 46 additions and 20 deletions

View file

@ -79,7 +79,7 @@ func create(id clio.Identification) (clio.Application, *cobra.Command) {
// add sub-commands
rootCmd.AddCommand(
commands.DB(app),
commands.Completion(),
commands.Completion(app),
commands.Explain(app),
clio.VersionCommand(id, syftVersion, dbVersion),
)

View file

@ -9,10 +9,12 @@ import (
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"
"github.com/spf13/cobra"
"github.com/anchore/clio"
)
// Completion returns a command to provide completion to various terminal shells
func Completion() *cobra.Command {
func Completion(app clio.Application) *cobra.Command {
return &cobra.Command{
Use: "completion [bash|zsh|fish]",
Short: "Generate a shell completion for Grype (listing local docker images)",
@ -49,6 +51,7 @@ $ grype completion fish > ~/.config/fish/completions/grype.fish
`,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "fish", "zsh"},
PreRunE: disableUI(app),
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
RunE: func(cmd *cobra.Command, args []string) error {
var err error

View file

@ -19,9 +19,10 @@ func DBCheck(app clio.Application) *cobra.Command {
opts := dbOptionsDefault(app.ID())
return app.SetupCommand(&cobra.Command{
Use: "check",
Short: "check to see if there is a database update available",
Args: cobra.ExactArgs(0),
Use: "check",
Short: "check to see if there is a database update available",
PreRunE: disableUI(app),
Args: cobra.ExactArgs(0),
RunE: func(_ *cobra.Command, _ []string) error {
return runDBCheck(opts.DB)
},

View file

@ -14,9 +14,10 @@ func DBDelete(app clio.Application) *cobra.Command {
opts := dbOptionsDefault(app.ID())
return app.SetupCommand(&cobra.Command{
Use: "delete",
Short: "delete the vulnerability database",
Args: cobra.ExactArgs(0),
Use: "delete",
Short: "delete the vulnerability database",
Args: cobra.ExactArgs(0),
PreRunE: disableUI(app),
RunE: func(_ *cobra.Command, _ []string) error {
return runDBDelete(opts.DB)
},

View file

@ -15,10 +15,11 @@ func DBImport(app clio.Application) *cobra.Command {
opts := dbOptionsDefault(app.ID())
return app.SetupCommand(&cobra.Command{
Use: "import FILE",
Short: "import a vulnerability database archive",
Long: fmt.Sprintf("import a vulnerability database archive from a local FILE.\nDB archives can be obtained from %q.", internal.DBUpdateURL),
Args: cobra.ExactArgs(1),
Use: "import FILE",
Short: "import a vulnerability database archive",
Long: fmt.Sprintf("import a vulnerability database archive from a local FILE.\nDB archives can be obtained from %q.", internal.DBUpdateURL),
Args: cobra.ExactArgs(1),
PreRunE: disableUI(app),
RunE: func(_ *cobra.Command, args []string) error {
return runDBImport(opts.DB, args[0])
},

View file

@ -29,9 +29,10 @@ func DBList(app clio.Application) *cobra.Command {
}
return app.SetupCommand(&cobra.Command{
Use: "list",
Short: "list all DBs available according to the listing URL",
Args: cobra.ExactArgs(0),
Use: "list",
Short: "list all DBs available according to the listing URL",
PreRunE: disableUI(app),
Args: cobra.ExactArgs(0),
RunE: func(_ *cobra.Command, _ []string) error {
return runDBList(opts)
},

View file

@ -14,9 +14,10 @@ func DBStatus(app clio.Application) *cobra.Command {
opts := dbOptionsDefault(app.ID())
return app.SetupCommand(&cobra.Command{
Use: "status",
Short: "display database status",
Args: cobra.ExactArgs(0),
Use: "status",
Short: "display database status",
Args: cobra.ExactArgs(0),
PostRunE: disableUI(app),
RunE: func(_ *cobra.Command, _ []string) error {
return runDBStatus(opts.DB)
},

View file

@ -28,8 +28,9 @@ func Explain(app clio.Application) *cobra.Command {
opts := &explainOptions{}
return app.SetupCommand(&cobra.Command{
Use: "explain --id [VULNERABILITY ID]",
Short: "Ask grype to explain a set of findings",
Use: "explain --id [VULNERABILITY ID]",
Short: "Ask grype to explain a set of findings",
PreRunE: disableUI(app),
RunE: func(_ *cobra.Command, _ []string) error {
log.Warn("grype explain is a prototype feature and is subject to change")
isStdinPipeOrRedirect, err := internal.IsStdinPipeOrRedirect()

View file

@ -7,9 +7,26 @@ import (
"sync"
"github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"
"github.com/anchore/clio"
"github.com/anchore/grype/cmd/grype/internal/ui"
)
func disableUI(app clio.Application) func(*cobra.Command, []string) error {
return func(_ *cobra.Command, _ []string) error {
type Stater interface {
State() *clio.State
}
state := app.(Stater).State()
state.UIs = []clio.UI{ui.None(state.Config.Log.Quiet)}
return nil
}
}
func stderrPrintLnf(message string, args ...interface{}) error {
if !strings.HasSuffix(message, "\n") {
message += "\n"