-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathcommand.rs
More file actions
203 lines (175 loc) · 5.46 KB
/
command.rs
File metadata and controls
203 lines (175 loc) · 5.46 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::{
collections::HashMap,
ffi::{OsStr, OsString},
io,
path::{Path, PathBuf},
process::Stdio,
};
#[cfg(unix)]
use fspy_shared_unix::exec::Exec;
use tokio::process::Command as TokioCommand;
use crate::{
TrackedChild,
os_impl::{self, spawn_impl},
};
#[derive(Debug)]
pub struct Command {
pub(crate) program: OsString,
pub(crate) args: Vec<OsString>,
pub(crate) envs: HashMap<OsString, OsString>,
pub(crate) cwd: Option<PathBuf>,
#[cfg(unix)]
pub(crate) arg0: Option<OsString>,
pub(crate) stderr: Option<Stdio>,
pub(crate) stdout: Option<Stdio>,
pub(crate) stdin: Option<Stdio>,
pub(crate) spy_inner: os_impl::SpyInner,
}
impl Command {
#[cfg(unix)]
pub fn get_exec(&self) -> Exec {
use std::{
iter::once,
os::unix::ffi::{OsStrExt, OsStringExt},
};
use bstr::{BString, ByteSlice as _};
let arg0 =
BString::from(self.arg0.clone().unwrap_or_else(|| self.program.clone()).into_vec());
Exec {
program: self.program.as_bytes().into(),
args: once(arg0)
.chain(self.args.iter().map(|arg| arg.as_bytes().as_bstr().to_owned()))
.collect(),
envs: self
.envs
.iter()
.map(|(name, value)| (name.as_bytes().into(), Some(value.as_bytes().into())))
.collect(),
}
}
#[cfg(unix)]
pub fn set_exec(&mut self, mut exec: Exec) {
use std::os::unix::ffi::OsStringExt;
self.program = OsString::from_vec(exec.program.into());
self.arg0 = Some(OsString::from_vec(exec.args.remove(0).into()));
self.args = exec.args.into_iter().map(|arg| OsString::from_vec(arg.into())).collect();
self.envs = exec
.envs
.into_iter()
.map(|(name, value)| {
(
OsString::from_vec(name.into()),
OsString::from_vec(value.unwrap_or_default().into()),
)
})
.collect();
}
pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command {
self.envs.remove(key.as_ref());
self
}
pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
self.stderr = Some(cfg.into());
self
}
pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
self.stdout = Some(cfg.into());
self
}
pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
self.stdin = Some(cfg.into());
self
}
pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
where
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
self.envs.insert(key.as_ref().to_os_string(), val.as_ref().to_os_string());
self
}
pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
where
I: IntoIterator<Item = (K, V)>,
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
self.envs.extend(
vars.into_iter()
.map(|(key, val)| (key.as_ref().to_os_string(), val.as_ref().to_os_string())),
);
self
}
pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Command {
self.cwd = Some(dir.as_ref().to_owned());
self
}
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command {
self.args.push(arg.as_ref().to_os_string());
self
}
pub fn args<I, S>(&mut self, args: I) -> &mut Command
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
self.args.extend(args.into_iter().map(|arg| arg.as_ref().to_os_string()));
self
}
#[cfg(unix)]
pub fn arg0<S>(&mut self, arg: S) -> &mut Command
where
S: AsRef<OsStr>,
{
self.arg0 = Some(arg.as_ref().to_os_string());
self
}
pub async fn spawn(mut self) -> io::Result<TrackedChild> {
self.resolve_program()?;
spawn_impl(self).await
}
/// Resolve program name to full path using `PATH` and cwd.
pub fn resolve_program(&mut self) -> io::Result<()> {
let mut path_env: Option<&OsStr> = None;
for (env_name, env_value) in &self.envs {
let Some(env_name) = env_name.to_str() else {
continue;
};
if env_name.eq_ignore_ascii_case("path") {
path_env = Some(env_value.as_ref());
break;
}
}
self.program = which::which_in(
self.program.as_os_str(),
path_env,
if let Some(cwd) = &self.cwd { cwd.clone() } else { std::env::current_dir()? },
)
.map_err(|err| io::Error::new(io::ErrorKind::NotFound, err))?
.into_os_string();
Ok(())
}
pub(crate) fn into_tokio_command(self) -> TokioCommand {
let mut tokio_cmd = TokioCommand::new(self.program);
if let Some(cwd) = &self.cwd {
tokio_cmd.current_dir(cwd);
}
#[cfg(unix)]
if let Some(arg0) = self.arg0 {
tokio_cmd.arg0(arg0);
}
tokio_cmd.args(self.args);
tokio_cmd.env_clear();
tokio_cmd.envs(self.envs);
if let Some(stdin) = self.stdin {
tokio_cmd.stdin(stdin);
}
if let Some(stdout) = self.stdout {
tokio_cmd.stdout(stdout);
}
if let Some(stderr) = self.stderr {
tokio_cmd.stderr(stderr);
}
tokio_cmd
}
}