-
Notifications
You must be signed in to change notification settings - Fork 8
/
exec.go
90 lines (70 loc) · 2.94 KB
/
exec.go
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
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"v.io/x/lib/cmdline"
"v.io/x/lib/gosh"
)
var cmdMadbExec = &cmdline.Command{
Runner: subCommandRunner{subCmd: runMadbExecForDevice},
Name: "exec",
Short: "Run the provided adb command on all devices and emulators concurrently",
Long: `
Runs the provided adb command on all devices and emulators concurrently.
For example, the following line:
madb exec push ./foo.txt /sdcard/foo.txt
copies the ./foo.txt file to /sdcard/foo.txt for all the currently connected devices.
There are a few pre-defined keywords that can be expanded within an argument.
"{{index}}" : the index of the current device, starting from 1.
"{{name}}" : the nickname of the current device, or the serial number if a nickname is not set.
"{{serial}}" : the serial number of the current device.
For example, the following line:
madb exec -n=Alice,Bob push ./{{name}}.txt /sdcard/{{name}}.txt
copies the ./Alice.txt file to the device named Alice, and ./Bob.txt to the device named Bob.
Note that you should type in "{{name}}" as-is, with the opening/closing curly braces, similar to
when you're using a template library such as mustache.
To see the list of available adb commands, type 'adb help'.
`,
ArgsName: "<command>",
ArgsLong: `
<command> is a normal adb command, which will be executed on all devices and emulators.
`,
}
var cmdMadbShell = &cmdline.Command{
Runner: subCommandRunner{subCmd: runMadbShellForDevice},
Name: "shell",
Short: "Run the provided adb shell command on all devices and emulators concurrently",
Long: `
Runs the provided adb shell command on all devices and emulators concurrently.
This command is a shorthand syntax for 'madb exec shell <command...>'.
See 'madb help exec' for more details.
`,
ArgsName: "<command>",
ArgsLong: `
<command> is a normal adb shell command, which will be executed on all devices and emulators.
`,
}
func runMadbExecForDevice(env *cmdline.Env, args []string, d device, properties variantProperties) error {
return runAdbCommandForDevice(env, args, d, properties, false)
}
func runMadbShellForDevice(env *cmdline.Env, args []string, d device, properties variantProperties) error {
return runAdbCommandForDevice(env, args, d, properties, true)
}
func runAdbCommandForDevice(env *cmdline.Env, args []string, d device, properties variantProperties, isShellCmd bool) error {
sh := gosh.NewShell(nil)
defer sh.Cleanup()
sh.ContinueOnError = true
// Expand the keywords before running the command.
expandedArgs := make([]string, len(args))
for i, arg := range args {
expandedArgs[i] = expandKeywords(arg, d)
}
cmdArgs := []string{"-s", d.Serial}
if isShellCmd {
cmdArgs = append(cmdArgs, "shell")
}
cmdArgs = append(cmdArgs, expandedArgs...)
cmd := sh.Cmd("adb", cmdArgs...)
return runGoshCommandForDevice(cmd, d, false)
}