From d180376e07c381d02bca7ec4aa37919221f5f056 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 21 May 2015 23:05:11 +0200 Subject: [PATCH] Implement device listing in adb --- adb/adb.go | 30 ++++++++++++++++++++++++++---- cmd/fdroidcl/main.go | 10 ++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/adb/adb.go b/adb/adb.go index 0336c21..41b831e 100644 --- a/adb/adb.go +++ b/adb/adb.go @@ -4,7 +4,9 @@ package adb import ( + "bufio" "os/exec" + "regexp" ) func StartServer() error { @@ -16,12 +18,32 @@ func StartServer() error { return nil } -func DeviceList() ([]string, error) { +type Device string + +var deviceRegex = regexp.MustCompile(`^(.+)\tdevice`) + +func Devices() ([]Device, error) { cmd := exec.Command("adb", "devices") - stdout, err := cmd.Output() + stdout, err := cmd.StdoutPipe() if err != nil { return nil, err } - _ = stdout - return nil, nil + if err := cmd.Start(); err != nil { + return nil, err + } + var devices []Device + scanner := bufio.NewScanner(stdout) + for scanner.Scan() { + line := scanner.Text() + m := deviceRegex.FindStringSubmatch(line) + if len(m) < 2 { + continue + } + device := Device(m[1]) + if device == "" { + continue + } + devices = append(devices, device) + } + return devices, nil } diff --git a/cmd/fdroidcl/main.go b/cmd/fdroidcl/main.go index 5103450..9c041d4 100644 --- a/cmd/fdroidcl/main.go +++ b/cmd/fdroidcl/main.go @@ -12,6 +12,7 @@ import ( "strings" "github.com/mvdan/fdroidcl" + "github.com/mvdan/fdroidcl/adb" ) func appMatches(fields []string, terms []string) bool { @@ -73,6 +74,7 @@ func init() { p(" list List all available apps") p(" search Search available apps") p(" show Show detailed info of an app") + p(" devices List connected devices") } } @@ -124,6 +126,14 @@ func main() { } app.WriteDetailed(os.Stdout) } + case "devices": + devices, err := adb.Devices() + if err != nil { + log.Fatalf("Could not list devices: %v", err) + } + for _, device := range devices { + fmt.Println(device) + } default: log.Printf("Unrecognised command '%s'\n\n", cmd) flag.Usage()