-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.go
More file actions
78 lines (68 loc) · 1.52 KB
/
util.go
File metadata and controls
78 lines (68 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package adb
import (
"bytes"
"context"
"fmt"
"os/exec"
"strings"
"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.
func filterErr(stderr string) error {
if stderr == "" {
return nil
}
switch {
case strings.Contains(stderr, "device not found"):
return ErrDeviceNotFound
case strings.Contains(stderr, "device offline"):
return ErrDeviceOffline
case strings.Contains(stderr, "device unauthorized"):
return ErrDeviceUnauthorized
case strings.Contains(stderr, "Connection refused"):
return ErrConnectionRefused
case strings.Contains(stderr, "more than one device"):
return ErrMoreThanOneDevice
default:
return nil
}
}