| // Copyright (C) 2016 The Android Open Source Project |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| // Package adb provides an interface to the Android Debug Bridge. |
| package adb |
| |
| import ( |
| "os" |
| "os/exec" |
| "path/filepath" |
| |
| "android.googlesource.com/platform/tools/gpu/client/shell" |
| ) |
| |
| // The path to the adb executable, or an empty string if the adb executable was |
| // not found. |
| var ADB string |
| |
| func init() { |
| search := "adb" |
| // If ANDROID_HOME is set, build a fully rooted path |
| // We still want to call LookPath to pick up the extension and check the binary exists |
| if home := os.Getenv("ANDROID_HOME"); home != "" { |
| search = filepath.Join(home, "platform-tools", search) |
| } |
| |
| // Search the path if no directory prefix, otherwise just check the executable. |
| if p, err := exec.LookPath(search); err == nil { |
| if p, err = filepath.Abs(p); err == nil { |
| ADB = p |
| } |
| } |
| } |
| |
| // Command is a helper that builds a shell.Cmd with the device as its target. |
| func (b *binding) Command(name string, args ...string) shell.Cmd { |
| return shell.Command(name, args...).On(deviceTarget{b}) |
| } |
| |
| // Shell is a helper that builds a shell.Cmd with d.ShellTarget() as its target |
| func (b *binding) Shell(name string, args ...string) shell.Cmd { |
| return shell.Command(name, args...).On(shellTarget{b}) |
| } |
| |
| func (b *binding) prepareADBCommand(cmd shell.Cmd, useShell bool) (shell.Process, error) { |
| // Adjust the command to: "adb -s device [shell] <cmd.Name> <cmd.Args...>" |
| old := cmd.Args |
| cmd.Args = make([]string, 0, len(old)+4) |
| cmd.Args = append(cmd.Args, "-s", b.To.URL().Host) |
| if useShell { |
| cmd.Args = append(cmd.Args, "shell") |
| } |
| cmd.Args = append(cmd.Args, cmd.Name) |
| cmd.Args = append(cmd.Args, old...) |
| cmd.Name = ADB |
| // And delegate to the normal local target |
| return shell.LocalTarget.Start(cmd) |
| } |
| |
| type deviceTarget struct{ b *binding } |
| |
| func (t deviceTarget) Start(cmd shell.Cmd) (shell.Process, error) { |
| return t.b.prepareADBCommand(cmd, false) |
| } |
| |
| func (t deviceTarget) String() string { |
| return "command:" + t.b.String() |
| } |
| |
| type shellTarget struct{ b *binding } |
| |
| func (t shellTarget) Start(cmd shell.Cmd) (shell.Process, error) { |
| return t.b.prepareADBCommand(cmd, true) |
| } |
| |
| func (t shellTarget) String() string { |
| return "shell:" + t.b.String() |
| } |