-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpygfried.go
85 lines (68 loc) · 1.47 KB
/
pygfried.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
package pygfried
import (
"fmt"
"os"
"path/filepath"
"github.com/richardlehane/siegfried"
"github.com/richardlehane/siegfried/pkg/config"
"github.com/richardlehane/siegfried/pkg/core"
"github.com/richardlehane/siegfried/pkg/static"
)
var sf *siegfried.Siegfried
func load() *siegfried.Siegfried {
if sf != nil {
return sf
}
sf = static.New()
return sf
}
func identify(sf *siegfried.Siegfried, path string) ([]core.Identification, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return sf.Identify(f, filepath.Base(path), "")
}
type Result struct {
Path string
Identifiers []string
Known bool
Error string
}
func buildResult(path string, ids []core.Identification, err error) *Result {
res := &Result{
Path: path,
}
if err != nil {
res.Error = err.Error()
} else {
for _, id := range ids {
res.Identifiers = append(res.Identifiers, id.String())
res.Known = id.Known()
}
}
return res
}
func Identify(path string) (*Result, error) {
sf := load()
ids, err := identify(sf, path)
if err != nil {
return nil, err
}
r := buildResult(path, ids, err)
return r, err
}
func IdentifyAll(paths []string) ([]*Result, error) {
sf := load()
rs := make([]*Result, len(paths))
for idx, path := range paths {
ids, err := identify(sf, path)
rs[idx] = buildResult(path, ids, err)
}
return rs, nil
}
func Version() string {
v := config.Version()
return fmt.Sprintf("%d.%d.%d", v[0], v[1], v[2])
}