2015-12-21 06:42:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-12-28 07:39:28 +00:00
|
|
|
"io"
|
2015-12-21 06:42:16 +00:00
|
|
|
"os"
|
2015-12-28 07:39:28 +00:00
|
|
|
"path/filepath"
|
2015-12-29 21:25:17 +00:00
|
|
|
"syscall"
|
2015-12-28 07:39:28 +00:00
|
|
|
"time"
|
2015-12-21 06:42:16 +00:00
|
|
|
|
2015-12-28 07:39:28 +00:00
|
|
|
"github.com/cheggaaa/pb"
|
2015-12-21 06:42:16 +00:00
|
|
|
"github.com/zach-klippenstein/goadb"
|
2015-12-28 07:39:28 +00:00
|
|
|
"github.com/zach-klippenstein/goadb/util"
|
2015-12-21 06:42:16 +00:00
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
|
|
|
)
|
|
|
|
|
2015-12-29 21:25:17 +00:00
|
|
|
const StdIoFilename = "-"
|
|
|
|
|
2015-12-21 06:42:16 +00:00
|
|
|
var (
|
2016-05-08 01:00:26 +00:00
|
|
|
serial = kingpin.Flag("serial",
|
|
|
|
"Connect to device by serial number.").
|
|
|
|
Short('s').
|
|
|
|
String()
|
2015-12-28 07:39:28 +00:00
|
|
|
|
2016-05-08 01:00:26 +00:00
|
|
|
shellCommand = kingpin.Command("shell",
|
|
|
|
"Run a shell command on the device.")
|
|
|
|
shellCommandArg = shellCommand.Arg("command",
|
|
|
|
"Command to run on device.").
|
|
|
|
Strings()
|
2015-12-28 07:39:28 +00:00
|
|
|
|
2016-05-08 01:00:26 +00:00
|
|
|
devicesCommand = kingpin.Command("devices",
|
|
|
|
"List devices.")
|
|
|
|
devicesLongFlag = devicesCommand.Flag("long",
|
|
|
|
"Include extra detail about devices.").
|
|
|
|
Short('l').
|
|
|
|
Bool()
|
2015-12-28 07:39:28 +00:00
|
|
|
|
2016-05-08 01:00:26 +00:00
|
|
|
pullCommand = kingpin.Command("pull",
|
|
|
|
"Pull a file from the device.")
|
|
|
|
pullProgressFlag = pullCommand.Flag("progress",
|
|
|
|
"Show progress.").
|
|
|
|
Short('p').
|
|
|
|
Bool()
|
|
|
|
pullRemoteArg = pullCommand.Arg("remote",
|
|
|
|
"Path of source file on device.").
|
|
|
|
Required().
|
|
|
|
String()
|
|
|
|
pullLocalArg = pullCommand.Arg("local",
|
|
|
|
"Path of destination file. If -, will write to stdout.").
|
|
|
|
String()
|
2015-12-28 07:39:28 +00:00
|
|
|
|
2016-05-08 01:00:26 +00:00
|
|
|
pushCommand = kingpin.Command("push",
|
|
|
|
"Push a file to the device.")
|
|
|
|
pushProgressFlag = pushCommand.Flag("progress",
|
|
|
|
"Show progress.").
|
|
|
|
Short('p').
|
|
|
|
Bool()
|
|
|
|
pushLocalArg = pushCommand.Arg("local",
|
|
|
|
"Path of source file. If -, will read from stdin.").
|
|
|
|
Required().
|
|
|
|
String()
|
|
|
|
pushRemoteArg = pushCommand.Arg("remote",
|
|
|
|
"Path of destination file on device.").
|
|
|
|
Required().
|
|
|
|
String()
|
2015-12-21 06:42:16 +00:00
|
|
|
)
|
|
|
|
|
2016-01-15 15:54:27 +00:00
|
|
|
var server adb.Server
|
2016-01-10 21:33:22 +00:00
|
|
|
|
2015-12-21 06:42:16 +00:00
|
|
|
func main() {
|
|
|
|
var exitCode int
|
|
|
|
|
2016-01-10 21:33:22 +00:00
|
|
|
var err error
|
2016-01-15 15:54:27 +00:00
|
|
|
server, err = adb.NewServer(adb.ServerConfig{})
|
2016-01-10 21:33:22 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "error:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2015-12-21 06:42:16 +00:00
|
|
|
switch kingpin.Parse() {
|
|
|
|
case "devices":
|
|
|
|
exitCode = listDevices(*devicesLongFlag)
|
|
|
|
case "shell":
|
|
|
|
exitCode = runShellCommand(*shellCommandArg, parseDevice())
|
2015-12-28 07:39:28 +00:00
|
|
|
case "pull":
|
|
|
|
exitCode = pull(*pullProgressFlag, *pullRemoteArg, *pullLocalArg, parseDevice())
|
|
|
|
case "push":
|
|
|
|
exitCode = push(*pushProgressFlag, *pushLocalArg, *pushRemoteArg, parseDevice())
|
2015-12-21 06:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:54:27 +00:00
|
|
|
func parseDevice() adb.DeviceDescriptor {
|
2015-12-21 06:42:16 +00:00
|
|
|
if *serial != "" {
|
2016-01-15 15:54:27 +00:00
|
|
|
return adb.DeviceWithSerial(*serial)
|
2015-12-21 06:42:16 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 15:54:27 +00:00
|
|
|
return adb.AnyDevice()
|
2015-12-21 06:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func listDevices(long bool) int {
|
2016-01-15 15:54:27 +00:00
|
|
|
client := adb.NewHostClient(server)
|
2015-12-21 06:42:16 +00:00
|
|
|
devices, err := client.ListDevices()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, device := range devices {
|
|
|
|
if long {
|
2015-12-28 01:37:17 +00:00
|
|
|
if device.Usb == "" {
|
|
|
|
fmt.Printf("%s\tproduct:%s model:%s device:%s\n",
|
|
|
|
device.Serial, device.Product, device.Model, device.DeviceInfo)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%s\tusb:%s product:%s model:%s device:%s\n",
|
|
|
|
device.Serial, device.Usb, device.Product, device.Model, device.DeviceInfo)
|
|
|
|
}
|
2015-12-21 06:42:16 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println(device.Serial)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:54:27 +00:00
|
|
|
func runShellCommand(commandAndArgs []string, device adb.DeviceDescriptor) int {
|
2015-12-21 06:42:16 +00:00
|
|
|
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:]
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:54:27 +00:00
|
|
|
client := adb.NewDeviceClient(server, device)
|
2015-12-21 06:42:16 +00:00
|
|
|
output, err := client.RunCommand(command, args...)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "error:", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Print(output)
|
|
|
|
return 0
|
|
|
|
}
|
2015-12-28 07:39:28 +00:00
|
|
|
|
2016-01-15 15:54:27 +00:00
|
|
|
func pull(showProgress bool, remotePath, localPath string, device adb.DeviceDescriptor) int {
|
2015-12-28 07:39:28 +00:00
|
|
|
if remotePath == "" {
|
|
|
|
fmt.Fprintln(os.Stderr, "error: must specify remote file")
|
|
|
|
kingpin.Usage()
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if localPath == "" {
|
|
|
|
localPath = filepath.Base(remotePath)
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:54:27 +00:00
|
|
|
client := adb.NewDeviceClient(server, device)
|
2015-12-28 07:39:28 +00:00
|
|
|
|
|
|
|
info, err := client.Stat(remotePath)
|
|
|
|
if util.HasErrCode(err, util.FileNoExistError) {
|
|
|
|
fmt.Fprintln(os.Stderr, "remote file does not exist:", remotePath)
|
|
|
|
return 1
|
|
|
|
} else if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error reading remote file %s: %s\n", remotePath, err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteFile, err := client.OpenRead(remotePath)
|
|
|
|
if err != nil {
|
2015-12-28 23:28:53 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "error opening remote file %s: %s\n", remotePath, util.ErrorWithCauseChain(err))
|
2015-12-28 07:39:28 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
defer remoteFile.Close()
|
|
|
|
|
2015-12-29 21:25:17 +00:00
|
|
|
var localFile io.WriteCloser
|
|
|
|
if localPath == StdIoFilename {
|
|
|
|
localFile = os.Stdout
|
|
|
|
} else {
|
|
|
|
localFile, err = os.Create(localPath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error opening local file %s: %s\n", localPath, err)
|
|
|
|
return 1
|
|
|
|
}
|
2015-12-28 07:39:28 +00:00
|
|
|
}
|
|
|
|
defer localFile.Close()
|
|
|
|
|
2015-12-28 09:08:51 +00:00
|
|
|
if err := copyWithProgressAndStats(localFile, remoteFile, int(info.Size), showProgress); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "error pulling file:", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:54:27 +00:00
|
|
|
func push(showProgress bool, localPath, remotePath string, device adb.DeviceDescriptor) int {
|
2015-12-28 09:08:51 +00:00
|
|
|
if remotePath == "" {
|
|
|
|
fmt.Fprintln(os.Stderr, "error: must specify remote file")
|
|
|
|
kingpin.Usage()
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-12-29 21:25:17 +00:00
|
|
|
var (
|
|
|
|
localFile io.ReadCloser
|
|
|
|
size int
|
|
|
|
perms os.FileMode
|
|
|
|
mtime time.Time
|
|
|
|
)
|
|
|
|
if localPath == "" || localPath == StdIoFilename {
|
|
|
|
localFile = os.Stdin
|
|
|
|
// 0 size will hide the progress bar.
|
|
|
|
perms = os.FileMode(0660)
|
2016-01-15 15:54:27 +00:00
|
|
|
mtime = adb.MtimeOfClose
|
2015-12-29 21:25:17 +00:00
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
localFile, err = os.Open(localPath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error opening local file %s: %s\n", localPath, err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
info, err := os.Stat(localPath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error reading local file %s: %s\n", localPath, err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
size = int(info.Size())
|
|
|
|
perms = info.Mode().Perm()
|
|
|
|
mtime = info.ModTime()
|
2015-12-28 09:08:51 +00:00
|
|
|
}
|
2015-12-29 21:25:17 +00:00
|
|
|
defer localFile.Close()
|
2015-12-28 09:08:51 +00:00
|
|
|
|
2016-01-15 15:54:27 +00:00
|
|
|
client := adb.NewDeviceClient(server, device)
|
2015-12-29 21:25:17 +00:00
|
|
|
writer, err := client.OpenWrite(remotePath, perms, mtime)
|
2015-12-28 09:08:51 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error opening remote file %s: %s\n", remotePath, err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
defer writer.Close()
|
|
|
|
|
2015-12-29 21:25:17 +00:00
|
|
|
if err := copyWithProgressAndStats(writer, localFile, size, showProgress); err != nil {
|
2015-12-28 09:08:51 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "error pushing file:", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-12-29 21:25:17 +00:00
|
|
|
// copyWithProgressAndStats copies src to dst.
|
|
|
|
// If showProgress is true and size is positive, a progress bar is shown.
|
|
|
|
// After copying, final stats about the transfer speed and size are shown.
|
|
|
|
// Progress and stats are printed to stderr.
|
2015-12-28 09:08:51 +00:00
|
|
|
func copyWithProgressAndStats(dst io.Writer, src io.Reader, size int, showProgress bool) error {
|
|
|
|
var progress *pb.ProgressBar
|
2015-12-29 21:25:17 +00:00
|
|
|
if showProgress && size > 0 {
|
2015-12-28 09:08:51 +00:00
|
|
|
progress = pb.New(size)
|
2015-12-29 21:25:17 +00:00
|
|
|
// Write to stderr in case dst is stdout.
|
|
|
|
progress.Output = os.Stderr
|
2015-12-28 09:08:51 +00:00
|
|
|
progress.ShowSpeed = true
|
|
|
|
progress.ShowPercent = true
|
|
|
|
progress.ShowTimeLeft = true
|
2015-12-29 21:25:17 +00:00
|
|
|
progress.SetUnits(pb.U_BYTES)
|
2015-12-28 09:08:51 +00:00
|
|
|
progress.Start()
|
|
|
|
dst = io.MultiWriter(dst, progress)
|
2015-12-28 07:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
startTime := time.Now()
|
2015-12-28 09:08:51 +00:00
|
|
|
copied, err := io.Copy(dst, src)
|
2015-12-28 07:39:28 +00:00
|
|
|
|
2015-12-28 09:08:51 +00:00
|
|
|
if progress != nil {
|
2015-12-29 21:25:17 +00:00
|
|
|
progress.Finish()
|
2015-12-28 09:08:51 +00:00
|
|
|
}
|
2015-12-28 07:39:28 +00:00
|
|
|
|
2015-12-29 21:25:17 +00:00
|
|
|
if pathErr, ok := err.(*os.PathError); ok {
|
|
|
|
if errno, ok := pathErr.Err.(syscall.Errno); ok && errno == syscall.EPIPE {
|
|
|
|
// Pipe closed. Handle this like an EOF.
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
}
|
2015-12-28 07:39:28 +00:00
|
|
|
if err != nil {
|
2015-12-28 09:08:51 +00:00
|
|
|
return err
|
2015-12-28 07:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
duration := time.Now().Sub(startTime)
|
|
|
|
rate := int64(float64(copied) / duration.Seconds())
|
2015-12-29 21:25:17 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "%d B/s (%d bytes in %s)\n", rate, copied, duration)
|
2015-12-28 07:39:28 +00:00
|
|
|
|
2015-12-28 09:08:51 +00:00
|
|
|
return nil
|
2015-12-28 07:39:28 +00:00
|
|
|
}
|