mirror of
https://github.com/taigrr/adb.git
synced 2026-04-02 02:58:42 -07:00
arbitrary exec example functional
This commit is contained in:
2
adb.go
2
adb.go
@@ -38,7 +38,7 @@ type ConnOptions struct {
|
||||
SerialNo Serial
|
||||
}
|
||||
|
||||
// Connect to a device by serial number or IP.
|
||||
// Connect to a device by IP:port.
|
||||
//
|
||||
// This will return a Device struct, which can be used to call other methods.
|
||||
// If the connection fails or cannot complete on time, Connect will return an error.
|
||||
|
||||
36
examples/shellExec/main.go
Normal file
36
examples/shellExec/main.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/taigrr/adb"
|
||||
)
|
||||
|
||||
func main() {
|
||||
command := "ls"
|
||||
ctx := context.TODO()
|
||||
devs, err := adb.Devices(ctx)
|
||||
if err != nil {
|
||||
fmt.Printf("Error enumerating devices: %v\n", err)
|
||||
return
|
||||
}
|
||||
for _, dev := range devs {
|
||||
if !dev.IsAuthorized {
|
||||
fmt.Printf("Dev `%s` is not authorized, authorize it to continue.\n", dev.SerialNo)
|
||||
continue
|
||||
}
|
||||
stdout, stderr, errcode, err := dev.Shell(ctx, command)
|
||||
_ = stderr
|
||||
_ = errcode
|
||||
switch {
|
||||
case err == nil:
|
||||
case errors.Is(err, adb.ErrUnspecified):
|
||||
default:
|
||||
fmt.Printf("Error running shell command `%s` on dev `%s`: %v\n", command, dev.SerialNo, err)
|
||||
continue
|
||||
}
|
||||
fmt.Printf("%s:\n\n%s\n", dev.SerialNo, stdout)
|
||||
}
|
||||
}
|
||||
2
go.mod
2
go.mod
@@ -1,3 +1,5 @@
|
||||
module github.com/taigrr/adb
|
||||
|
||||
go 1.16
|
||||
|
||||
require github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
|
||||
|
||||
2
go.sum
2
go.sum
@@ -0,0 +1,2 @@
|
||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
|
||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
|
||||
|
||||
12
shell.go
12
shell.go
@@ -3,6 +3,8 @@ package adb
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/shlex"
|
||||
)
|
||||
|
||||
// Shell allows you to run an arbitrary shell command against a device.
|
||||
@@ -12,8 +14,14 @@ import (
|
||||
// Instead of using Shell, please consider submitting a PR with the functionality
|
||||
// you require.
|
||||
func (d Device) Shell(ctx context.Context, command string) (stdout string, stderr string, ErrCode int, err error) {
|
||||
|
||||
return "", "", 1, nil
|
||||
cmd, err := shlex.Split(command)
|
||||
if err != nil {
|
||||
return "", "", 1, err
|
||||
}
|
||||
prefix := []string{"-s", string(d.SerialNo), "shell"}
|
||||
cmd = append(prefix, cmd...)
|
||||
stdout, stderr, errcode, err := execute(ctx, cmd)
|
||||
return stdout, stderr, errcode, err
|
||||
}
|
||||
|
||||
// adb shell wm size
|
||||
|
||||
2
util.go
2
util.go
@@ -47,7 +47,7 @@ func execute(ctx context.Context, args []string) (string, string, int, error) {
|
||||
if customErr != nil {
|
||||
err = customErr
|
||||
}
|
||||
if code != 0 && err == nil {
|
||||
if _, ok := err.(*exec.ExitError); ok && code != 0 {
|
||||
err = fmt.Errorf("received error code %d for stderr `%s`: %w", code, warnings, ErrUnspecified)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user