Browse Source

Add argument to connect to remote device

master
Trevor Slocum 7 months ago
parent
commit
c98171418b
6 changed files with 126 additions and 45 deletions
  1. +7
    -3
      adb.go
  2. +67
    -0
      file.go
  3. +5
    -5
      go.mod
  4. +14
    -14
      go.sum
  5. +6
    -22
      gui.go
  6. +27
    -1
      main.go

+ 7
- 3
adb.go View File

@ -27,9 +27,9 @@ type adbConn struct {
sync.Mutex
}
// connect establishes a connection with an ADB server. When host and port
// are unspecified they are replaced with localhost and the default port.
func connect(host string, port int, pathToADB string) (bridge *adbConn, err error) {
// connectBridge establishes a connection with an ADB server. When host and
// port are unspecified they are replaced with localhost and the default port.
func connectBridge(host string, port int, pathToADB string) (bridge *adbConn, err error) {
adbBridge, err := adb.NewWithConfig(adb.ServerConfig{
PathToAdb: pathToADB,
Host: host,
@ -49,6 +49,10 @@ func connect(host string, port int, pathToADB string) (bridge *adbConn, err erro
return &adbConn{bridge: adbBridge}, nil
}
func (a *adbConn) connectDevice(host string, port int) (err error) {
return a.bridge.Connect(host, port)
}
func (a *adbConn) listDevices() (devices []*adb.DeviceInfo, err error) {
a.Lock()
defer a.Unlock()


+ 67
- 0
file.go View File

@ -0,0 +1,67 @@
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
)
func copyFile(src string, dst string) error {
dstFile, err := os.Create(dst)
if err != nil {
return fmt.Errorf("failed to paste file: failed to create dst file: %s", err)
}
defer dstFile.Close()
srcFile, err := os.OpenFile(src, os.O_RDONLY, 0655)
if err != nil {
return fmt.Errorf("failed to paste file: failed to read src file: %s", err)
}
defer srcFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return err
}
return nil
}
func copyRecursive(src string, dst string) error {
var err error
var fds []os.FileInfo
var srcinfo os.FileInfo
if srcinfo, err = os.Stat(src); err != nil {
return err
}
if srcinfo.Mode()&os.ModeDir != 0 {
if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
return err
}
if fds, err = ioutil.ReadDir(src); err != nil {
return err
}
} else {
fds = append(fds, srcinfo)
}
for _, fd := range fds {
srcfp := path.Join(src, fd.Name())
dstfp := path.Join(dst, fd.Name())
if fd.IsDir() {
if err = copyRecursive(srcfp, dstfp); err != nil {
return err
}
} else {
if err = copyFile(srcfp, dstfp); err != nil {
return err
}
}
}
return nil
}

+ 5
- 5
go.mod View File

@ -3,11 +3,11 @@ module gitlab.com/tslocum/adbfm
go 1.15
require (
github.com/gdamore/tcell/v2 v2.0.1-0.20201019142633-1057d5591ed1
github.com/gdamore/tcell/v2 v2.0.1-0.20201109052606-7d87d8188c8d
github.com/mattn/go-runewidth v0.0.9
github.com/stretchr/testify v1.6.1 // indirect
github.com/zach-klippenstein/goadb v0.0.0-20170530005145-029cc6bee481
gitlab.com/tslocum/cbind v0.1.3
gitlab.com/tslocum/cview v1.5.2-0.20201107170141-79a35fe4de6c
golang.org/x/sys v0.0.0-20201109165425-215b40eba54c // indirect
github.com/zach-klippenstein/goadb v0.0.0-20201208042340-620e0e950ed7
gitlab.com/tslocum/cbind v0.1.4
gitlab.com/tslocum/cview v1.5.2
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d // indirect
)

+ 14
- 14
go.sum View File

@ -3,8 +3,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.0.0-dev/go.mod h1:vSVL/GV5mCSlPC6thFP5kfOFdM9MGZcalipmpTxTgQA=
github.com/gdamore/tcell/v2 v2.0.1-0.20201019142633-1057d5591ed1 h1:gp9ujdOQmQf1gMvqOYYgxdMS5tRpRGE3HAgRH4Hgzd4=
github.com/gdamore/tcell/v2 v2.0.1-0.20201019142633-1057d5591ed1/go.mod h1:vSVL/GV5mCSlPC6thFP5kfOFdM9MGZcalipmpTxTgQA=
github.com/gdamore/tcell/v2 v2.0.1-0.20201109052606-7d87d8188c8d h1:C1FQEuzw5kUUveSXpZp3v0+qOR+VEnzHsQ7K6n39LsM=
github.com/gdamore/tcell/v2 v2.0.1-0.20201109052606-7d87d8188c8d/go.mod h1:vSVL/GV5mCSlPC6thFP5kfOFdM9MGZcalipmpTxTgQA=
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
@ -12,23 +12,23 @@ github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/Qd
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0 h1:+2KBaVoUmb9XzDsrx/Ct0W/EYOSFf/nWTauy++DprtY=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/zach-klippenstein/goadb v0.0.0-20170530005145-029cc6bee481 h1:yVrbGOZZHihWLaa3xpaTKoO5FbYg/vG5hUufn429HAo=
github.com/zach-klippenstein/goadb v0.0.0-20170530005145-029cc6bee481/go.mod h1:Drd+klC4FSDx0vKNEQDsSpWX5so04NA7l0vzHqkH8AQ=
gitlab.com/tslocum/cbind v0.1.3 h1:FT/fTQ4Yj3eo5021lB3IbkIt8eVtYGhrw/xur+cjvUU=
gitlab.com/tslocum/cbind v0.1.3/go.mod h1:RvwYE3auSjBNlCmWeGspzn+jdLUVQ8C2QGC+0nP9ChI=
gitlab.com/tslocum/cview v1.5.2-0.20201107170141-79a35fe4de6c h1:umKPjQ2bSmLWf0rpYWhjeZsl5IdhNGEq3041e+yq//U=
gitlab.com/tslocum/cview v1.5.2-0.20201107170141-79a35fe4de6c/go.mod h1:BRtUi0zXzVXufhqFm/1GD7GL+iznKh5m9pEGN19SnKA=
github.com/zach-klippenstein/goadb v0.0.0-20201208042340-620e0e950ed7 h1:xwmuUst0P21SJmJlIOPPq/geECy23t+DUxgnRSqt6Hg=
github.com/zach-klippenstein/goadb v0.0.0-20201208042340-620e0e950ed7/go.mod h1:Drd+klC4FSDx0vKNEQDsSpWX5so04NA7l0vzHqkH8AQ=
gitlab.com/tslocum/cbind v0.1.4 h1:cbZXPPcieXspk8cShoT6efz7HAT8yMNQcofYWNizis4=
gitlab.com/tslocum/cbind v0.1.4/go.mod h1:RvwYE3auSjBNlCmWeGspzn+jdLUVQ8C2QGC+0nP9ChI=
gitlab.com/tslocum/cview v1.5.2 h1:pt1d4L0LGghwpGRDXN5dPTp0+M5cZhoYooblLLNOA1A=
gitlab.com/tslocum/cview v1.5.2/go.mod h1:tnNYj71jg9ZvvF9Eu9/2f0Yji0Rwjy2JK36UoS/Hxvo=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201013132646-2da7054afaeb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201101102859-da207088b7d1 h1:a/mKvvZr9Jcc8oKfcmgzyp7OwF73JPWsQLvH1z2Kxck=
golang.org/x/sys v0.0.0-20201101102859-da207088b7d1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201109165425-215b40eba54c h1:+B+zPA6081G5cEb2triOIJpcvSW4AYzmIyWAqMn2JAc=
golang.org/x/sys v0.0.0-20201109165425-215b40eba54c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201202213521-69691e467435 h1:25AvDqqB9PrNqj1FLf2/70I4W0L19qqoaFq3gjNwbKk=
golang.org/x/sys v0.0.0-20201202213521-69691e467435/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d h1:MiWWjyhUzZ+jvhZvloX6ZrUsdEghn8a64Upd8EMHglE=
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4 h1:0YWbFKbhXG/wIiuHDSKpS0Iy7FSA+u45VtBMfQcFTTc=


+ 6
- 22
gui.go View File

@ -2,7 +2,6 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
@ -181,6 +180,9 @@ func initTUI() {
}
if len(devices) == 0 {
if connectAddress != "" {
log.Fatalf("failed to connect to device %s", connectAddress)
}
log.Fatal("failed to start adbfm: please connect to a device via ADB before launching")
}
@ -282,33 +284,15 @@ func initTUI() {
file.Write(data)
file.Close()
} else {
// TODO recursively upload dir, check exists
fi, err := os.Stat(clipboardPath)
if err != nil {
log.Fatalf("failed to paste file: failed to read cut/copied file or directory: %s", err)
} else if fi.Mode()&os.ModeDir != 0 {
log.Fatal("copying directories not yet implemented")
}
pastedFile, err := os.Create(path.Join(localPath, filepath.Base(clipboardPath)))
if err != nil {
log.Fatalf("failed to paste file: failed to create local file: %s", err)
}
clipboardFile, err := os.OpenFile(clipboardPath, os.O_RDONLY, 0655)
// TODO warn exists
err = copyRecursive(clipboardPath, path.Join(localPath, filepath.Base(clipboardPath)))
if err != nil {
pastedFile.Close()
log.Fatalf("failed to paste file: failed to read clipboard file: %s", err)
log.Fatalf("failed to paste file: %s", err)
}
io.Copy(pastedFile, clipboardFile)
if clipboardCut {
os.RemoveAll(clipboardPath)
}
clipboardFile.Close()
pastedFile.Close()
}
clipboardPath = ""


+ 27
- 1
main.go View File

@ -5,7 +5,9 @@ import (
"flag"
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
"sync"
@ -30,6 +32,8 @@ var (
localPath string
remotePath string
connectAddress string
localEntries []*adb.DirEntry
localEntriesShown []*adb.DirEntry
remoteEntries []*adb.DirEntry
@ -118,6 +122,7 @@ func main() {
flag.StringVar(&localPath, "local", "", "local starting directory")
flag.StringVar(&remotePath, "remote", "", "remote starting directory")
flag.BoolVar(&showHidden, "hidden", false, "show hidden files and folders")
flag.StringVar(&connectAddress, "connect", "", "connect to device over network")
flag.Parse()
err := run()
@ -142,11 +147,32 @@ func run() error {
}
var err error
bridge, err = connect("", 0, "")
bridge, err = connectBridge("", 0, "")
if err != nil {
return fmt.Errorf("failed to connect to ADB server: %s", err)
}
if connectAddress != "" {
if !strings.ContainsRune(connectAddress, ':') {
connectAddress += ":5555"
}
host, p, err := net.SplitHostPort(connectAddress)
if err != nil {
log.Fatalf("invalid device address %s: %s", connectAddress, err)
}
port, err := strconv.Atoi(p)
if err != nil {
log.Fatalf("invalid device address %s: %s", connectAddress, err)
}
err = bridge.connectDevice(host, port)
if err != nil {
log.Fatalf("failed to connect to device %s: %s", connectAddress, err)
}
}
err = setKeyBinds()
if err != nil {
return fmt.Errorf("failed to set keybinds: %s", err)


Loading…
Cancel
Save