/
core.go
102 lines (85 loc) · 2.33 KB
/
core.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
*/
package plugin
import (
"fmt"
"log"
"net/rpc"
"net/rpc/jsonrpc"
"os"
"path/filepath"
"github.com/hosom/gomandrake/config"
"github.com/natefinch/pie"
)
// AnalyzerCaller is a wrapper specifically intended to be utilized for
// wrapping Analyzer plugins.
type AnalyzerCaller struct {
Name string
Path string
Args []string
MimeFilter []string
client *rpc.Client
}
// allTypeAnalyzer is a function to check if a plugin should be ran against
// all mimetypes. If this is the case, then we will make some behind the
// scenes configuration optimizations to prevent multiple executions of the
// same plugin.
func allTypeAnalyzer(m []string) bool {
for _, filter := range m {
if filter == "all" {
return true
}
}
return false
}
func NewAnalyzerCaller(c config.AnalyzerConfig) AnalyzerCaller {
a := AnalyzerCaller{}
a.Path = c.Path
a.Name = filepath.Base(a.Path)
a.Args = c.Args
if allTypeAnalyzer(c.MimeFilter) == true {
a.MimeFilter = []string{"all"}
} else {
a.MimeFilter = c.MimeFilter
}
client, err := pie.StartProviderCodec(jsonrpc.NewClientCodec, os.Stderr, a.Path, a.Args...)
if err != nil {
log.Println(err)
log.Fatalf("Error starting plugin: %s", a.Name)
}
a.client = client
return a
}
// Analyze sends a filepath location to an analyzer plugin for the plugin
// to perform analysis on.
func (a AnalyzerCaller) Analyze(fmeta string) (result string, err error) {
err = a.client.Call(fmt.Sprintf("%s.Analyze", a.Name), fmeta, &result)
return result, err
}
// LoggerCaller is a wrapper specifically intended to be utilized for
// wrapping Logger plugins.
type LoggerCaller struct {
Name string
Path string
Args []string
client *rpc.Client
}
func NewLoggerCaller(c config.LoggerConfig) LoggerCaller {
l := LoggerCaller{}
l.Path = c.Path
l.Name = filepath.Base(l.Path)
l.Args = c.Args
client, err := pie.StartProviderCodec(jsonrpc.NewClientCodec, os.Stderr, l.Path, l.Args...)
if err != nil {
log.Fatalf("Error starting plugin: %s", l.Name)
}
l.client = client
return l
}
// Log sends a json message to a logger plugin describing analysis that
// has been completed.
func (l LoggerCaller) Log(msg string) (result string, err error) {
log.Printf("Dispatching call to logger: %s", l.Name)
err = l.client.Call(fmt.Sprintf("%s.Log", l.Name), msg, &result)
return result, err
}