Skip to content

Commit

Permalink
disable TUI for simpler commands
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
  • Loading branch information
wagoodman committed May 21, 2024
1 parent 8347931 commit 25b25a1
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/grype/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand Down
5 changes: 4 additions & 1 deletion cmd/grype/cli/commands/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions cmd/grype/cli/commands/db_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
7 changes: 4 additions & 3 deletions cmd/grype/cli/commands/db_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
9 changes: 5 additions & 4 deletions cmd/grype/cli/commands/db_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
},
Expand Down
7 changes: 4 additions & 3 deletions cmd/grype/cli/commands/db_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
7 changes: 4 additions & 3 deletions cmd/grype/cli/commands/db_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
5 changes: 3 additions & 2 deletions cmd/grype/cli/commands/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
17 changes: 17 additions & 0 deletions cmd/grype/cli/commands/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 25b25a1

Please sign in to comment.