mirror of
https://github.com/anchore/grype
synced 2024-11-10 06:34:13 +00:00
fix: add support for partybus ui on grype db update
cmd (#806)
This commit is contained in:
parent
b3a078aa02
commit
8ab0159f9f
6 changed files with 94 additions and 19 deletions
|
@ -4,8 +4,14 @@ import (
|
|||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/wagoodman/go-partybus"
|
||||
|
||||
"github.com/anchore/grype/grype/db"
|
||||
"github.com/anchore/grype/grype/event"
|
||||
"github.com/anchore/grype/internal/bus"
|
||||
"github.com/anchore/grype/internal/log"
|
||||
"github.com/anchore/grype/internal/ui"
|
||||
"github.com/anchore/stereoscope"
|
||||
)
|
||||
|
||||
var dbUpdateCmd = &cobra.Command{
|
||||
|
@ -19,21 +25,48 @@ func init() {
|
|||
dbCmd.AddCommand(dbUpdateCmd)
|
||||
}
|
||||
|
||||
func startDBUpdateCmd() <-chan error {
|
||||
errs := make(chan error)
|
||||
go func() {
|
||||
defer close(errs)
|
||||
dbCurator, err := db.NewCurator(appConfig.DB.ToCuratorConfig())
|
||||
if err != nil {
|
||||
errs <- err
|
||||
return
|
||||
}
|
||||
updated, err := dbCurator.Update()
|
||||
if err != nil {
|
||||
errs <- fmt.Errorf("unable to update vulnerability database: %+v", err)
|
||||
}
|
||||
|
||||
result := "No vulnerability database update available\n"
|
||||
if updated {
|
||||
result = "Vulnerability database updated!\n"
|
||||
}
|
||||
|
||||
bus.Publish(partybus.Event{
|
||||
Type: event.NonRootCommandFinished,
|
||||
Value: result,
|
||||
})
|
||||
}()
|
||||
return errs
|
||||
}
|
||||
|
||||
func runDBUpdateCmd(_ *cobra.Command, _ []string) error {
|
||||
dbCurator, err := db.NewCurator(appConfig.DB.ToCuratorConfig())
|
||||
reporter, closer, err := reportWriter()
|
||||
defer func() {
|
||||
if err := closer(); err != nil {
|
||||
log.Warnf("unable to write to report destination: %+v", err)
|
||||
}
|
||||
}()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
updated, err := dbCurator.Update()
|
||||
if err != nil {
|
||||
fmt.Println("Unable to update vulnerability database")
|
||||
return fmt.Errorf("unable to update vulnerability database: %+v", err)
|
||||
}
|
||||
|
||||
if updated {
|
||||
return stderrPrintLnf("Vulnerability database updated!")
|
||||
}
|
||||
|
||||
return stderrPrintLnf("No vulnerability database update available")
|
||||
return eventLoop(
|
||||
startDBUpdateCmd(),
|
||||
setupSignals(),
|
||||
eventSubscription,
|
||||
stereoscope.Cleanup,
|
||||
ui.Select(isVerbose(), appConfig.Quiet, reporter)...,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -9,4 +9,5 @@ const (
|
|||
VulnerabilityScanningFinished partybus.EventType = "grype-vulnerability-scanning-finished"
|
||||
AttestationVerified partybus.EventType = "grype-attestation-signature-passed"
|
||||
AttestationVerificationSkipped partybus.EventType = "grype-attestation-verification-skipped"
|
||||
NonRootCommandFinished partybus.EventType = "grype-non-root-command-finished"
|
||||
)
|
||||
|
|
|
@ -87,3 +87,16 @@ func ParseVulnerabilityScanningFinished(e partybus.Event) (presenter.Presenter,
|
|||
|
||||
return pres, nil
|
||||
}
|
||||
|
||||
func ParseNonRootCommandFinished(e partybus.Event) (*string, error) {
|
||||
if err := checkEventType(e.Type, event.NonRootCommandFinished); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result, ok := e.Value.(string)
|
||||
if !ok {
|
||||
return nil, newPayloadErr(e.Type, "Value", e.Value)
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
|
|
@ -21,3 +21,16 @@ func handleVulnerabilityScanningFinished(event partybus.Event, reportOutput io.W
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleNonRootCommandFinished(event partybus.Event, reportOutput io.Writer) error {
|
||||
// show the report to stdout
|
||||
result, err := grypeEventParsers.ParseNonRootCommandFinished(event)
|
||||
if err != nil {
|
||||
return fmt.Errorf("bad NonRootCommandFinished event: %w", err)
|
||||
}
|
||||
|
||||
if _, err := reportOutput.Write([]byte(*result)); err != nil {
|
||||
return fmt.Errorf("unable to show vulnerability report: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -92,6 +92,16 @@ func (h *ephemeralTerminalUI) Handle(event partybus.Event) error {
|
|||
log.Errorf("unable to show %s event: %+v", event.Type, err)
|
||||
}
|
||||
|
||||
// this is the last expected event, stop listening to events
|
||||
return h.unsubscribe()
|
||||
|
||||
case event.Type == grypeEvent.NonRootCommandFinished:
|
||||
h.closeScreen(false)
|
||||
|
||||
if err := handleNonRootCommandFinished(event, h.reportOutput); err != nil {
|
||||
log.Errorf("unable to show %s event: %+v", event.Type, err)
|
||||
}
|
||||
|
||||
// this is the last expected event, stop listening to events
|
||||
return h.unsubscribe()
|
||||
}
|
||||
|
|
|
@ -27,15 +27,20 @@ func (l *loggerUI) Setup(unsubscribe func() error) error {
|
|||
}
|
||||
|
||||
func (l loggerUI) Handle(event partybus.Event) error {
|
||||
// ignore all events except for the final event
|
||||
if event.Type != grypeEvent.VulnerabilityScanningFinished {
|
||||
switch event.Type {
|
||||
case grypeEvent.VulnerabilityScanningFinished:
|
||||
if err := handleVulnerabilityScanningFinished(event, l.reportOutput); err != nil {
|
||||
log.Warnf("unable to show catalog image finished event: %+v", err)
|
||||
}
|
||||
case grypeEvent.NonRootCommandFinished:
|
||||
if err := handleNonRootCommandFinished(event, l.reportOutput); err != nil {
|
||||
log.Warnf("unable to show command finished event: %+v", err)
|
||||
}
|
||||
// ignore all events except for the final events
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := handleVulnerabilityScanningFinished(event, l.reportOutput); err != nil {
|
||||
log.Warnf("unable to show catalog image finished event: %+v", err)
|
||||
}
|
||||
|
||||
// this is the last expected event, stop listening to events
|
||||
return l.unsubscribe()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue