Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New state added for the authorising | Device watch implemented in the main.go #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>] <command> [<args> ...]

Flags:
--help Show context-sensitive help (also try --help-long and --help-man).
-s, --serial=SERIAL Connect to device by serial number.

Commands:
help [<command>...]
Show help.

shell [<command>...]
Run a shell command on the device.

devices [<flags>]
List devices.

pull [<flags>] <remote> [<local>]
Pull a file from the device.

push [<flags>] <local> <remote>
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}

```
47 changes: 31 additions & 16 deletions cmd/adb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/cheggaaa/pb"
"github.com/zach-klippenstein/goadb"
adb "github.com/zach-klippenstein/goadb"
"gopkg.in/alecthomas/kingpin.v2"
)

Expand All @@ -33,6 +33,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.")
Expand Down Expand Up @@ -78,7 +82,7 @@ func main() {

switch kingpin.Parse() {
case "devices":
exitCode = listDevices(*devicesLongFlag)
exitCode = listDevices(*devicesLongFlag, *devicesWatchFlag)
case "shell":
exitCode = runShellCommand(*shellCommandArg, parseDevice())
case "pull":
Expand All @@ -98,24 +102,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)
}
}

Expand Down
2 changes: 2 additions & 0 deletions device_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type DeviceState int8

const (
StateInvalid DeviceState = iota
StateAuthorizing
StateUnauthorized
StateDisconnected
StateOffline
Expand All @@ -23,6 +24,7 @@ var deviceStateStrings = map[string]DeviceState{
"offline": StateOffline,
"device": StateOnline,
"unauthorized": StateUnauthorized,
"authorizing": StateAuthorizing,
}

func parseDeviceState(str string) (DeviceState, error) {
Expand Down
4 changes: 2 additions & 2 deletions devicestate_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.