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,23 @@ 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+ for _ , m := range matches {
61+ status := m [2 ]
62+ ds = append (ds , Device {
63+ Serial : m [1 ],
64+ Status : status ,
65+ })
66+ }
67+ return
68+ }
69+
70+ func listDetailedDevices () (ds []Device , err error ) {
5071 output , err := exec .Command ("adb" , "devices" , "-l" ).CombinedOutput ()
5172 if err != nil {
5273 return
@@ -89,7 +110,7 @@ func choose(devices []Device) Device {
89110}
90111
91112func chooseOne () (serial string , err error ) {
92- devices , err := listDevices ()
113+ devices , err := listDetailedDevices ()
93114 if err != nil {
94115 return
95116 }
@@ -156,20 +177,32 @@ func main() {
156177 EnvVar : "ANDROID_SERIAL" ,
157178 Destination : & defaultSerial ,
158179 },
180+ cli.StringFlag {
181+ Name : "host, H" ,
182+ Usage : "name of adb server host" ,
183+ Value : "localhost" ,
184+ Destination : & defaultHost ,
185+ },
186+ cli.IntFlag {
187+ Name : "port, P" ,
188+ Usage : "port of adb server" ,
189+ Value : 5037 ,
190+ Destination : & defaultPort ,
191+ },
159192 }
160193 app .Commands = []cli.Command {
161194 {
162195 Name : "version" ,
163196 Usage : "show version" ,
164197 Action : func (ctx * cli.Context ) error {
165- fmt .Printf ("fa version %s\n " , version )
166- adbVersion , err := DefaultAdbClient .Version ()
198+ fmt .Printf ("fa version %s\n " , version )
199+ adbVersion , err := NewAdbClient () .Version ()
167200 if err != nil {
168201 fmt .Printf ("adb version err: %v\n " , err )
169202 return err
170203 }
171- fmt .Println ("adb version" , adbVersion )
172204 fmt .Println ("adb path" , adbPath ())
205+ fmt .Println ("adb server version" , adbVersion )
173206 return nil
174207 // output, err := exec.Command(adbPath(), "version").Output()
175208 // for _, line := range strings.Split(string(output), "\n") {
@@ -178,6 +211,31 @@ func main() {
178211 // return err
179212 },
180213 },
214+ {
215+ Name : "devices" ,
216+ Usage : "show connected devices" ,
217+ Flags : []cli.Flag {
218+ cli.BoolFlag {
219+ Name : "json" ,
220+ Usage : "output json format" ,
221+ },
222+ },
223+ Action : func (ctx * cli.Context ) error {
224+ ds , err := listDevices ()
225+ if err != nil {
226+ return err
227+ }
228+ if ctx .Bool ("json" ) {
229+ data , _ := json .MarshalIndent (ds , "" , " " )
230+ fmt .Println (string (data ))
231+ } else {
232+ for _ , d := range ds {
233+ fmt .Printf ("%s\t %s\n " , d .Serial , d .Status )
234+ }
235+ }
236+ return nil
237+ },
238+ },
181239 {
182240 Name : "adb" ,
183241 Usage : "exec adb with device select" ,
0 commit comments