forked from zach-klippenstein/goadb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_info.go
87 lines (77 loc) · 1.87 KB
/
device_info.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
package adb
import (
"bufio"
"fmt"
"io"
"strings"
"github.com/pkg/errors"
)
type DeviceInfo struct {
// Must be always set.
Serial string
// Product, device, and model are not set in the short form.
Product string
Model string
Device string
// Only set for devices connected via USB.
USB string
}
// IsUSB returns true if the device is connected via USB.
// remove?
func (d DeviceInfo) IsUSB() bool {
return d.USB != ""
}
func FormatDeviceInfo(dd []DeviceInfo) string {
f := new(strings.Builder)
fmt.Fprintln(f, "Serial Product Model Device USB")
for _, d := range dd {
fmt.Fprintf(f, "%6s %7s %5s %6s %3s\n",
d.Serial, d.Product, d.Model, d.Device, d.USB)
}
return f.String()
}
func parseDeviceList(list io.Reader, lineParseFunc func(string) (DeviceInfo, error)) ([]DeviceInfo, error) {
devices := make([]DeviceInfo, 0, 4)
scanner := bufio.NewScanner(list)
for scanner.Scan() {
device, err := lineParseFunc(scanner.Text())
if err != nil {
return nil, err
}
devices = append(devices, device)
}
return devices, nil
}
func parseDeviceShort(line string) (DeviceInfo, error) {
fields := strings.Fields(line)
if len(fields) != 2 {
return DeviceInfo{}, errors.Errorf(
"malformed device line, expected 2 fields but found %d", len(fields))
}
return DeviceInfo{Serial: fields[0]}, nil
}
func parseDeviceLong(line string) (DeviceInfo, error) {
fields := strings.Fields(line)
if len(fields) < 5 {
return DeviceInfo{}, errors.Errorf(
"malformed device line, expected at least 5 fields but found %d", len(fields))
}
di := DeviceInfo{Serial: fields[0]}
for _, field := range fields[2:] {
split := strings.Split(field, ":")
if len(split) != 2 {
continue
}
switch s := split[1]; split[0] {
case "product":
di.Product = s
case "model":
di.Model = s
case "device":
di.Device = s
case "usb":
di.USB = s
}
}
return di, nil
}