github.com/anchore/syft@v1.38.2/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 ParsePullSourceStarted(e partybus.Event) (progress.StagedProgressable, *monitor.GenericTask, error) { 81 if err := checkEventType(e.Type, event.PullSourceStarted); err != nil { 82 return nil, nil, err 83 } 84 85 var mon progress.StagedProgressable 86 87 source, ok := e.Source.(monitor.GenericTask) 88 if !ok { 89 return nil, nil, newPayloadErr(e.Type, "Source", e.Source) 90 } 91 92 mon, ok = e.Value.(progress.StagedProgressable) 93 if !ok { 94 mon = nil 95 } 96 97 return mon, &source, nil 98 } 99 100 func ParseAttestationStartedEvent(e partybus.Event) (io.Reader, progress.Progressable, *monitor.GenericTask, error) { 101 if err := checkEventType(e.Type, event.AttestationStarted); err != nil { 102 return nil, nil, nil, err 103 } 104 105 source, ok := e.Source.(monitor.GenericTask) 106 if !ok { 107 return nil, nil, nil, newPayloadErr(e.Type, "Source", e.Source) 108 } 109 110 sp, ok := e.Value.(*monitor.ShellProgress) 111 if !ok { 112 return nil, nil, nil, newPayloadErr(e.Type, "Value", e.Value) 113 } 114 115 return sp.Reader, sp.Progressable, &source, nil 116 } 117 118 // CLI event types 119 120 type UpdateCheck struct { 121 New string 122 Current string 123 } 124 125 func ParseCLIAppUpdateAvailable(e partybus.Event) (*UpdateCheck, error) { 126 if err := checkEventType(e.Type, event.CLIAppUpdateAvailable); err != nil { 127 return nil, err 128 } 129 130 updateCheck, ok := e.Value.(UpdateCheck) 131 if !ok { 132 return nil, newPayloadErr(e.Type, "Value", e.Value) 133 } 134 135 return &updateCheck, nil 136 } 137 138 func ParseCLIReport(e partybus.Event) (string, string, error) { 139 if err := checkEventType(e.Type, event.CLIReport); err != nil { 140 return "", "", err 141 } 142 143 context, ok := e.Source.(string) 144 if !ok { 145 // this is optional 146 context = "" 147 } 148 149 report, ok := e.Value.(string) 150 if !ok { 151 return "", "", newPayloadErr(e.Type, "Value", e.Value) 152 } 153 154 return context, report, nil 155 } 156 157 func ParseCLINotification(e partybus.Event) (string, string, error) { 158 if err := checkEventType(e.Type, event.CLINotification); err != nil { 159 return "", "", err 160 } 161 162 context, ok := e.Source.(string) 163 if !ok { 164 // this is optional 165 context = "" 166 } 167 168 notification, ok := e.Value.(string) 169 if !ok { 170 return "", "", newPayloadErr(e.Type, "Value", e.Value) 171 } 172 173 return context, notification, nil 174 }