-
Notifications
You must be signed in to change notification settings - Fork 0
/
udevadm.go
55 lines (45 loc) · 963 Bytes
/
udevadm.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
package drivelist
import (
"bufio"
"bytes"
"os/exec"
"strings"
"github.com/golang/glog"
)
type UdevAdmData struct {
DeviceName string
Attribs map[string]string
}
func GetUdevAdmData(name string) (*UdevAdmData, error) {
data, err := exec.Command("/bin/udevadm", "info", "--query=all", "--name="+name).Output()
if err != nil {
return nil, err
}
return parseUdevAdmData(name, data)
}
func parseUdevAdmData(name string, data []byte) (*UdevAdmData, error) {
d := &UdevAdmData{}
d.Attribs = make(map[string]string)
scanner := bufio.NewScanner(bytes.NewReader(data))
for scanner.Scan() {
t := scanner.Text()
if len(t) > 0 {
switch t[0] {
case 'N':
d.DeviceName = t[3:]
case 'L':
// Ignore
case 'S':
// Ignore
case 'P':
// Ignore
case 'E':
sp := strings.Split(t[3:], "=")
d.Attribs[sp[0]] = sp[1]
default:
glog.Warningf("Found unknown udevadm line: %s\n", t)
}
}
}
return d, nil
}