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
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use crate::error::*;
use std::{
    path::{Path, PathBuf},
    process::Command,
};

#[derive(Clone, Default)]
pub struct AdbTools {
    install: Option<PathBuf>,
    forward: Option<String>,
    connect: Option<String>,
    devices: bool,
    devices_l: bool,
    kill_server: bool,
    tcpip: Option<String>,
    help: bool,
    d: bool,
    e: bool,
    s: bool,
    a: bool,
    h: Option<String>,
    p: Option<String>,
    l: Option<String>,
    version: bool,
}

impl AdbTools {
    pub fn new() -> Self {
        Self {
            ..Default::default()
        }
    }

    /// Only push files that are newer on the host than the device
    pub fn install(&mut self, install: &Path) -> &mut Self {
        self.install = Some(install.to_owned());
        self
    }

    pub fn forward(&mut self, forward: String) -> &mut Self {
        self.forward = Some(forward);
        self
    }

    pub fn connect(&mut self, connect: String) -> &mut Self {
        self.connect = Some(connect);
        self
    }

    pub fn devices(&mut self, devices: bool) -> &mut Self {
        self.devices = devices;
        self
    }

    pub fn devices_l(&mut self, devices_l: bool) -> &mut Self {
        self.devices_l = devices_l;
        self
    }

    pub fn kill_server(&mut self, kill_server: bool) -> &mut Self {
        self.kill_server = kill_server;
        self
    }

    pub fn tcpip(&mut self, tcpip: String) -> &mut Self {
        self.tcpip = Some(tcpip);
        self
    }

    /// Show this help message
    pub fn help(&mut self, help: bool) -> &mut Self {
        self.help = help;
        self
    }

    /// Use USB device (error if multiple devices connected)
    pub fn d(&mut self, d: bool) -> &mut Self {
        self.d = d;
        self
    }

    /// Use TCP/IP device (error if multiple TCP/IP devices available)
    pub fn e(&mut self, e: bool) -> &mut Self {
        self.e = e;
        self
    }

    /// Use device with given serial (overrides $ANDROID_SERIAL)
    pub fn s(&mut self, s: bool) -> &mut Self {
        self.s = s;
        self
    }

    /// Listen on all network interfaces, not just localhost
    pub fn a(&mut self, a: bool) -> &mut Self {
        self.a = a;
        self
    }

    /// Name of adb server host [default=localhost]
    pub fn h(&mut self, h: String) -> &mut Self {
        self.h = Some(h);
        self
    }

    /// Port of adb server [default=5037]
    pub fn p(&mut self, p: String) -> &mut Self {
        self.p = Some(p);
        self
    }

    /// Listen on given socket for adb server [default=tcp:localhost:5037]
    pub fn l(&mut self, l: String) -> &mut Self {
        self.l = Some(l);
        self
    }

    pub fn version(&mut self, version: bool) -> &mut Self {
        self.version = version;
        self
    }
    pub fn run(&self) -> Result<()> {
        let mut adb = Command::new("adb");
        if self.devices {
            adb.arg("devices");
        }
        if let Some(install) = &self.install {
            adb.arg("install").arg(install);
        }
        if let Some(forward) = &self.forward {
            adb.arg("forward").arg(forward);
        }
        if let Some(connect) = &self.connect {
            adb.arg("connect").arg(connect);
        }
        if self.devices_l {
            adb.arg("devices -l");
        }
        if self.kill_server {
            adb.arg("kill-server");
        }
        if let Some(tcpip) = &self.tcpip {
            adb.arg("tcpip").arg(tcpip);
        }
        if self.help {
            adb.arg("--help");
        }
        if self.d {
            adb.arg("-d");
        }
        if self.e {
            adb.arg("-e");
        }
        if self.s {
            adb.arg("-s");
        }
        if self.a {
            adb.arg("-a");
        }
        if let Some(h) = &self.h {
            adb.arg("-H").arg(h);
        }
        if let Some(p) = &self.p {
            adb.arg("-P").arg(p);
        }
        if let Some(l) = &self.l {
            adb.arg("-L").arg(l);
        }
        if self.version {
            adb.arg("--version");
        }
        adb.output_err(true)?;
        Ok(())
    }
}