mirror of
https://github.com/taigrr/adb.git
synced 2026-04-02 02:58:42 -07:00
fix: replace panic with error return in execute(), fix Pull/Push/Reboot/Root targeting
- execute() no longer panics when adb is not found; returns ErrNotInstalled - Use sync.Once for lazy adb path lookup instead of init() - Fix Pull() checking src (device path) instead of dest (local path) - Add ErrDestExists error for Pull destination conflicts - Add -s serial flag to Push, Pull, Reboot, Root for multi-device support - Root() now parses stdout to determine actual success - Fix Shell() redundant variable redeclaration - Fix 'inconsostency' typo in KillServer doc - Add doc comments to all error variables - Update Go 1.26.0 -> 1.26.1 - Add tests: ConnString, ShortenSleep, GetLength, JSON roundtrip, SequenceImporter.ToInput, insertSleeps, parseDevices edge cases, parseScreenResolution edge cases, filterErr
This commit is contained in:
35
util.go
35
util.go
@@ -4,16 +4,18 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var adb string
|
||||
var (
|
||||
adb string
|
||||
adbOnce sync.Once
|
||||
)
|
||||
|
||||
func init() {
|
||||
func findADB() {
|
||||
path, err := exec.LookPath("adb")
|
||||
if err != nil {
|
||||
log.Printf("%v", ErrNotInstalled)
|
||||
adb = ""
|
||||
return
|
||||
}
|
||||
@@ -21,25 +23,24 @@ func init() {
|
||||
}
|
||||
|
||||
func execute(ctx context.Context, args []string) (string, string, int, error) {
|
||||
var (
|
||||
err error
|
||||
stderr bytes.Buffer
|
||||
stdout bytes.Buffer
|
||||
code int
|
||||
output string
|
||||
warnings string
|
||||
)
|
||||
adbOnce.Do(findADB)
|
||||
|
||||
if adb == "" {
|
||||
panic(ErrNotInstalled)
|
||||
return "", "", -1, ErrNotInstalled
|
||||
}
|
||||
|
||||
var (
|
||||
stderr bytes.Buffer
|
||||
stdout bytes.Buffer
|
||||
)
|
||||
|
||||
cmd := exec.CommandContext(ctx, adb, args...)
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
err = cmd.Run()
|
||||
output = stdout.String()
|
||||
warnings = stderr.String()
|
||||
code = cmd.ProcessState.ExitCode()
|
||||
err := cmd.Run()
|
||||
output := stdout.String()
|
||||
warnings := stderr.String()
|
||||
code := cmd.ProcessState.ExitCode()
|
||||
|
||||
customErr := filterErr(warnings)
|
||||
if customErr != nil {
|
||||
|
||||
Reference in New Issue
Block a user