Files
adb/util.go
Tai Groot 9ed4d3ef4f 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
2026-03-14 06:33:10 +00:00

63 lines
1.1 KiB
Go

package adb
import (
"bytes"
"context"
"fmt"
"os/exec"
"sync"
)
var (
adb string
adbOnce sync.Once
)
func findADB() {
path, err := exec.LookPath("adb")
if err != nil {
adb = ""
return
}
adb = path
}
func execute(ctx context.Context, args []string) (string, string, int, error) {
adbOnce.Do(findADB)
if adb == "" {
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()
customErr := filterErr(warnings)
if customErr != nil {
err = customErr
}
if _, ok := err.(*exec.ExitError); ok && code != 0 {
err = fmt.Errorf("received error code %d for stderr `%s`: %w", code, warnings, ErrUnspecified)
}
return output, warnings, code, err
}
// filterErr matches known output strings against the stderr.
//
// The inferred error type is then returned.
// TODO: implement
func filterErr(stderr string) error {
return nil
}