-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.go
72 lines (61 loc) · 1.32 KB
/
main.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"
recog "github.com/runZeroInc/recog-go"
)
func visit(files *[]string) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatal(err)
}
if info.IsDir() || filepath.Ext(path) != ".xml" {
return nil
}
*files = append(*files, path)
return nil
}
}
func fingerprint(fingerprints []recog.FingerprintDB, text string) {
for _, fdb := range fingerprints {
match := fdb.MatchFirst(text)
if match.Matched {
j, _ := json.Marshal(match.Values)
fmt.Printf("%s\n", j)
}
}
}
func main() {
var files []string
if len(os.Args) < 2 {
log.Fatalf("missing: recog xml directory")
}
err := filepath.Walk(os.Args[1], visit(&files))
if err != nil {
log.Fatal(err)
}
var fingerprints []recog.FingerprintDB
for _, file := range files {
fdb, err := recog.LoadFingerprintDBFromFile(file)
if err != nil {
log.Fatalf("error loading fingerprints from %s: %s", file, err)
}
fingerprints = append(fingerprints, fdb)
}
var text string
text = strings.Join(os.Args[2:], " ")
if len(text) < 1 {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
text = scanner.Text()
fingerprint(fingerprints, text)
}
} else {
fingerprint(fingerprints, text)
}
}