github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/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 "github.com/anchore/syft/syft/pkg/cataloger" 16 ) 17 18 type ErrBadPayload struct { 19 Type partybus.EventType 20 Field string 21 Value interface{} 22 } 23 24 func (e *ErrBadPayload) Error() string { 25 return fmt.Sprintf("event='%s' has bad event payload field=%q: %q", string(e.Type), e.Field, e.Value) 26 } 27 28 func newPayloadErr(t partybus.EventType, field string, value interface{}) error { 29 return &ErrBadPayload{ 30 Type: t, 31 Field: field, 32 Value: value, 33 } 34 } 35 36 func checkEventType(actual, expected partybus.EventType) error { 37 if actual != expected { 38 return newPayloadErr(expected, "Type", actual) 39 } 40 return nil 41 } 42 43 func ParsePackageCatalogerStarted(e partybus.Event) (*cataloger.Monitor, error) { 44 if err := checkEventType(e.Type, event.PackageCatalogerStarted); err != nil { 45 return nil, err 46 } 47 48 monitor, ok := e.Value.(cataloger.Monitor) 49 if !ok { 50 return nil, newPayloadErr(e.Type, "Value", e.Value) 51 } 52 53 return &monitor, nil 54 } 55 56 func ParseFileMetadataCatalogingStarted(e partybus.Event) (progress.StagedProgressable, error) { 57 if err := checkEventType(e.Type, event.FileMetadataCatalogerStarted); err != nil { 58 return nil, err 59 } 60 61 prog, ok := e.Value.(progress.StagedProgressable) 62 if !ok { 63 return nil, newPayloadErr(e.Type, "Value", e.Value) 64 } 65 66 return prog, nil 67 } 68 69 func ParseFileDigestsCatalogingStarted(e partybus.Event) (progress.StagedProgressable, error) { 70 if err := checkEventType(e.Type, event.FileDigestsCatalogerStarted); err != nil { 71 return nil, err 72 } 73 74 prog, ok := e.Value.(progress.StagedProgressable) 75 if !ok { 76 return nil, newPayloadErr(e.Type, "Value", e.Value) 77 } 78 79 return prog, nil 80 } 81 82 func ParseFileIndexingStarted(e partybus.Event) (string, progress.StagedProgressable, error) { 83 if err := checkEventType(e.Type, event.FileIndexingStarted); err != nil { 84 return "", nil, err 85 } 86 87 path, ok := e.Source.(string) 88 if !ok { 89 return "", nil, newPayloadErr(e.Type, "Source", e.Source) 90 } 91 92 prog, ok := e.Value.(progress.StagedProgressable) 93 if !ok { 94 return "", nil, newPayloadErr(e.Type, "Value", e.Value) 95 } 96 97 return path, prog, nil 98 } 99 100 func ParseCatalogerTaskStarted(e partybus.Event) (*monitor.CatalogerTask, error) { 101 if err := checkEventType(e.Type, event.CatalogerTaskStarted); err != nil { 102 return nil, err 103 } 104 105 source, ok := e.Source.(*monitor.CatalogerTask) 106 if !ok { 107 return nil, newPayloadErr(e.Type, "Source", e.Source) 108 } 109 110 return source, nil 111 } 112 113 func ParseAttestationStartedEvent(e partybus.Event) (io.Reader, progress.Progressable, *monitor.GenericTask, error) { 114 if err := checkEventType(e.Type, event.AttestationStarted); err != nil { 115 return nil, nil, nil, err 116 } 117 118 source, ok := e.Source.(monitor.GenericTask) 119 if !ok { 120 return nil, nil, nil, newPayloadErr(e.Type, "Source", e.Source) 121 } 122 123 sp, ok := e.Value.(*monitor.ShellProgress) 124 if !ok { 125 return nil, nil, nil, newPayloadErr(e.Type, "Value", e.Value) 126 } 127 128 return sp.Reader, sp.Progressable, &source, nil 129 } 130 131 // CLI event types 132 133 type UpdateCheck struct { 134 New string 135 Current string 136 } 137 138 func ParseCLIAppUpdateAvailable(e partybus.Event) (*UpdateCheck, error) { 139 if err := checkEventType(e.Type, event.CLIAppUpdateAvailable); err != nil { 140 return nil, err 141 } 142 143 updateCheck, ok := e.Value.(UpdateCheck) 144 if !ok { 145 return nil, newPayloadErr(e.Type, "Value", e.Value) 146 } 147 148 return &updateCheck, nil 149 } 150 151 func ParseCLIReport(e partybus.Event) (string, string, error) { 152 if err := checkEventType(e.Type, event.CLIReport); err != nil { 153 return "", "", err 154 } 155 156 context, ok := e.Source.(string) 157 if !ok { 158 // this is optional 159 context = "" 160 } 161 162 report, ok := e.Value.(string) 163 if !ok { 164 return "", "", newPayloadErr(e.Type, "Value", e.Value) 165 } 166 167 return context, report, nil 168 } 169 170 func ParseCLINotification(e partybus.Event) (string, string, error) { 171 if err := checkEventType(e.Type, event.CLINotification); err != nil { 172 return "", "", err 173 } 174 175 context, ok := e.Source.(string) 176 if !ok { 177 // this is optional 178 context = "" 179 } 180 181 notification, ok := e.Value.(string) 182 if !ok { 183 return "", "", newPayloadErr(e.Type, "Value", e.Value) 184 } 185 186 return context, notification, nil 187 }