-
Notifications
You must be signed in to change notification settings - Fork 585
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Keith Zantow <kzantow@gmail.com>
- Loading branch information
Showing
75 changed files
with
1,417 additions
and
2,319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package cli | ||
|
||
import ( | ||
"os" | ||
"runtime/debug" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/anchore/clio" | ||
"github.com/anchore/grype/cmd/grype/cli/commands" | ||
handler "github.com/anchore/grype/cmd/grype/cli/ui" | ||
"github.com/anchore/grype/cmd/grype/internal/ui" | ||
"github.com/anchore/grype/internal/bus" | ||
"github.com/anchore/grype/internal/log" | ||
"github.com/anchore/grype/internal/redact" | ||
"github.com/anchore/stereoscope" | ||
"github.com/anchore/syft/syft" | ||
) | ||
|
||
func Application(id clio.Identification) clio.Application { | ||
app, _ := create(id) | ||
return app | ||
} | ||
|
||
func Command(id clio.Identification) *cobra.Command { | ||
_, cmd := create(id) | ||
return cmd | ||
} | ||
|
||
func create(id clio.Identification) (clio.Application, *cobra.Command) { | ||
clioCfg := clio.NewSetupConfig(id). | ||
WithGlobalConfigFlag(). // add persistent -c <path> for reading an application config from | ||
WithGlobalLoggingFlags(). // add persistent -v and -q flags tied to the logging config | ||
WithConfigInRootHelp(). // --help on the root command renders the full application config in the help text | ||
WithUIConstructor( | ||
// select a UI based on the logging configuration and state of stdin (if stdin is a tty) | ||
func(cfg clio.Config) ([]clio.UI, error) { | ||
noUI := ui.None(cfg.Log.Quiet) | ||
if !cfg.Log.AllowUI(os.Stdin) || cfg.Log.Quiet { | ||
return []clio.UI{noUI}, nil | ||
} | ||
|
||
h := handler.New(handler.DefaultHandlerConfig()) | ||
|
||
return []clio.UI{ | ||
ui.New(cfg.Log.Quiet, h), | ||
noUI, | ||
}, nil | ||
}, | ||
). | ||
WithInitializers( | ||
func(state *clio.State) error { | ||
// clio is setting up and providing the bus, redact store, and logger to the application. Once loaded, | ||
// we can hoist them into the internal packages for global use. | ||
stereoscope.SetBus(state.Bus) | ||
syft.SetBus(state.Bus) | ||
bus.Set(state.Bus) | ||
|
||
redact.Set(state.RedactStore) | ||
|
||
stereoscope.SetLogger(state.Logger) | ||
syft.SetLogger(state.Logger) | ||
log.Set(state.Logger) | ||
|
||
return nil | ||
}, | ||
) | ||
|
||
app := clio.New(*clioCfg) | ||
|
||
rootCmd := commands.Root(app) | ||
|
||
// add sub-commands | ||
rootCmd.AddCommand( | ||
commands.DB(app), | ||
commands.Completion(), | ||
commands.Explain(app), | ||
clio.VersionCommand(id, syftVersion), | ||
) | ||
|
||
return app, rootCmd | ||
} | ||
|
||
func syftVersion() (string, string) { | ||
buildInfo, ok := debug.ReadBuildInfo() | ||
if !ok { | ||
log.Debug("unable to find the buildinfo section of the binary (syft version is unknown)") | ||
return "", "" | ||
} | ||
|
||
for _, d := range buildInfo.Deps { | ||
if d.Path == "github.com/anchore/syft" { | ||
return "SyftVersion", d.Version | ||
} | ||
} | ||
|
||
log.Debug("unable to find 'github.com/anchore/syft' from the buildinfo section of the binary") | ||
return "", "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package cli | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/anchore/clio" | ||
) | ||
|
||
func Test_Command(t *testing.T) { | ||
root := Command(clio.Identification{ | ||
Name: "test-name", | ||
Version: "test-version", | ||
}) | ||
|
||
require.Equal(t, root.Name(), "test-name") | ||
require.NotEmpty(t, root.Commands()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/anchore/clio" | ||
"github.com/anchore/grype/cmd/grype/cli/options" | ||
) | ||
|
||
type DBOptions struct { | ||
DB options.Database `yaml:"db" json:"db" mapstructure:"db"` | ||
} | ||
|
||
func dbOptionsDefault(id clio.Identification) *DBOptions { | ||
return &DBOptions{ | ||
DB: options.DefaultDatabase(id), | ||
} | ||
} | ||
|
||
func DB(app clio.Application) *cobra.Command { | ||
db := &cobra.Command{ | ||
Use: "db", | ||
Short: "vulnerability database operations", | ||
} | ||
|
||
db.AddCommand( | ||
DBCheck(app), | ||
DBDelete(app), | ||
DBDiff(app), | ||
DBImport(app), | ||
DBList(app), | ||
DBStatus(app), | ||
DBUpdate(app), | ||
) | ||
|
||
return db | ||
} |
29 changes: 18 additions & 11 deletions
29
cmd/grype/cli/legacy/db_check.go → cmd/grype/cli/commands/db_check.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/anchore/clio" | ||
"github.com/anchore/grype/cmd/grype/cli/options" | ||
"github.com/anchore/grype/grype/db" | ||
"github.com/anchore/grype/internal/bus" | ||
) | ||
|
||
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), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runDBDelete(opts.DB) | ||
}, | ||
}, opts) | ||
} | ||
|
||
func runDBDelete(opts options.Database) error { | ||
defer bus.Exit() | ||
|
||
dbCurator, err := db.NewCurator(opts.ToCuratorConfig()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := dbCurator.Delete(); err != nil { | ||
return fmt.Errorf("unable to delete vulnerability database: %+v", err) | ||
} | ||
|
||
return stderrPrintLnf("Vulnerability database deleted") | ||
} |
Oops, something went wrong.