constructor.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package adb
  2. import (
  3. "os/exec"
  4. "path/filepath"
  5. "notabug.org/Umnik/GoAndroidSDK/v2/components/misc"
  6. "notabug.org/Umnik/GoAndroidSDK/v2/components/sdk"
  7. )
  8. func adbFromPath(dirPath string, err error) (*Adb, error) {
  9. if err != nil {
  10. return nil, err
  11. }
  12. binPath := filepath.Join(dirPath, "adb")
  13. if err = misc.TestFile(binPath); err != nil {
  14. return nil, err
  15. }
  16. return &Adb{binPath: binPath}, nil
  17. }
  18. // NewAdbFind finds adb bin file (in $PATH) and creates Adb if bin file founds
  19. func NewAdbFind() (*Adb, error) {
  20. if p, err := findAdbBin(); err != nil {
  21. return nil, err
  22. } else {
  23. return &Adb{
  24. binPath: p,
  25. }, nil
  26. }
  27. }
  28. // NewAdbFromSdk finds adb bin file in sdk.SDK and created Adb if bin file founds
  29. func NewAdbFromSdk(sdk sdk.SDK) (*Adb, error) {
  30. return adbFromPath(sdk.PlatformTools())
  31. }
  32. // NewAdbFromPath creates Adb from path to bin file
  33. func NewAdbFromPath(binPath string) (*Adb, error) {
  34. if err := misc.TestFile(binPath); err != nil {
  35. return nil, err
  36. } else {
  37. return &Adb{
  38. binPath: binPath,
  39. }, nil
  40. }
  41. }
  42. func findAdbBin() (string, error) {
  43. return exec.LookPath("adb")
  44. }
  45. func NewDevice(serial, dType, usb, product, model, device, transport string) Device {
  46. return Device{
  47. serial: serial,
  48. deviceType: dType,
  49. usb: usb,
  50. product: product,
  51. model: model,
  52. device: device,
  53. transportId: transport,
  54. }
  55. }