Skip to content

Commit

Permalink
add command: adb devices
Browse files Browse the repository at this point in the history
  • Loading branch information
hzsunshx committed Dec 17, 2018
1 parent 83dffb8 commit 12b73a8
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ go.sum

# User defined
ya
*.apk
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
- [x] screenshot
- [x] install support http url
- [x] support launch after install apk
- [x] support `fa devices --json`
- [ ] install apk and auto click confirm
- [.] check device health status
- [ ] show current app
- [ ] unlock device
- [ ] reset device state, clean up installed packages
- [ ] support `fa devices --json`
- [ ] show wlan (ip,mac,signal), enable and disable it
- [ ] share device to public web
- [ ] install ipa support
Expand All @@ -35,6 +35,15 @@ download binary from [**releases**](https://github.com/codeskyblue/fa/releases)
```bash
$ fa version
fa version v0.0.5 # just example
adb server version 0028
```

### Show devices
- [x] Remove header `List of devices attached` to make it easy parse

```bash
$ fa devices
3578298f device
```

### Run adb command with device select
Expand All @@ -59,7 +68,7 @@ $ ANDROID_SERIAL=3578298 fa adb shell pwd
```
### Screenshot
only `png` format
only `png` format now.
```bash
fa screenshot -o screenshot.png
Expand Down Expand Up @@ -91,14 +100,19 @@ Launch io.appium.android.apis ...
```
## Reference
Articles
- <https://github.com/mzlogin/awesome-adb>
- [Facebook One World Project](https://code.fb.com/android/managing-resources-for-large-scale-testing/)
- [Facebook Device Lab](https://code.fb.com/android/the-mobile-device-lab-at-the-prineville-data-center/)
- Article reverse ssh tunnling <https://www.howtoforge.com/reverse-ssh-tunneling>
- [openstf/adbkit](https://github.com/openstf/adbkit)
- [ADB Source Code](https://github.com/aosp-mirror/platform_system_core/blob/master/adb)
- [OVERVIEW.TXT](https://github.com/aosp-mirror/platform_system_core/blob/master/adb/OVERVIEW.TXT)
- [SERVICES.TXT](https://github.com/aosp-mirror/platform_system_core/blob/master/adb/SERVICES.TXT)
- [SYNC.TXT](https://github.com/aosp-mirror/platform_system_core/blob/master/adb/SYNC.TXT)
Libs might be useful
Expand Down
2 changes: 1 addition & 1 deletion adb.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ type AdbClient struct {

func NewAdbClient() *AdbClient {
return &AdbClient{
Addr: "127.0.0.1:5037",
Addr: defaultHost + ":" + strconv.Itoa(defaultPort),
}
}

Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ module github.com/codeskyblue/fa

require (
github.com/cavaliercoder/grab v2.0.0+incompatible
github.com/kr/pty v1.1.3 // indirect
github.com/manifoldco/promptui v0.3.2
github.com/mattn/go-runewidth v0.0.3 // indirect
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
github.com/pkg/errors v0.8.0
github.com/shogo82148/androidbinary v0.0.0-20180627093851-01c4bfa8b3b5
gopkg.in/cheggaaa/pb.v1 v1.0.25
golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06 // indirect
golang.org/x/tools v0.0.0-20181214171254-3c39ce7b6105 // indirect
gopkg.in/cheggaaa/pb.v1 v1.0.27
gopkg.in/urfave/cli.v1 v1.20.0
)
70 changes: 64 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"log"
Expand All @@ -19,11 +20,14 @@ var (
version = "develop"
debug = false
defaultSerial string
defaultHost string
defaultPort int
)

type Device struct {
Serial string
Description string
Serial string `json:"serial"`
Status string `json:"status"`
Description string `json:"-"`
}

func (d *Device) String() string {
Expand All @@ -47,6 +51,23 @@ func shortDeviceInfo(s string) string {
}

func listDevices() (ds []Device, err error) {
output, err := exec.Command("adb", "devices").CombinedOutput()
if err != nil {
return
}
re := regexp.MustCompile(`(?m)^([^\s]+)\s+(device|offline|unauthorized)\s*$`)
matches := re.FindAllStringSubmatch(string(output), -1)
for _, m := range matches {
status := m[2]
ds = append(ds, Device{
Serial: m[1],
Status: status,
})
}
return
}

func listDetailedDevices() (ds []Device, err error) {
output, err := exec.Command("adb", "devices", "-l").CombinedOutput()
if err != nil {
return
Expand Down Expand Up @@ -89,7 +110,7 @@ func choose(devices []Device) Device {
}

func chooseOne() (serial string, err error) {
devices, err := listDevices()
devices, err := listDetailedDevices()
if err != nil {
return
}
Expand Down Expand Up @@ -156,20 +177,32 @@ func main() {
EnvVar: "ANDROID_SERIAL",
Destination: &defaultSerial,
},
cli.StringFlag{
Name: "host, H",
Usage: "name of adb server host",
Value: "localhost",
Destination: &defaultHost,
},
cli.IntFlag{
Name: "port, P",
Usage: "port of adb server",
Value: 5037,
Destination: &defaultPort,
},
}
app.Commands = []cli.Command{
{
Name: "version",
Usage: "show version",
Action: func(ctx *cli.Context) error {
fmt.Printf("fa version %s\n", version)
adbVersion, err := DefaultAdbClient.Version()
fmt.Printf("fa version %s\n", version)
adbVersion, err := NewAdbClient().Version()
if err != nil {
fmt.Printf("adb version err: %v\n", err)
return err
}
fmt.Println("adb version", adbVersion)
fmt.Println("adb path", adbPath())
fmt.Println("adb server version", adbVersion)
return nil
// output, err := exec.Command(adbPath(), "version").Output()
// for _, line := range strings.Split(string(output), "\n") {
Expand All @@ -178,6 +211,31 @@ func main() {
// return err
},
},
{
Name: "devices",
Usage: "show connected devices",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "json",
Usage: "output json format",
},
},
Action: func(ctx *cli.Context) error {
ds, err := listDevices()
if err != nil {
return err
}
if ctx.Bool("json") {
data, _ := json.MarshalIndent(ds, "", " ")
fmt.Println(string(data))
} else {
for _, d := range ds {
fmt.Printf("%s\t%s\n", d.Serial, d.Status)
}
}
return nil
},
},
{
Name: "adb",
Usage: "exec adb with device select",
Expand Down
8 changes: 7 additions & 1 deletion screenshot.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
"fmt"
"log"
"os"
"runtime"
"time"

"github.com/pkg/browser"
// "github.com/urfave/cli"
Expand Down Expand Up @@ -36,14 +39,17 @@ func takeScreenshot(serial, output string) error {
return c.Run()
}
screencap := func() error {
tmpPath := "/sdcard/fa-screenshot.png"
tmpPath := fmt.Sprintf("/sdcard/fa-screenshot-%d.png", time.Now().UnixNano())
c := adbCommand(serial, "shell", "screencap", "-p", tmpPath)
if err := c.Run(); err != nil {
return err
}
defer adbCommand(serial, "shell", "rm", tmpPath).Run()
return adbCommand(serial, "pull", tmpPath, output).Run()
}
if runtime.GOOS == "windows" {
return screencap()
}
return anyFuncs(execOut, screencap)
}

Expand Down

0 comments on commit 12b73a8

Please sign in to comment.