-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathusb_enum_darwin.go
45 lines (39 loc) · 934 Bytes
/
usb_enum_darwin.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
package main
import (
"bufio"
"bytes"
"os/exec"
"regexp"
"strings"
)
var deviceRE = regexp.MustCompile(`^(.*):$`)
var locationRE = regexp.MustCompile(`^Location ID: (.*)$`)
//TODO: Call library directly
func enumerateDevices() ([]device, error) {
if err := checkExe("system_profiler"); err != nil {
return nil, err
}
out, err := exec.Command("system_profiler", "SPUSBDataType").Output()
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(bytes.NewReader(out))
var (
deviceList []device
deviceName string
)
for scanner.Scan() {
line := strings.Trim(scanner.Text(), " ")
switch {
case locationRE.MatchString(line):
m := locationRE.FindStringSubmatch(line)[1]
if deviceName != "" {
deviceList = append(deviceList, device{deviceName, m})
}
deviceName = ""
case deviceRE.MatchString(line):
deviceName = deviceRE.FindStringSubmatch(line)[1]
}
}
return deviceList, nil
}