forked from itchio/wizardry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoidentify.go
65 lines (48 loc) · 1.1 KB
/
doidentify.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 golibmagic
import (
"fmt"
"os"
"github.com/pkg/errors"
"github.com/postfix/golibmagic/interpreter"
"github.com/postfix/golibmagic/parser"
"github.com/postfix/golibmagic/util"
)
func doIdentify() error {
magdir := *identifyArgs.magdir
NoLogf := func(format string, args ...interface{}) {}
Logf := func(format string, args ...interface{}) {
fmt.Println(fmt.Sprintf(format, args...))
}
pctx := &parser.ParseContext{
Logf: NoLogf,
}
if *appArgs.debugParser {
pctx.Logf = Logf
}
book := make(parser.Spellbook)
err := pctx.ParseAll(magdir, book)
if err != nil {
return errors.WithStack(err)
}
target := *identifyArgs.target
targetReader, err := os.Open(target)
if err != nil {
panic(err)
}
defer targetReader.Close()
stat, _ := targetReader.Stat()
ictx := &interpreter.InterpretContext{
Logf: NoLogf,
Book: book,
}
if *appArgs.debugInterpreter {
ictx.Logf = Logf
}
sr := util.NewSliceReader(targetReader, 0, stat.Size())
result, err := ictx.Identify(sr)
if err != nil {
panic(err)
}
fmt.Printf("%s: %s\n", target, util.MergeStrings(result))
return nil
}