android_tools/adb/
adb_tools.rs

1use crate::error::*;
2use std::{
3    path::{Path, PathBuf},
4    process::Command,
5};
6
7#[derive(Clone, Default)]
8pub struct AdbTools {
9    install: Option<PathBuf>,
10    forward: Option<String>,
11    connect: Option<String>,
12    devices: bool,
13    devices_l: bool,
14    kill_server: bool,
15    tcpip: Option<String>,
16    help: bool,
17    d: bool,
18    e: bool,
19    s: bool,
20    a: bool,
21    h: Option<String>,
22    p: Option<String>,
23    l: Option<String>,
24    version: bool,
25}
26
27impl AdbTools {
28    pub fn new() -> Self {
29        Self {
30            ..Default::default()
31        }
32    }
33
34    /// Only push files that are newer on the host than the device
35    pub fn install(&mut self, install: &Path) -> &mut Self {
36        self.install = Some(install.to_owned());
37        self
38    }
39
40    pub fn forward(&mut self, forward: String) -> &mut Self {
41        self.forward = Some(forward);
42        self
43    }
44
45    pub fn connect(&mut self, connect: String) -> &mut Self {
46        self.connect = Some(connect);
47        self
48    }
49
50    pub fn devices(&mut self, devices: bool) -> &mut Self {
51        self.devices = devices;
52        self
53    }
54
55    pub fn devices_l(&mut self, devices_l: bool) -> &mut Self {
56        self.devices_l = devices_l;
57        self
58    }
59
60    pub fn kill_server(&mut self, kill_server: bool) -> &mut Self {
61        self.kill_server = kill_server;
62        self
63    }
64
65    pub fn tcpip(&mut self, tcpip: String) -> &mut Self {
66        self.tcpip = Some(tcpip);
67        self
68    }
69
70    /// Show this help message
71    pub fn help(&mut self, help: bool) -> &mut Self {
72        self.help = help;
73        self
74    }
75
76    /// Use USB device (error if multiple devices connected)
77    pub fn d(&mut self, d: bool) -> &mut Self {
78        self.d = d;
79        self
80    }
81
82    /// Use TCP/IP device (error if multiple TCP/IP devices available)
83    pub fn e(&mut self, e: bool) -> &mut Self {
84        self.e = e;
85        self
86    }
87
88    /// Use device with given serial (overrides $ANDROID_SERIAL)
89    pub fn s(&mut self, s: bool) -> &mut Self {
90        self.s = s;
91        self
92    }
93
94    /// Listen on all network interfaces, not just localhost
95    pub fn a(&mut self, a: bool) -> &mut Self {
96        self.a = a;
97        self
98    }
99
100    /// Name of adb server host [default=localhost]
101    pub fn h(&mut self, h: String) -> &mut Self {
102        self.h = Some(h);
103        self
104    }
105
106    /// Port of adb server [default=5037]
107    pub fn p(&mut self, p: String) -> &mut Self {
108        self.p = Some(p);
109        self
110    }
111
112    /// Listen on given socket for adb server [default=tcp:localhost:5037]
113    pub fn l(&mut self, l: String) -> &mut Self {
114        self.l = Some(l);
115        self
116    }
117
118    pub fn version(&mut self, version: bool) -> &mut Self {
119        self.version = version;
120        self
121    }
122    pub fn run(&self) -> Result<()> {
123        let mut adb = Command::new("adb");
124        if self.devices {
125            adb.arg("devices");
126        }
127        if let Some(install) = &self.install {
128            adb.arg("install").arg(install);
129        }
130        if let Some(forward) = &self.forward {
131            adb.arg("forward").arg(forward);
132        }
133        if let Some(connect) = &self.connect {
134            adb.arg("connect").arg(connect);
135        }
136        if self.devices_l {
137            adb.arg("devices -l");
138        }
139        if self.kill_server {
140            adb.arg("kill-server");
141        }
142        if let Some(tcpip) = &self.tcpip {
143            adb.arg("tcpip").arg(tcpip);
144        }
145        if self.help {
146            adb.arg("--help");
147        }
148        if self.d {
149            adb.arg("-d");
150        }
151        if self.e {
152            adb.arg("-e");
153        }
154        if self.s {
155            adb.arg("-s");
156        }
157        if self.a {
158            adb.arg("-a");
159        }
160        if let Some(h) = &self.h {
161            adb.arg("-H").arg(h);
162        }
163        if let Some(p) = &self.p {
164            adb.arg("-P").arg(p);
165        }
166        if let Some(l) = &self.l {
167            adb.arg("-L").arg(l);
168        }
169        if self.version {
170            adb.arg("--version");
171        }
172        adb.output_err(true)?;
173        Ok(())
174    }
175}