github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/event/parsers/parsers.go (about) 1 /* 2 Package parsers provides parser helpers to extract payloads for each event type that the syft library publishes onto the event bus. 3 */ 4 package parsers 5 6 import ( 7 "fmt" 8 "io" 9 10 "github.com/wagoodman/go-partybus" 11 "github.com/wagoodman/go-progress" 12 13 "github.com/anchore/syft/syft/event" 14 "github.com/anchore/syft/syft/event/monitor" 15 ) 16 17 type ErrBadPayload struct { 18 Type partybus.EventType 19 Field string 20 Value interface{} 21 } 22 23 func (e *ErrBadPayload) Error() string { 24 return fmt.Sprintf("event='%s' has bad event payload field=%q: %q", string(e.Type), e.Field, e.Value) 25 } 26 27 func newPayloadErr(t partybus.EventType, field string, value interface{}) error { 28 return &ErrBadPayload{ 29 Type: t, 30 Field: field, 31 Value: value, 32 } 33 } 34 35 func checkEventType(actual, expected partybus.EventType) error { 36 if actual != expected { 37 return newPayloadErr(expected, "Type", actual) 38 } 39 return nil 40 } 41 42 func ParseFileIndexingStarted(e partybus.Event) (string, progress.StagedProgressable, error) { 43 if err := checkEventType(e.Type, event.FileIndexingStarted); err != nil { 44 return "", nil, err 45 } 46 47 path, ok := e.Source.(string) 48 if !ok { 49 return "", nil, newPayloadErr(e.Type, "Source", e.Source) 50 } 51 52 prog, ok := e.Value.(progress.StagedProgressable) 53 if !ok { 54 return "", nil, newPayloadErr(e.Type, "Value", e.Value) 55 } 56 57 return path, prog, nil 58 } 59 60 func ParseCatalogerTaskStarted(e partybus.Event) (progress.StagedProgressable, *monitor.GenericTask, error) { 61 if err := checkEventType(e.Type, event.CatalogerTaskStarted); err != nil { 62 return nil, nil, err 63 } 64 65 var mon progress.StagedProgressable 66 67 source, ok := e.Source.(monitor.GenericTask) 68 if !ok { 69 return nil, nil, newPayloadErr(e.Type, "Source", e.Source) 70 } 71 72 mon, ok = e.Value.(progress.StagedProgressable) 73 if !ok { 74 mon = nil 75 } 76 77 return mon, &source, nil 78 } 79 80 func ParseAttestationStartedEvent(e partybus.Event) (io.Reader, progress.Progressable, *monitor.GenericTask, error) { 81 if err := checkEventType(e.Type, event.AttestationStarted); err != nil { 82 return nil, nil, nil, err 83 } 84 85 source, ok := e.Source.(monitor.GenericTask) 86 if !ok { 87 return nil, nil, nil, newPayloadErr(e.Type, "Source", e.Source) 88 } 89 90 sp, ok := e.Value.(*monitor.ShellProgress) 91 if !ok { 92 return nil, nil, nil, newPayloadErr(e.Type, "Value", e.Value) 93 } 94 95 return sp.Reader, sp.Progressable, &source, nil 96 } 97 98 // CLI event types 99 100 type UpdateCheck struct { 101 New string 102 Current string 103 } 104 105 func ParseCLIAppUpdateAvailable(e partybus.Event) (*UpdateCheck, error) { 106 if err := checkEventType(e.Type, event.CLIAppUpdateAvailable); err != nil { 107 return nil, err 108 } 109 110 updateCheck, ok := e.Value.(UpdateCheck) 111 if !ok { 112 return nil, newPayloadErr(e.Type, "Value", e.Value) 113 } 114 115 return &updateCheck, nil 116 } 117 118 func ParseCLIReport(e partybus.Event) (string, string, error) { 119 if err := checkEventType(e.Type, event.CLIReport); err != nil { 120 return "", "", err 121 } 122 123 context, ok := e.Source.(string) 124 if !ok { 125 // this is optional 126 context = "" 127 } 128 129 report, ok := e.Value.(string) 130 if !ok { 131 return "", "", newPayloadErr(e.Type, "Value", e.Value) 132 } 133 134 return context, report, nil 135 } 136 137 func ParseCLINotification(e partybus.Event) (string, string, error) { 138 if err := checkEventType(e.Type, event.CLINotification); err != nil { 139 return "", "", err 140 } 141 142 context, ok := e.Source.(string) 143 if !ok { 144 // this is optional 145 context = "" 146 } 147 148 notification, ok := e.Value.(string) 149 if !ok { 150 return "", "", newPayloadErr(e.Type, "Value", e.Value) 151 } 152 153 return context, notification, nil 154 }