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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use std::{
ffi::OsStr,
io,
path::Path,
process::{Child, ChildStdin, Command, Stdio},
};
use crate::utils::is_recoverable_kill_error;
pub trait ConfigureCommand {
fn current_dir(&mut self, dir: &Path);
fn env(&mut self, name: &str, value: &OsStr);
}
impl ConfigureCommand for Command {
fn current_dir(&mut self, dir: &Path) {
self.current_dir(dir);
}
fn env(&mut self, name: &str, value: &OsStr) {
self.env(name, value);
}
}
pub trait SpawnShell: ConfigureCommand {
type ShellProcess: ShellProcess;
type Reader: io::Read + 'static + Send;
type Writer: io::Write + 'static + Send;
fn spawn_shell(&mut self) -> io::Result<SpawnedShell<Self>>;
}
pub trait ShellProcess {
fn check_is_alive(&mut self) -> io::Result<()>;
fn terminate(self) -> io::Result<()>;
fn is_echoing(&self) -> bool {
false
}
}
#[derive(Debug)]
pub struct SpawnedShell<S: SpawnShell + ?Sized> {
pub shell: S::ShellProcess,
pub reader: S::Reader,
pub writer: S::Writer,
}
impl SpawnShell for Command {
type ShellProcess = Child;
type Reader = os_pipe::PipeReader;
type Writer = ChildStdin;
#[cfg_attr(feature = "tracing", tracing::instrument(level = "debug", err))]
fn spawn_shell(&mut self) -> io::Result<SpawnedShell<Self>> {
let (pipe_reader, pipe_writer) = os_pipe::pipe()?;
#[cfg(feature = "tracing")]
tracing::debug!("created OS pipe");
let mut shell = self
.stdin(Stdio::piped())
.stdout(pipe_writer.try_clone()?)
.stderr(pipe_writer)
.spawn()?;
#[cfg(feature = "tracing")]
tracing::debug!("created child");
self.stdout(Stdio::null()).stderr(Stdio::null());
let stdin = shell.stdin.take().unwrap();
Ok(SpawnedShell {
shell,
reader: pipe_reader,
writer: stdin,
})
}
}
impl ShellProcess for Child {
#[cfg_attr(feature = "tracing", tracing::instrument(level = "debug", err))]
fn check_is_alive(&mut self) -> io::Result<()> {
if let Some(exit_status) = self.try_wait()? {
let message = format!("Shell process has prematurely exited: {exit_status}");
Err(io::Error::new(io::ErrorKind::BrokenPipe, message))
} else {
Ok(())
}
}
#[cfg_attr(feature = "tracing", tracing::instrument(level = "debug", err))]
fn terminate(mut self) -> io::Result<()> {
if self.try_wait()?.is_none() {
self.kill().or_else(|err| {
if is_recoverable_kill_error(&err) {
Ok(())
} else {
Err(err)
}
})?;
}
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct Echoing<S> {
inner: S,
is_echoing: bool,
}
impl<S> Echoing<S> {
pub fn new(inner: S, is_echoing: bool) -> Self {
Self { inner, is_echoing }
}
}
impl<S: ConfigureCommand> ConfigureCommand for Echoing<S> {
fn current_dir(&mut self, dir: &Path) {
self.inner.current_dir(dir);
}
fn env(&mut self, name: &str, value: &OsStr) {
self.inner.env(name, value);
}
}
impl<S: SpawnShell> SpawnShell for Echoing<S> {
type ShellProcess = Echoing<S::ShellProcess>;
type Reader = S::Reader;
type Writer = S::Writer;
fn spawn_shell(&mut self) -> io::Result<SpawnedShell<Self>> {
let spawned = self.inner.spawn_shell()?;
Ok(SpawnedShell {
shell: Echoing {
inner: spawned.shell,
is_echoing: self.is_echoing,
},
reader: spawned.reader,
writer: spawned.writer,
})
}
}
impl<S: ShellProcess> ShellProcess for Echoing<S> {
fn check_is_alive(&mut self) -> io::Result<()> {
self.inner.check_is_alive()
}
fn terminate(self) -> io::Result<()> {
self.inner.terminate()
}
fn is_echoing(&self) -> bool {
self.is_echoing
}
}