From f6d7b0b6653f48006167cc33afe3134011657568 Mon Sep 17 00:00:00 2001 From: Zach Klippenstein Date: Sun, 20 Dec 2015 22:42:16 -0800 Subject: [PATCH 1/2] Started basic clone of the adb command. --- cmd/adb/main.go | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 cmd/adb/main.go diff --git a/cmd/adb/main.go b/cmd/adb/main.go new file mode 100644 index 0000000..27746b0 --- /dev/null +++ b/cmd/adb/main.go @@ -0,0 +1,81 @@ +package main + +import ( + "fmt" + "os" + + "github.com/zach-klippenstein/goadb" + "gopkg.in/alecthomas/kingpin.v2" +) + +var ( + serial = kingpin.Flag("serial", "Connect to device by serial number.").Short('s').String() + shellCommand = kingpin.Command("shell", "Run a shell command on the device.") + shellCommandArg = shellCommand.Arg("command", "Command to run on device.").Strings() + devicesCommand = kingpin.Command("devices", "List devices.") + devicesLongFlag = devicesCommand.Flag("long", "Include extra detail about devices.").Short('l').Bool() +) + +func main() { + var exitCode int + + switch kingpin.Parse() { + case "devices": + exitCode = listDevices(*devicesLongFlag) + case "shell": + exitCode = runShellCommand(*shellCommandArg, parseDevice()) + } + + os.Exit(exitCode) +} + +func parseDevice() goadb.DeviceDescriptor { + if *serial != "" { + return goadb.DeviceWithSerial(*serial) + } + + return goadb.AnyDevice() +} + +func listDevices(long bool) int { + client := goadb.NewHostClient(goadb.ClientConfig{}) + devices, err := client.ListDevices() + if err != nil { + fmt.Fprintln(os.Stderr, "error:", err) + } + + for _, device := range devices { + if long { + fmt.Printf("%s\t\t%s\t%s\n", device.Serial, device.Product, device.Model) + } else { + fmt.Println(device.Serial) + } + } + + return 0 +} + +func runShellCommand(commandAndArgs []string, device goadb.DeviceDescriptor) int { + if len(commandAndArgs) == 0 { + fmt.Fprintln(os.Stderr, "error: no command") + kingpin.Usage() + return 1 + } + + command := commandAndArgs[0] + var args []string + + if len(commandAndArgs) > 1 { + args = commandAndArgs[1:] + } + + client := goadb.NewDeviceClient(goadb.ClientConfig{}, device) + output, err := client.RunCommand(command, args...) + if err != nil { + fmt.Fprintln(os.Stderr, "error:", err) + return 1 + } + + fmt.Print(output) + return 0 +} From b65c913aaf36dbd836d3f5b7e34cd88e46f17196 Mon Sep 17 00:00:00 2001 From: Zach Klippenstein Date: Sun, 20 Dec 2015 23:01:07 -0800 Subject: [PATCH 2/2] Updated go version for travis build. --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3f41d36..8941c0a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ language: go go: - - 1.4 + - 1.5.1 + - tip