-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathos_cache.go
71 lines (59 loc) · 1.21 KB
/
os_cache.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
package devicedetector
import (
"log"
"github.com/gijsbers/go-pcre"
"github.com/robicode/device-detector/util"
)
type CachedOSVersion struct {
Regex string
Version string
}
type CachedOS struct {
Regex string
compileError error
compiledRegex pcre.Regexp
compiled bool
Name string
Version string
Versions []CachedOSVersion
}
type OSCache interface {
Find(userAgent string) *CachedOS
}
type EmbeddedOSCache struct {
osList []CachedOS
}
var osFiles = []string{
"oss.yml",
}
func NewEmbeddedOSCache() (*EmbeddedOSCache, error) {
files := NewCacheFileList(osFiles...)
oss, err := parseOSs(files)
if err != nil {
return nil, err
}
return &EmbeddedOSCache{
osList: oss,
}, nil
}
func (e *EmbeddedOSCache) Find(userAgent string) *CachedOS {
for _, os := range e.osList {
if !os.compiled && os.compileError == nil {
re, err := pcre.Compile(util.FixupRegex(os.Regex), pcre.CASELESS)
if err != nil {
os.compileError = err
log.Println(err)
continue
}
os.compiled = true
os.compiledRegex = re
}
if os.compileError == nil {
matcher := os.compiledRegex.MatcherString(userAgent, 0)
if matcher.Matches() {
return &os
}
}
}
return nil
}