-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathexpr.go
52 lines (41 loc) · 956 Bytes
/
expr.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
package formatter
import (
"fmt"
"github.com/expr-lang/expr"
)
// filterExpr filters NMAPRun.Hosts by given expression
func filterExpr(r NMAPRun, code string) (NMAPRun, error) {
program, err := expr.Compile(
fmt.Sprintf("filter(Host, { %s })", code),
expr.Env(r),
)
if err != nil {
return r, err
}
output, err := expr.Run(program, r)
if err != nil {
return r, err
}
hosts, err := convertToHosts(output)
if err != nil {
return r, err
}
r.Host = hosts
return r, nil
}
// convertToHosts converts output from expression engine to []Host
func convertToHosts(output interface{}) ([]Host, error) {
outputInterfaces, ok := output.([]interface{})
if !ok {
return nil, fmt.Errorf("output is not []interface{}")
}
hosts := make([]Host, len(outputInterfaces))
for i, v := range outputInterfaces {
host, ok := v.(Host)
if !ok {
return nil, fmt.Errorf("element is not Host")
}
hosts[i] = host
}
return hosts, nil
}