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