From d1da3deb8d8ddb3634cf62777e476d842171f2e3 Mon Sep 17 00:00:00 2001 From: Wesley van Tilburg Date: Mon, 13 Oct 2025 19:04:32 +0200 Subject: [PATCH] adb: Allow raw command execution --- adb.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 +++ types.go | 5 +++++ 3 files changed, 53 insertions(+) create mode 100644 adb.go create mode 100644 go.mod create mode 100644 types.go diff --git a/adb.go b/adb.go new file mode 100644 index 0000000..f68dcb5 --- /dev/null +++ b/adb.go @@ -0,0 +1,45 @@ +package goadb + +import ( + "fmt" + "os/exec" + "strings" +) + +// Get the already connected adb devices +func GetConnectedDevices() ([]Device, error) { + var devices []Device + + output, err := ExecuteCommand("devices") + if err != nil { + return nil, fmt.Errorf("Failed to determine device status: %w", err) + } + + lines := strings.Split(string(output), "\n") + for _, line := range lines { + if strings.Contains(line, "device") && !strings.Contains(line, "List of") { + fields := strings.Fields(line) + if len(fields) >= 2 && fields[1] == "device" { + devices = append(devices, Device{Serial: fields[0]}) + } + } + } + + return devices, nil +} + +// Execute a raw adb command and return the output +func ExecuteCommand(command string) ([]byte, error) { + + //Check if adb is available + if _, err := exec.LookPath("adb"); err != nil { + return nil, fmt.Errorf("adb binary not found in PATH") + } + + cmd := exec.Command("sh", "-c", "adb "+command) + output, err := cmd.CombinedOutput() + if err != nil { + return nil, fmt.Errorf("command failed: %v", err) + } + return output, nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2ed2669 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.plabble.org/misthios/goadb + +go 1.24.6 diff --git a/types.go b/types.go new file mode 100644 index 0000000..1766f5e --- /dev/null +++ b/types.go @@ -0,0 +1,5 @@ +package goadb + +type Device struct { + Serial string +}