adb

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2019 License: Apache-2.0 Imports: 9 Imported by: 2

README

go-adb

GoDoc Go Report Card

Tiny wrapper for ADB server written in go.

Usage

For examples look at examples_test.go file in this repository or in GoDoc.

License

Apache License 2.0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ADBPath string

ADBPath contains the absolute path to the adb executable, empty string if the adb executable was not found. In last case it should be set manually.

View Source
var ErrADBNotFound = errors.New("ADB command not found")

ErrADBNotFound is returned when the ADB executable is not found.

View Source
var ErrNoDevicesFound = errors.New("no devices found")

ErrNoDevicesFound is returned when the ADB executable returns an empty device list.

View Source
var ErrParseDeviceList = errors.New("could not parse device list")

ErrParseDeviceList is returned when the ADB executable returns an invalid device list.

View Source
var ErrParseDeviceState = errors.New("could not parse device state")

ErrParseDeviceState is returned when the ADB executable returns an invalid device state.

View Source
var RegexpSerial = regexp.MustCompile(`^[0-9a-f]{8}$`)

RegexpSerial contains regular expression to match serial numbers.

Functions

func Exec

func Exec(t time.Duration, arg ...string) (string, error)

Exec builds up an ADB command and executes it.

Example
// simple error checking
s, err := adb.Exec(5, "version")
if err != nil {
	fmt.Printf("error occurred: %v\n", err)
} else {
	fmt.Println("no error occurred")
	_ = s
	// fmt.Println(s)
}

// extended error checking
s, err = adb.Exec(5, "foobar")
if err != nil {
	// check for exit code != 0
	if exiterr, ok := err.(*exec.ExitError); ok {
		// try to get exit code
		if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
			fmt.Printf("exit code not 0: %d\n", status.ExitStatus())
		}
	} else {
		fmt.Printf("error occurred: %v\n", err)
	}
} else {
	fmt.Printf("exit code is 0\n")
	_ = s
	//fmt.Println(s)
}
Output:

no error occurred
exit code not 0: 1

func RestartServer

func RestartServer() error

RestartServer restarts the server.

func StartServer

func StartServer() error

StartServer ensures that there is a server running.

func StopServer

func StopServer() error

StopServer stops the server if it is running.

Types

type Device

type Device struct {
	Serial string
	State  DeviceState
}

Device contains details about an attached device.

func GetDevices

func GetDevices() ([]*Device, error)

GetDevices is a helper function that returns a list of attached devices.

Example
package main

import ()

func main() {
	// we can not simulate ADB device
	//d, err := adb.GetDevices()
	//fmt.Println(d[0].Serial)
}

func ParseDevices

func ParseDevices(s string) ([]*Device, error)

ParseDevices parses the output from ADB devices command to Device struct.

Example
o := `List of devices attached
	36b8b42e        offline`

d, err := adb.ParseDevices(o)
if err != nil {
	fmt.Println("error")
}

fmt.Println(d[0].Serial)
Output:

36b8b42e

type DeviceState

type DeviceState int

DeviceState is the state of an device.

const (
	//DeviceOffline referes to the state offline.
	DeviceOffline DeviceState = iota
	//DeviceOnline referes to the state online.
	DeviceOnline
	//DeviceUnauthorized referes to the state unauthorized.
	DeviceUnauthorized
	//DeviceUnknown refers to the state unknown.
	DeviceUnknown
)

func GetDeviceState

func GetDeviceState(serial string) (DeviceState, error)

GetDeviceState returns state of specific device.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL