Skip to content

Commit cb55d61

Browse files
committed
2 parents 580b9e4 + f7149d9 commit cb55d61

File tree

6 files changed

+95
-12
lines changed

6 files changed

+95
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ go.sum
1515

1616
# User defined
1717
ya
18+
*.apk

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
- [x] screenshot
99
- [x] install support http url
1010
- [x] support launch after install apk
11+
- [x] support `fa devices --json`
1112
- [ ] install apk and auto click confirm
1213
- [.] check device health status
1314
- [ ] show current app
1415
- [ ] unlock device
1516
- [ ] reset device state, clean up installed packages
16-
- [ ] support `fa devices --json`
1717
- [ ] show wlan (ip,mac,signal), enable and disable it
1818
- [ ] share device to public web
1919
- [ ] install ipa support
@@ -35,6 +35,15 @@ download binary from [**releases**](https://github.com/codeskyblue/fa/releases)
3535
```bash
3636
$ fa version
3737
fa version v0.0.5 # just example
38+
adb server version 0028
39+
```
40+
41+
### Show devices
42+
- [x] Remove header `List of devices attached` to make it easy parse
43+
44+
```bash
45+
$ fa devices
46+
3578298f device
3847
```
3948

4049
### Run adb command with device select
@@ -59,7 +68,7 @@ $ ANDROID_SERIAL=3578298 fa adb shell pwd
5968
```
6069
6170
### Screenshot
62-
only `png` format
71+
only `png` format now.
6372
6473
```bash
6574
fa screenshot -o screenshot.png
@@ -91,14 +100,19 @@ Launch io.appium.android.apis ...
91100
```
92101
93102
94-
95103
## Reference
96104
Articles
97105
98106
- <https://github.com/mzlogin/awesome-adb>
99107
- [Facebook One World Project](https://code.fb.com/android/managing-resources-for-large-scale-testing/)
100108
- [Facebook Device Lab](https://code.fb.com/android/the-mobile-device-lab-at-the-prineville-data-center/)
101109
- Article reverse ssh tunnling <https://www.howtoforge.com/reverse-ssh-tunneling>
110+
- [openstf/adbkit](https://github.com/openstf/adbkit)
111+
- [ADB Source Code](https://github.com/aosp-mirror/platform_system_core/blob/master/adb)
112+
113+
- [OVERVIEW.TXT](https://github.com/aosp-mirror/platform_system_core/blob/master/adb/OVERVIEW.TXT)
114+
- [SERVICES.TXT](https://github.com/aosp-mirror/platform_system_core/blob/master/adb/SERVICES.TXT)
115+
- [SYNC.TXT](https://github.com/aosp-mirror/platform_system_core/blob/master/adb/SYNC.TXT)
102116
103117
Libs might be useful
104118

adb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ type AdbClient struct {
138138

139139
func NewAdbClient() *AdbClient {
140140
return &AdbClient{
141-
Addr: "127.0.0.1:5037",
141+
Addr: defaultHost + ":" + strconv.Itoa(defaultPort),
142142
}
143143
}
144144

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ module github.com/codeskyblue/fa
22

33
require (
44
github.com/cavaliercoder/grab v2.0.0+incompatible
5+
github.com/kr/pty v1.1.3 // indirect
56
github.com/manifoldco/promptui v0.3.2
67
github.com/mattn/go-runewidth v0.0.3 // indirect
78
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
89
github.com/pkg/errors v0.8.0
910
github.com/shogo82148/androidbinary v0.0.0-20180627093851-01c4bfa8b3b5
10-
gopkg.in/cheggaaa/pb.v1 v1.0.25
11+
golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06 // indirect
12+
golang.org/x/tools v0.0.0-20181214171254-3c39ce7b6105 // indirect
13+
gopkg.in/cheggaaa/pb.v1 v1.0.27
1114
gopkg.in/urfave/cli.v1 v1.20.0
1215
)

main.go

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"log"
@@ -19,11 +20,14 @@ var (
1920
version = "develop"
2021
debug = false
2122
defaultSerial string
23+
defaultHost string
24+
defaultPort int
2225
)
2326

2427
type Device struct {
25-
Serial string
26-
Description string
28+
Serial string `json:"serial"`
29+
Status string `json:"status"`
30+
Description string `json:"-"`
2731
}
2832

2933
func (d *Device) String() string {
@@ -47,6 +51,24 @@ func shortDeviceInfo(s string) string {
4751
}
4852

4953
func listDevices() (ds []Device, err error) {
54+
output, err := exec.Command("adb", "devices").CombinedOutput()
55+
if err != nil {
56+
return
57+
}
58+
re := regexp.MustCompile(`(?m)^([^\s]+)\s+(device|offline|unauthorized)\s*$`)
59+
matches := re.FindAllStringSubmatch(string(output), -1)
60+
ds = make([]Device, 0, len(matches))
61+
for _, m := range matches {
62+
status := m[2]
63+
ds = append(ds, Device{
64+
Serial: m[1],
65+
Status: status,
66+
})
67+
}
68+
return
69+
}
70+
71+
func listDetailedDevices() (ds []Device, err error) {
5072
output, err := exec.Command("adb", "devices", "-l").CombinedOutput()
5173
if err != nil {
5274
return
@@ -89,7 +111,7 @@ func choose(devices []Device) Device {
89111
}
90112

91113
func chooseOne() (serial string, err error) {
92-
devices, err := listDevices()
114+
devices, err := listDetailedDevices()
93115
if err != nil {
94116
return
95117
}
@@ -156,20 +178,32 @@ func main() {
156178
EnvVar: "ANDROID_SERIAL",
157179
Destination: &defaultSerial,
158180
},
181+
cli.StringFlag{
182+
Name: "host, H",
183+
Usage: "name of adb server host",
184+
Value: "localhost",
185+
Destination: &defaultHost,
186+
},
187+
cli.IntFlag{
188+
Name: "port, P",
189+
Usage: "port of adb server",
190+
Value: 5037,
191+
Destination: &defaultPort,
192+
},
159193
}
160194
app.Commands = []cli.Command{
161195
{
162196
Name: "version",
163197
Usage: "show version",
164198
Action: func(ctx *cli.Context) error {
165-
fmt.Printf("fa version %s\n", version)
166-
adbVersion, err := DefaultAdbClient.Version()
199+
fmt.Printf("fa version %s\n", version)
200+
adbVersion, err := NewAdbClient().Version()
167201
if err != nil {
168202
fmt.Printf("adb version err: %v\n", err)
169203
return err
170204
}
171-
fmt.Println("adb version", adbVersion)
172205
fmt.Println("adb path", adbPath())
206+
fmt.Println("adb server version", adbVersion)
173207
return nil
174208
// output, err := exec.Command(adbPath(), "version").Output()
175209
// for _, line := range strings.Split(string(output), "\n") {
@@ -178,6 +212,31 @@ func main() {
178212
// return err
179213
},
180214
},
215+
{
216+
Name: "devices",
217+
Usage: "show connected devices",
218+
Flags: []cli.Flag{
219+
cli.BoolFlag{
220+
Name: "json",
221+
Usage: "output json format",
222+
},
223+
},
224+
Action: func(ctx *cli.Context) error {
225+
ds, err := listDevices()
226+
if err != nil {
227+
return err
228+
}
229+
if ctx.Bool("json") {
230+
data, _ := json.MarshalIndent(ds, "", " ")
231+
fmt.Println(string(data))
232+
} else {
233+
for _, d := range ds {
234+
fmt.Printf("%s\t%s\n", d.Serial, d.Status)
235+
}
236+
}
237+
return nil
238+
},
239+
},
181240
{
182241
Name: "adb",
183242
Usage: "exec adb with device select",

screenshot.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package main
22

33
import (
4+
"fmt"
45
"log"
56
"os"
7+
"runtime"
8+
"time"
69

710
"github.com/pkg/browser"
811
// "github.com/urfave/cli"
@@ -36,14 +39,17 @@ func takeScreenshot(serial, output string) error {
3639
return c.Run()
3740
}
3841
screencap := func() error {
39-
tmpPath := "/sdcard/fa-screenshot.png"
42+
tmpPath := fmt.Sprintf("/sdcard/fa-screenshot-%d.png", time.Now().UnixNano())
4043
c := adbCommand(serial, "shell", "screencap", "-p", tmpPath)
4144
if err := c.Run(); err != nil {
4245
return err
4346
}
4447
defer adbCommand(serial, "shell", "rm", tmpPath).Run()
4548
return adbCommand(serial, "pull", tmpPath, output).Run()
4649
}
50+
if runtime.GOOS == "windows" {
51+
return screencap()
52+
}
4753
return anyFuncs(execOut, screencap)
4854
}
4955

0 commit comments

Comments
 (0)