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
use crate::error::*;
use std::{
    path::{Path, PathBuf},
    process::Command,
};

#[derive(Clone, Default)]
pub struct Shell {
    pwd: bool,
    netstat: bool,
    service_list: bool,
    ps: bool,
    wm_size: Option<String>,
    ls: bool,
    ls_s: bool,
    ls_r: bool,
    install: Option<PathBuf>,
    install_r: Option<PathBuf>,
    uninstall: Option<String>,
    permissions_groups: bool,
    list_permissions_g_r: bool,
    dump: Option<String>,
    path: Option<PathBuf>,
}

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

    /// Print current working directory
    pub fn pwd(&mut self, pwd: bool) -> &mut Self {
        self.pwd = pwd;
        self
    }

    /// List TCP connectivity
    pub fn netstat(&mut self, netstat: bool) -> &mut Self {
        self.netstat = netstat;
        self
    }

    /// List all services
    pub fn service_list(&mut self, service_list: bool) -> &mut Self {
        self.service_list = service_list;
        self
    }

    /// Print process status
    pub fn ps(&mut self, ps: bool) -> &mut Self {
        self.ps = ps;
        self
    }

    /// Displays the current screen resolution
    pub fn wm_size(&mut self, wm_size: String) -> &mut Self {
        self.wm_size = Some(wm_size);
        self
    }

    /// List directory contents
    pub fn ls(&mut self, ls: bool) -> &mut Self {
        self.ls = ls;
        self
    }

    /// Print size of each file
    pub fn ls_s(&mut self, ls_s: bool) -> &mut Self {
        self.ls_s = ls_s;
        self
    }

    /// Install app or install app from phone path
    pub fn install(&mut self, install: &Path) -> &mut Self {
        self.install = Some(install.to_owned());
        self
    }

    /// Install app from phone path
    pub fn install_r(&mut self, install_r: &Path) -> &mut Self {
        self.install_r = Some(install_r.to_owned());
        self
    }

    /// Remove the app
    pub fn uninstall(&mut self, uninstall: String) -> &mut Self {
        self.uninstall = Some(uninstall);
        self
    }

    /// List permission groups definitions
    pub fn permissions_groups(&mut self, permissions_groups: bool) -> &mut Self {
        self.permissions_groups = permissions_groups;
        self
    }

    /// List permissions details
    pub fn list_permissions_g_r(&mut self, list_permissions_g_r: bool) -> &mut Self {
        self.list_permissions_g_r = list_permissions_g_r;
        self
    }

    /// List info on one package
    pub fn dump(&mut self, dump: String) -> &mut Self {
        self.dump = Some(dump);
        self
    }

    /// Path to the apk file
    pub fn path(&mut self, path: &Path) -> &mut Self {
        self.path = Some(path.to_owned());
        self
    }

    pub fn run(&self) -> Result<()> {
        let mut shell = Command::new("adb");
        shell.arg("shell");
        if self.pwd {
            shell.arg("pwd");
        }
        if self.netstat {
            shell.arg("netstat");
        }
        if self.service_list {
            shell.arg("service list");
        }
        if self.ps {
            shell.arg("ps");
        }
        if let Some(wm_size) = &self.wm_size {
            shell.arg("wm size").arg(wm_size);
        }
        if self.ls {
            shell.arg("ls");
        }
        if self.ls_s {
            shell.arg("ls -s");
        }
        if self.ls_r {
            shell.arg("ls -R");
        }
        if let Some(install) = &self.install {
            shell.arg("install").arg(install);
        }
        if let Some(install_r) = &self.install_r {
            shell.arg("install -r").arg(install_r);
        }
        if let Some(uninstall) = &self.uninstall {
            shell.arg("uninstall").arg(uninstall);
        }
        if self.permissions_groups {
            shell.arg("permissions groups");
        }
        if self.list_permissions_g_r {
            shell.arg("list permissions -g -r");
        }
        if let Some(dump) = &self.dump {
            shell.arg("dump").arg(dump);
        }
        if let Some(path) = &self.path {
            shell.arg("path").arg(path);
        }
        shell.output_err(true)?;
        Ok(())
    }
}