Skip to content

Commit baf007f

Browse files
committed
fix: check for all possible adb states when parsing device output
1 parent 898b02c commit baf007f

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

pkg/actions/tools/adb/device.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,33 @@ import (
77
)
88

99
// ActionDevices completes adb device serial numbers
10+
//
11+
// Device states from https://android.googlesource.com/platform/packages/modules/adb/+/refs/heads/main/adb.cpp
1012
func ActionDevices() carapace.Action {
1113
return carapace.ActionExecCommand("adb", "devices", "-l")(func(output []byte) carapace.Action {
1214
vals := make([]string, 0)
1315

16+
validStates := map[string]bool{
17+
"device": true,
18+
"offline": true,
19+
"bootloader": true,
20+
"recovery": true,
21+
"rescue": true,
22+
"sideload": true,
23+
"unauthorized": true,
24+
"authorizing": true,
25+
"connecting": true,
26+
"detached": true,
27+
}
28+
1429
for _, line := range strings.Split(string(output), "\n") {
1530
fields := strings.Fields(line)
16-
if len(fields) < 2 || fields[1] != "device" {
31+
if len(fields) < 2 || !validStates[fields[1]] {
1732
continue
1833
}
1934

2035
serial := fields[0]
36+
state := fields[1]
2137
props := make(map[string]string)
2238
for _, field := range fields[2:] {
2339
if key, value, ok := strings.Cut(field, ":"); ok {
@@ -29,9 +45,18 @@ func ActionDevices() carapace.Action {
2945
if model := props["model"]; model != "" {
3046
desc = strings.ReplaceAll(model, "_", " ")
3147
desc = strings.ReplaceAll(desc, "x86 64", "x86_64")
32-
if usb := props["usb"]; usb != "" {
33-
desc += " (usb:" + usb + ")"
48+
}
49+
if state != "device" {
50+
if desc != "" {
51+
desc += " "
52+
}
53+
desc += "[" + state + "]"
54+
}
55+
if usb := props["usb"]; usb != "" {
56+
if desc != "" {
57+
desc += " "
3458
}
59+
desc += "(usb:" + usb + ")"
3560
}
3661

3762
vals = append(vals, serial, desc)

0 commit comments

Comments
 (0)