From 0cc529adb8ec618b9e43408082de2a73279e2239 Mon Sep 17 00:00:00 2001 From: ned-lambdatest Date: Wed, 18 Aug 2021 12:14:04 +0530 Subject: [PATCH 1/2] New device state string add to handle authorizing | Watch flag added to the main file --- README.md | 55 +++++++++++++++++++++++++++++++++++++++++++ cmd/adb/main.go | 46 +++++++++++++++++++++++------------- device_state.go | 2 ++ devicestate_string.go | 4 ++-- 4 files changed, 89 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 457c9cf..b49ad0c 100644 --- a/README.md +++ b/README.md @@ -6,3 +6,58 @@ A Golang library for interacting with the Android Debug Bridge (adb). See [demo.go](cmd/demo/demo.go) for usage. + +## Usage + +go run cmd/adb/main.go + +``` +usage: main [] [ ...] + +Flags: + --help Show context-sensitive help (also try --help-long and --help-man). + -s, --serial=SERIAL Connect to device by serial number. + +Commands: + help [...] + Show help. + + shell [...] + Run a shell command on the device. + + devices [] + List devices. + + pull [] [] + Pull a file from the device. + + push [] + Push a file to the device. + +``` + +## Get List of devices + +``` +go run cmd/adb/main.go devices + +Output: +ABCDEFGH + +``` + +## Watch for the device list + +``` +go run cmd/adb/main.go devices --watch + +Output: +{Serial:ABCDEFGH OldState:StateDisconnected NewState:StateOnline} +{Serial:ABCDEFGH OldState:StateOnline NewState:StateOffline} +{Serial:ABCDEFGH OldState:StateOffline NewState:StateDisconnected} +{Serial:ABCDEFGH OldState:StateDisconnected NewState:StateOffline} +{Serial:ABCDEFGH OldState:StateOffline NewState:StateAuthorizing} +{Serial:ABCDEFGH OldState:StateAuthorizing NewState:StateOffline} +{Serial:ABCDEFGH OldState:StateOffline NewState:StateOnline} + +``` \ No newline at end of file diff --git a/cmd/adb/main.go b/cmd/adb/main.go index 07d5eac..181af74 100644 --- a/cmd/adb/main.go +++ b/cmd/adb/main.go @@ -9,7 +9,6 @@ import ( "time" "github.com/cheggaaa/pb" - "github.com/zach-klippenstein/goadb" "gopkg.in/alecthomas/kingpin.v2" ) @@ -33,6 +32,10 @@ var ( "Include extra detail about devices."). Short('l'). Bool() + devicesWatchFlag = devicesCommand.Flag("watch", + "Device watch for the online and offline devices"). + Short('w'). + Bool() pullCommand = kingpin.Command("pull", "Pull a file from the device.") @@ -78,7 +81,7 @@ func main() { switch kingpin.Parse() { case "devices": - exitCode = listDevices(*devicesLongFlag) + exitCode = listDevices(*devicesLongFlag, *devicesWatchFlag) case "shell": exitCode = runShellCommand(*shellCommandArg, parseDevice()) case "pull": @@ -98,24 +101,35 @@ func parseDevice() adb.DeviceDescriptor { return adb.AnyDevice() } -func listDevices(long bool) int { +func listDevices(long bool, watch bool) int { //client := adb.New(server) - devices, err := client.ListDevices() - if err != nil { - fmt.Fprintln(os.Stderr, "error:", err) - } - for _, device := range devices { - if long { - if device.Usb == "" { - fmt.Printf("%s\tproduct:%s model:%s device:%s\n", - device.Serial, device.Product, device.Model, device.DeviceInfo) + // Listen to the watch flag + if watch { + watcher := client.NewDeviceWatcher() + for event := range watcher.C() { + fmt.Printf("%+v\n", event) + } + if watcher.Err() != nil { + fmt.Print(watcher.Err().Error()) + } + } else { + devices, err := client.ListDevices() + if err != nil { + fmt.Fprintln(os.Stderr, "error:", err) + } + for _, device := range devices { + if long { + if device.Usb == "" { + fmt.Printf("%s\tproduct:%s model:%s device:%s\n", + device.Serial, device.Product, device.Model, device.DeviceInfo) + } else { + fmt.Printf("%s\tusb:%s product:%s model:%s device:%s\n", + device.Serial, device.Usb, device.Product, device.Model, device.DeviceInfo) + } } else { - fmt.Printf("%s\tusb:%s product:%s model:%s device:%s\n", - device.Serial, device.Usb, device.Product, device.Model, device.DeviceInfo) + fmt.Println(device.Serial) } - } else { - fmt.Println(device.Serial) } } diff --git a/device_state.go b/device_state.go index 7657e40..00acb7f 100644 --- a/device_state.go +++ b/device_state.go @@ -12,6 +12,7 @@ type DeviceState int8 const ( StateInvalid DeviceState = iota + StateAuthorizing StateUnauthorized StateDisconnected StateOffline @@ -23,6 +24,7 @@ var deviceStateStrings = map[string]DeviceState{ "offline": StateOffline, "device": StateOnline, "unauthorized": StateUnauthorized, + "authorizing": StateAuthorizing, } func parseDeviceState(str string) (DeviceState, error) { diff --git a/devicestate_string.go b/devicestate_string.go index 9bc6e1c..fa7996a 100644 --- a/devicestate_string.go +++ b/devicestate_string.go @@ -4,9 +4,9 @@ package adb import "fmt" -const _DeviceState_name = "StateInvalidStateUnauthorizedStateDisconnectedStateOfflineStateOnline" +const _DeviceState_name = "StateInvalidStateAuthorizingStateUnauthorizedStateDisconnectedStateOfflineStateOnline" -var _DeviceState_index = [...]uint8{0, 12, 29, 46, 58, 69} +var _DeviceState_index = [...]uint8{0, 12, 28, 45, 62, 74, 85} func (i DeviceState) String() string { if i < 0 || i >= DeviceState(len(_DeviceState_index)-1) { From db732099c9cc0c8b8f1b0b1a4b65bc8a109d5cb1 Mon Sep 17 00:00:00 2001 From: ned-lambdatest Date: Wed, 18 Aug 2021 18:31:50 +0530 Subject: [PATCH 2/2] Import the missing mod --- cmd/adb/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/adb/main.go b/cmd/adb/main.go index 181af74..65b03d2 100644 --- a/cmd/adb/main.go +++ b/cmd/adb/main.go @@ -9,6 +9,7 @@ import ( "time" "github.com/cheggaaa/pb" + adb "github.com/zach-klippenstein/goadb" "gopkg.in/alecthomas/kingpin.v2" )