forked from zach-klippenstein/goadb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.go
161 lines (139 loc) · 3.26 KB
/
demo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// An app demonstrating most of the library's features.
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"time"
adb "github.com/yosemite-open/go-adb"
"github.com/yosemite-open/go-adb/internal/errors"
)
var (
port = flag.Int("p", adb.AdbPort, "")
client *adb.Adb
)
func main() {
flag.Parse()
var err error
client, err = adb.NewWithConfig(adb.ServerConfig{
Port: *port,
})
if err != nil {
log.Fatal(err)
}
fmt.Println("Starting server…")
client.StartServer()
serverVersion, err := client.ServerVersion()
if err != nil {
log.Fatal(err)
}
fmt.Println("Server version:", serverVersion)
devices, err := client.ListDevices()
if err != nil {
log.Fatal(err)
}
fmt.Println("Devices:")
for _, device := range devices {
fmt.Printf("\t%+v\n", *device)
}
PrintDeviceInfoAndError(adb.AnyDevice())
PrintDeviceInfoAndError(adb.AnyLocalDevice())
PrintDeviceInfoAndError(adb.AnyUsbDevice())
serials, err := client.ListDeviceSerials()
if err != nil {
log.Fatal(err)
}
for _, serial := range serials {
PrintDeviceInfoAndError(adb.DeviceWithSerial(serial))
}
fmt.Println()
fmt.Println("Watching for device state changes.")
watcher := client.NewDeviceWatcher()
for event := range watcher.C() {
fmt.Printf("\t[%s]%+v\n", time.Now(), event)
}
if watcher.Err() != nil {
printErr(watcher.Err())
}
//fmt.Println("Killing server…")
//client.KillServer()
}
func printErr(err error) {
switch err := err.(type) {
case *errors.Err:
fmt.Println(err.Error())
if err.Cause != nil {
fmt.Print("caused by ")
printErr(err.Cause)
}
default:
fmt.Println("error:", err)
}
}
func PrintDeviceInfoAndError(descriptor adb.DeviceDescriptor) {
device := client.Device(descriptor)
if err := PrintDeviceInfo(device); err != nil {
log.Println(err)
}
}
func PrintDeviceInfo(device *adb.Device) error {
serialNo, err := device.Serial()
if err != nil {
return err
}
devPath, err := device.DevicePath()
if err != nil {
return err
}
state, err := device.State()
if err != nil {
return err
}
fmt.Println(device)
fmt.Printf("\tserial no: %s\n", serialNo)
fmt.Printf("\tdevPath: %s\n", devPath)
fmt.Printf("\tstate: %s\n", state)
cmdOutput, err := device.RunCommand("pwd")
if err != nil {
fmt.Println("\terror running command:", err)
}
fmt.Printf("\tcmd output: %s\n", cmdOutput)
stat, err := device.Stat("/sdcard")
if err != nil {
fmt.Println("\terror stating /sdcard:", err)
}
fmt.Printf("\tstat \"/sdcard\": %+v\n", stat)
fmt.Println("\tfiles in \"/\":")
entries, err := device.ListDirEntries("/")
if err != nil {
fmt.Println("\terror listing files:", err)
} else {
for entries.Next() {
fmt.Printf("\t%+v\n", *entries.Entry())
}
if entries.Err() != nil {
fmt.Println("\terror listing files:", err)
}
}
fmt.Println("\tnon-existent file:")
stat, err = device.Stat("/supercalifragilisticexpialidocious")
if err != nil {
fmt.Println("\terror:", err)
} else {
fmt.Printf("\tstat: %+v\n", stat)
}
fmt.Print("\tload avg: ")
loadavgReader, err := device.OpenRead("/proc/loadavg")
if err != nil {
fmt.Println("\terror opening file:", err)
} else {
loadAvg, err := ioutil.ReadAll(loadavgReader)
if err != nil {
fmt.Println("\terror reading file:", err)
} else {
fmt.Println(string(loadAvg))
}
}
return nil
}