| OLD | NEW |
| 1 // Utility that contains utility methods for interacting with adb. | 1 // Utility that contains utility methods for interacting with adb. |
| 2 package adb | 2 package adb |
| 3 | 3 |
| 4 import ( | 4 import ( |
| 5 "fmt" | 5 "fmt" |
| 6 "os/exec" | 6 "os/exec" |
| 7 "time" | 7 "time" |
| 8 | 8 |
| 9 "github.com/skia-dev/glog" | |
| 10 | |
| 11 "go.skia.org/infra/ct/go/util" | 9 "go.skia.org/infra/ct/go/util" |
| 10 skexec "go.skia.org/infra/go/exec" |
| 12 skutil "go.skia.org/infra/go/util" | 11 skutil "go.skia.org/infra/go/util" |
| 13 ) | 12 ) |
| 14 | 13 |
| 15 // VerifyLocalDevice does not throw an error if an Android device is connected a
nd | 14 // VerifyLocalDevice does not throw an error if an Android device is connected a
nd |
| 16 // online. An error is returned if either "adb" is not installed or if the Andro
id | 15 // online. An error is returned if either "adb" is not installed or if the Andro
id |
| 17 // device is offline or missing. | 16 // device is offline or missing. |
| 18 func VerifyLocalDevice() error { | 17 func VerifyLocalDevice() error { |
| 19 // Run "adb version". | 18 // Run "adb version". |
| 20 // Command should return without an error. | 19 // Command should return without an error. |
| 21 if err := util.ExecuteCmd(util.BINARY_ADB, []string{"version"}, []string
{}, 5*time.Minute, nil, nil); err != nil { | 20 if err := util.ExecuteCmd(util.BINARY_ADB, []string{"version"}, []string
{}, 5*time.Minute, nil, nil); err != nil { |
| 22 return fmt.Errorf("adb not installed or not found: %s", err) | 21 return fmt.Errorf("adb not installed or not found: %s", err) |
| 23 } | 22 } |
| 24 | 23 |
| 25 // Run "adb devices | grep offline". | 24 // Run "adb devices | grep offline". |
| 26 // Command should return with an error. | 25 // Command should return with an error. |
| 27 devicesCmd := exec.Command(util.BINARY_ADB, "devices") | 26 devicesCmd := exec.Command(util.BINARY_ADB, "devices") |
| 28 offlineCmd := exec.Command("grep", "offline") | 27 offlineCmd := exec.Command("grep", "offline") |
| 29 offlineCmd.Stdin, _ = devicesCmd.StdoutPipe() | 28 offlineCmd.Stdin, _ = devicesCmd.StdoutPipe() |
| 30 » offlineCmd.Stdout = util.WriteLog{LogFunc: glog.Infof, OutputFile: nil} | 29 » offlineCmd.Stdout = skexec.WriteInfoLog |
| 31 skutil.LogErr(offlineCmd.Start()) | 30 skutil.LogErr(offlineCmd.Start()) |
| 32 skutil.LogErr(devicesCmd.Run()) | 31 skutil.LogErr(devicesCmd.Run()) |
| 33 if err := offlineCmd.Wait(); err == nil { | 32 if err := offlineCmd.Wait(); err == nil { |
| 34 // A nil error here means that an offline device was found. | 33 // A nil error here means that an offline device was found. |
| 35 return fmt.Errorf("Android device is offline: %s", err) | 34 return fmt.Errorf("Android device is offline: %s", err) |
| 36 } | 35 } |
| 37 | 36 |
| 38 // Running "adb devices | grep device$ | 37 // Running "adb devices | grep device$ |
| 39 // Command should return without an error. | 38 // Command should return without an error. |
| 40 devicesCmd = exec.Command(util.BINARY_ADB, "devices") | 39 devicesCmd = exec.Command(util.BINARY_ADB, "devices") |
| 41 missingCmd := exec.Command("grep", "device$") | 40 missingCmd := exec.Command("grep", "device$") |
| 42 missingCmd.Stdin, _ = devicesCmd.StdoutPipe() | 41 missingCmd.Stdin, _ = devicesCmd.StdoutPipe() |
| 43 » missingCmd.Stdout = util.WriteLog{LogFunc: glog.Infof, OutputFile: nil} | 42 » missingCmd.Stdout = skexec.WriteInfoLog |
| 44 skutil.LogErr(missingCmd.Start()) | 43 skutil.LogErr(missingCmd.Start()) |
| 45 skutil.LogErr(devicesCmd.Run()) | 44 skutil.LogErr(devicesCmd.Run()) |
| 46 if err := missingCmd.Wait(); err != nil { | 45 if err := missingCmd.Wait(); err != nil { |
| 47 // An error here means that the device is missing. | 46 // An error here means that the device is missing. |
| 48 return fmt.Errorf("Android device is missing: %s", err) | 47 return fmt.Errorf("Android device is missing: %s", err) |
| 49 } | 48 } |
| 50 | 49 |
| 51 return nil | 50 return nil |
| 52 } | 51 } |
| OLD | NEW |