-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapusb.go
More file actions
47 lines (35 loc) · 949 Bytes
/
mapusb.go
File metadata and controls
47 lines (35 loc) · 949 Bytes
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
package map_usb_devices
import (
"errors"
"sort"
"strings"
)
type DeviceInfo struct {
Manufacturer string `json:"manufacturer"`
Product string `json:"product"`
SerialNumber string `json:"serialNumber`
Description string `json:"description"`
}
func GetUsbDeviceMap(root, dev string, product, vendor uint16) (map[string]string, error) {
pPorts, err := GetPluggedDevices(root, dev)
if err != nil {
return nil, err
}
sort.Strings(pPorts)
resMap := make(map[string]string, len(pPorts))
pDevices, err := GetDevicesInfo(product, vendor)
if err != nil {
return nil, err
}
sort.Strings(pDevices)
if len(pPorts) != len(pDevices) {
return nil, errors.New("Error comparison ports and devices")
}
for i, d := range pDevices {
if !strings.Contains(pPorts[i], d) {
return nil, errors.New("Error between ports and devices: values not aligned, check rules.d file")
}
resMap[pPorts[i]] = d
}
return resMap, nil
}