-
Notifications
You must be signed in to change notification settings - Fork 2
/
event_types.go
65 lines (56 loc) · 1.38 KB
/
event_types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"errors"
"path"
"path/filepath"
"plugin"
"github.com/patrobinson/go-fish/event"
log "github.com/sirupsen/logrus"
)
type eventType interface {
Name() string
Decode([]byte) (event.Event, error)
}
func getEventTypes(eventFolder string) ([]eventType, error) {
var events []eventType
evtGlob := path.Join(eventFolder, "/*.so")
evt, err := filepath.Glob(evtGlob)
if err != nil {
return events, err
}
var plugins []*plugin.Plugin
for _, pFile := range evt {
if plug, err := plugin.Open(pFile); err == nil {
plugins = append(plugins, plug)
}
}
for _, p := range plugins {
symEvt, err := p.Lookup("EventType")
if err != nil {
log.Errorf("Event Type has no eventType symbol: %v", err)
continue
}
e, ok := symEvt.(eventType)
if !ok {
log.Errorf("Event Type is not an Event interface type")
continue
}
events = append(events, e)
}
log.Infof("Found %v event types", len(events))
return events, nil
}
func matchEventType(eventTypes []eventType, rawEvt interface{}) (event.Event, error) {
var evt event.Event
for _, et := range eventTypes {
byteEvt, ok := rawEvt.([]byte)
if !ok {
return evt, errors.New("Could not decode raw event to byte array")
}
if evt, err := et.Decode(byteEvt); err == nil {
log.Debugf("Matched event to type %s", et.Name())
return evt, nil
}
}
return evt, errors.New("No Event Type matched")
}