Browse Source

Initial commit

merge-requests/2/head
Trevor Slocum 1 year ago
commit
cef0c8414c
12 changed files with 551 additions and 0 deletions
  1. +4
    -0
      .gitignore
  2. +36
    -0
      .gitlab-ci.yml
  3. +21
    -0
      LICENSE
  4. +18
    -0
      README.md
  5. +1
    -0
      adbfm.go
  6. +5
    -0
      cmd/adbfm-gtk/main.go
  7. +111
    -0
      cmd/adbfm/gui.go
  8. +72
    -0
      cmd/adbfm/gui_key.go
  9. +142
    -0
      cmd/adbfm/main.go
  10. +11
    -0
      go.mod
  11. +30
    -0
      go.sum
  12. +100
    -0
      pkg/android/adb.go

+ 4
- 0
.gitignore View File

@ -0,0 +1,4 @@
.idea/
*.sh
cmd/adbfm/adbfm
cmd/adbfm-gtk/adbfm-gtk

+ 36
- 0
.gitlab-ci.yml View File

@ -0,0 +1,36 @@
image: golang:latest
stages:
- validate
- build
fmt:
stage: validate
script:
- gofmt -l -s -e .
- exit $(gofmt -l -s -e . | wc -l)
vet:
stage: validate
script:
- apt-get update && apt-get install -y libgtk-3-dev
- go vet -composites=false ./...
test:
stage: validate
script:
- apt-get update && apt-get install -y libgtk-3-dev
- go test -race -v ./...
build-adbfm:
stage: build
script:
- cd cmd/adbfm
- go build
build-adbfm-gtk:
stage: build
script:
- apt-get update && apt-get install -y libgtk-3-dev
- cd cmd/adbfm-gtk
- go build

+ 21
- 0
LICENSE View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020 Trevor Slocum <trevor@rocketnine.space>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

+ 18
- 0
README.md View File

@ -0,0 +1,18 @@
# adbfm
[![GoDoc](https://godoc.org/gitlab.com/tslocum/adbfm?status.svg)](https://godoc.org/gitlab.com/tslocum/adbfm)
[![CI status](https://gitlab.com/tslocum/adbfm/badges/master/pipeline.svg)](https://gitlab.com/tslocum/adbfm/commits/master)
[![Donate](https://img.shields.io/liberapay/receives/rocketnine.space.svg?logo=liberapay)](https://liberapay.com/rocketnine.space)
ADB file manager
## Dependencies
* [zach-klippenstein/goadb](https://github.com/zach-klippenstein/goadb)
## Documentation
Documentation is available via [gdooc](https://godoc.org/gitlab.com/tslocum/adbfm).
## Support
Please share issues/suggestions [here](https://gitlab.com/tslocum/adbfm/issues).

+ 1
- 0
adbfm.go View File

@ -0,0 +1 @@
package adbfm

+ 5
- 0
cmd/adbfm-gtk/main.go View File

@ -0,0 +1,5 @@
package main
func main() {
// TODO
}

+ 111
- 0
cmd/adbfm/gui.go View File

@ -0,0 +1,111 @@
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/gdamore/tcell"
"gitlab.com/tslocum/cbind"
"gitlab.com/tslocum/cview"
)
var (
app *cview.Application
inputConfig = cbind.NewConfiguration()
devicesDropDown *cview.DropDown
localBuffer *cview.TextView
remoteBuffer *cview.TextView
currentFocus = 0
)
func initTUI() error {
devices, err := adb.ListDevices()
if err != nil {
log.Fatalf("failed to list devices: %s", err)
}
if len(devices) == 0 {
log.Fatal("no devices")
}
cview.Styles.TitleColor = tcell.ColorDefault
cview.Styles.BorderColor = tcell.ColorDefault
cview.Styles.PrimaryTextColor = tcell.ColorDefault
cview.Styles.PrimitiveBackgroundColor = tcell.ColorDefault
app = cview.NewApplication().
SetInputCapture(inputConfig.Capture)
devicesDropDown = cview.NewDropDown().SetLabel(" Device: ").SetLabelColor(tcell.ColorDefault)
for _, device := range devices {
devicesDropDown.AddOption(device.Serial, setDeviceFunc(adb, device))
}
localBuffer = cview.NewTextView().SetWrap(true).SetWordWrap(false)
localBuffer.SetTitleAlign(cview.AlignLeft).SetBorder(true)
remoteBuffer = cview.NewTextView().SetWrap(true).SetWordWrap(false)
remoteBuffer.SetTitleAlign(cview.AlignLeft).SetBorder(true)
// Select first device
if len(devices) > 0 {
devicesDropDown.SetCurrentOption(0)
}
pad := cview.NewTextView()
grid := cview.NewGrid().
SetRows(1, 1, -1).
SetColumns(-1, -1).
AddItem(devicesDropDown, 0, 0, 1, 2, 0, 0, true).
AddItem(pad, 1, 0, 1, 2, 0, 0, true).
AddItem(localBuffer, 2, 0, 1, 1, 0, 0, false).
AddItem(remoteBuffer, 2, 1, 1, 1, 0, 0, false)
app.SetRoot(grid, true).SetFocus(devicesDropDown)
// TODO No errors possible?
return nil
}
func focusUpdated() {
switch currentFocus {
case 0:
app.SetFocus(devicesDropDown)
case 1:
app.SetFocus(localBuffer)
case 2:
app.SetFocus(remoteBuffer)
}
}
func listLocalDir(p string) {
var entries []string
err := filepath.Walk(p, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
entries = append(entries, info.Name())
return nil
})
if err != nil {
log.Fatal(err)
}
localBuffer.SetTitle(" " + p + " ")
localBuffer.Clear()
for _, entry := range entries {
fmt.Fprintf(localBuffer, entry+"\n")
}
}

+ 72
- 0
cmd/adbfm/gui_key.go View File

@ -0,0 +1,72 @@
package main
import "github.com/gdamore/tcell"
func selectItem(ev *tcell.EventKey) *tcell.EventKey {
if currentFocus == 0 {
return ev
}
return nil
}
func previousItem(ev *tcell.EventKey) *tcell.EventKey {
if currentFocus == 0 {
return ev
}
return nil
}
func nextItem(ev *tcell.EventKey) *tcell.EventKey {
if currentFocus == 0 {
return ev
}
return nil
}
func previousPage(ev *tcell.EventKey) *tcell.EventKey {
if currentFocus == 0 {
return ev
}
return nil
}
func nextPage(ev *tcell.EventKey) *tcell.EventKey {
if currentFocus == 0 {
return ev
}
return nil
}
func previousField(_ *tcell.EventKey) *tcell.EventKey {
if currentFocus > 0 {
currentFocus--
}
focusUpdated()
return nil
}
func nextField(_ *tcell.EventKey) *tcell.EventKey {
if currentFocus < 2 {
currentFocus++
}
focusUpdated()
return nil
}
func exit(_ *tcell.EventKey) *tcell.EventKey {
return nil
}

+ 142
- 0
cmd/adbfm/main.go View File

@ -0,0 +1,142 @@
package main
import (
"fmt"
"log"
"strings"
"github.com/gdamore/tcell"
goadb "github.com/zach-klippenstein/goadb"
"gitlab.com/tslocum/adbfm/pkg/android"
"gitlab.com/tslocum/cbind"
)
type appConfig struct {
Input map[string][]string // Keybinds
}
var config = &appConfig{}
var (
adb *android.ADB
done = make(chan bool)
)
const (
actionSelect = "select"
actionPreviousItem = "previous-item"
actionNextItem = "next-item"
actionPreviousPage = "previous-page"
actionNextPage = "next-page"
actionPreviousField = "previous-field"
actionNextField = "next-field"
actionExit = "exit"
)
var actionHandlers = map[string]func(*tcell.EventKey) *tcell.EventKey{
actionSelect: selectItem,
actionPreviousItem: previousItem,
actionNextItem: nextItem,
actionPreviousPage: previousPage,
actionNextPage: nextPage,
actionPreviousField: previousField,
actionNextField: nextField,
actionExit: exit,
}
func setKeyBinds() error {
if len(config.Input) == 0 {
setDefaultKeyBinds()
}
for a, keys := range config.Input {
a = strings.ToLower(a)
handler := actionHandlers[a]
if handler == nil {
return fmt.Errorf("failed to set keybind for %s: unknown action", a)
}
for _, k := range keys {
mod, key, ch, err := cbind.Decode(k)
if err != nil {
return fmt.Errorf("failed to set keybind %s for %s: %s", k, a, err)
}
if key == tcell.KeyRune {
inputConfig.SetRune(mod, ch, handler)
} else {
inputConfig.SetKey(mod, key, handler)
}
}
}
return nil
}
func setDefaultKeyBinds() {
config.Input = map[string][]string{
actionSelect: {"Enter"},
actionPreviousItem: {"Up", "k"},
actionNextItem: {"Down", "j"},
actionPreviousPage: {"PageUp"},
actionNextPage: {"PageDown"},
actionPreviousField: {"Backtab"},
actionNextField: {"Tab"},
actionExit: {"Alt+q"},
}
}
func setDeviceFunc(adb *android.ADB, device *goadb.DeviceInfo) func() {
return func() {
err := adb.SetDevice(device)
if err != nil {
log.Fatal(err)
}
remoteBuffer.Clear()
p := "/storage/emulated/0"
remoteBuffer.SetTitle(" " + p + " ")
entries, err := adb.DirectoryEntries(p)
if err != nil {
log.Fatalf("failed to list files: %s", err)
}
for _, entry := range entries {
fmt.Fprintf(remoteBuffer, entry.Name+"\n")
}
}
}
func main() {
var err error
adb, err = android.NewADB("", 0, "")
if err != nil {
log.Fatalf("failed to connect to ADB server: %s", err)
}
err = setKeyBinds()
if err != nil {
log.Fatalf("failed to set keybinds: %s", err)
}
err = initTUI()
if err != nil {
log.Fatalf("failed to initialize user interface: %s", err)
}
listLocalDir("/home/trevor/Downloads")
go func() {
if err := app.Run(); err != nil {
log.Fatalf("failed to run application: %s", err)
}
done <- true
}()
<-done
}

+ 11
- 0
go.mod View File

@ -0,0 +1,11 @@
module gitlab.com/tslocum/adbfm
go 1.13
require (
github.com/gdamore/tcell v1.3.0
github.com/zach-klippenstein/goadb v0.0.0-20170530005145-029cc6bee481
gitlab.com/tslocum/cbind v0.1.0
gitlab.com/tslocum/cview v1.4.2-0.20200128151041-339db80f666d
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 // indirect
)

+ 30
- 0
go.sum View File

@ -0,0 +1,30 @@
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
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 v1.3.0 h1:r35w0JBADPZCVQijYebl6YMWWtHRqVEGt7kL2eBADRM=
github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebKS4zMM=
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
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.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.8 h1:3tS41NlGYSmhhe/8fhGRzc+z3AYCw1Fe1WAyLuujKs0=
github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
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/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.0 h1:Ot6y7K6mwp7Qp/MWOH5cuhf/VC5HKpJaWFvgzMC8nnM=
gitlab.com/tslocum/cbind v0.1.0/go.mod h1:xxuB0UqPYytWTkjI0l2VzOJJzqvIUf18r6YahPXYDd8=
gitlab.com/tslocum/cview v1.4.2-0.20200128151041-339db80f666d h1:5rPwwmNYGLcOsyawvAw7m/Jtwp5rAuvLoqVW5k09AP0=
gitlab.com/tslocum/cview v1.4.2-0.20200128151041-339db80f666d/go.mod h1:QbxliYQa2I32UJH2boP54jq6tnWlgm6yViaFXKGDfuM=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200103143344-a1369afcdac7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 h1:1/DFK4b7JH8DmkqhUk48onnSfrPzImPoVxuomtbT2nk=
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

+ 100
- 0
pkg/android/adb.go View File

@ -0,0 +1,100 @@
package android
import (
"errors"
"fmt"
"log"
"sync"
adb "github.com/zach-klippenstein/goadb"
)
// DefaultPort is the default port the ADB server listens on.
const DefaultPort = 5037
// ADB represents a connection with an ADB server.
type ADB struct {
bridge *adb.Adb
device *adb.Device
*sync.Mutex
}
// NewADB establishes a connection with an ADB server. When host and port are
// unspecified they are replaced with localhost and the default port.
func NewADB(host string, port int, pathToADB string) (*ADB, error) {
bridge, err := adb.NewWithConfig(adb.ServerConfig{
PathToAdb: pathToADB,
Host: host,
Port: port,
})
if err != nil {
log.Fatal(err)
}
bridge.StartServer() // Ignore error
_, err = bridge.ServerVersion()
if err != nil {
return nil, fmt.Errorf("failed to get server version: %s", err)
}
a := ADB{bridge: bridge, Mutex: new(sync.Mutex)}
return &a, nil
}
func (a *ADB) ListDevices() (devices []*adb.DeviceInfo, err error) {
a.Lock()
defer a.Unlock()
deviceInfoList, err := a.bridge.ListDevices()
if err != nil {
return nil, err
}
for _, deviceInfo := range deviceInfoList {
devices = append(devices, deviceInfo)
}
return devices, nil
}
func (a *ADB) SetDevice(deviceInfo *adb.DeviceInfo) (err error) {
a.Lock()
defer a.Unlock()
return a.setDevice(deviceInfo)
}
func (a *ADB) setDevice(deviceInfo *adb.DeviceInfo) (err error) {
device := a.bridge.Device(adb.DeviceWithSerial(deviceInfo.Serial))
if device == nil {
return fmt.Errorf("failed to get device with serial %s", deviceInfo.Serial)
}
a.device = device
return nil
}
func (a *ADB) DirectoryEntries(path string) (entries []*adb.DirEntry, err error) {
a.Lock()
defer a.Unlock()
if a.device == nil {
return nil, errors.New("no device")
}
listEntries, err := a.device.ListDirEntries(path)
if err != nil {
return nil, fmt.Errorf("failed to get dirents: %s", err)
}
for listEntries.Next() {
entries = append(entries, listEntries.Entry())
}
if listEntries.Err() != nil {
return nil, fmt.Errorf("failed to list files: %s", err)
}
return entries, nil
}

Loading…
Cancel
Save