goadb/cmd/raw-adb/raw-adb.go
Zach Klippenstein 9f7d11a3bc Refactored ClientConfig into a Server interface.
* StartServer is now a method on Server.
* What used to be Dialer.Dial is now Server.Dial.
* Server.Dial handles trying to start the server if the initial connection fails.
* Dialer now dials a network address.
* All types that took a Dialer now take a Server.
* Server now has tests!
2016-01-10 14:01:58 -08:00

88 lines
1.3 KiB
Go

// A simple tool for sending raw messages to an adb server.
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"github.com/zach-klippenstein/goadb"
"github.com/zach-klippenstein/goadb/wire"
)
var port = flag.Int("p", goadb.AdbPort, "port the adb server is listening on")
func main() {
flag.Parse()
fmt.Println("using port", *port)
printServerVersion()
for {
line := readLine()
err := doCommand(line)
if err != nil {
fmt.Println("error:", err)
}
}
}
func printServerVersion() {
err := doCommand("host:version")
if err != nil {
log.Fatal(err)
}
}
func readLine() string {
fmt.Print("> ")
line, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil && err != io.EOF {
log.Fatal(err)
}
return strings.TrimSpace(line)
}
func doCommand(cmd string) error {
server, err := goadb.NewServer(goadb.ServerConfig{
Port: *port,
})
if err != nil {
log.Fatal(err)
}
conn, err := server.Dial()
if err != nil {
log.Fatal(err)
}
defer conn.Close()
if err := wire.SendMessageString(conn, cmd); err != nil {
return err
}
status, err := conn.ReadStatus("")
if err != nil {
return err
}
var msg string
for err == nil {
msg, err = wire.ReadMessageString(conn)
if err == nil {
fmt.Printf("%s> %s\n", status, msg)
}
}
if err != io.EOF {
return err
}
return nil
}