11package main
22
33import (
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
2427type Device struct {
25- Serial string
26- Description string
28+ Serial string `json:"serial"`
29+ Status string `json:"status"`
30+ Description string `json:"-"`
2731}
2832
2933func (d * Device ) String () string {
@@ -47,6 +51,24 @@ func shortDeviceInfo(s string) string {
4751}
4852
4953func 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
91113func 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" ,
0 commit comments