-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.go
161 lines (130 loc) · 3.15 KB
/
parse.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package devicedetector
import (
"log"
"github.com/robicode/device-detector/util"
"gopkg.in/yaml.v3"
)
func parseEmbeddedDeviceFile(filename string) (*CachedDeviceList, error) {
devices := make(map[string]CachedDevice)
bytes, err := embeddedData.ReadFile(filename)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(bytes, &devices)
if err != nil {
return nil, err
}
deviceList := NewDeviceList()
deviceList.list = devices
return deviceList, nil
}
func parseDevices(fileList *CacheFileList) (map[string]CachedDeviceList, error) {
deviceTree := make(map[string]CachedDeviceList)
for _, filename := range fileList.filenames {
list, err := parseEmbeddedDeviceFile(filename)
if err != nil {
return deviceTree, err
}
deviceTree[filename] = *list
}
return deviceTree, nil
}
func parseBots(fileList *CacheFileList) ([]CachedBot, error) {
var bots []CachedBot
for _, filename := range fileList.filenames {
fnBots, err := parseEmbeddedBotsFile(filename)
if err != nil {
return nil, err
}
bots = append(bots, fnBots...)
}
bots = util.ReverseArray(bots)
return bots, nil
}
func parseEmbeddedBotsFile(filename string) ([]CachedBot, error) {
var bots []CachedBot
bytes, err := embeddedData.ReadFile(filename)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(bytes, &bots)
if err != nil {
return nil, err
}
return bots, nil
}
func parseEmbeddedOSFile(filename string) ([]CachedOS, error) {
var oss []CachedOS
bytes, err := embeddedData.ReadFile(filename)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(bytes, &oss)
if err != nil {
return nil, err
}
return oss, nil
}
func parseOSs(fileList *CacheFileList) ([]CachedOS, error) {
var oss []CachedOS
for _, filename := range fileList.filenames {
fnOSs, err := parseEmbeddedOSFile(filename)
if err != nil {
return nil, err
}
oss = append(oss, fnOSs...)
}
return oss, nil
}
func parseEmbeddedHintsFile(filename string) (map[string]string, error) {
output := make(map[string]string)
data, err := embeddedData.ReadFile(filename)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(data, &output)
if err != nil {
return nil, err
}
return output, err
}
func parseHints(fileList *CacheFileList) (map[string]string, error) {
list := make(map[string]string)
for _, filename := range fileList.filenames {
m, err := parseEmbeddedHintsFile(filename)
if err != nil {
return nil, err
}
for name, item := range m {
if _, ok := list[name]; ok {
log.Println("Item already in list:", name)
} else {
list[name] = item
}
}
}
return list, nil
}
func parseEmbeddedClientFile(filename string) ([]CachedClient, error) {
var clients []CachedClient
data, err := embeddedData.ReadFile(filename)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(data, &clients)
if err != nil {
return nil, err
}
return clients, nil
}
func parseClients(fileList *CacheFileList) ([]CachedClient, error) {
var clients []CachedClient
for _, filename := range fileList.filenames {
list, err := parseEmbeddedClientFile(filename)
if err != nil {
return clients, err
}
clients = append(clients, list...)
}
return clients, nil
}