forked from zhaofengli/colmena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.rs
258 lines (210 loc) · 6.74 KB
/
util.rs
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use std::convert::TryFrom;
use std::process::Stdio;
use async_trait::async_trait;
use futures::future::join3;
use serde::de::DeserializeOwned;
use tokio::io::{AsyncBufReadExt, AsyncRead, BufReader};
use tokio::process::Command;
use super::error::{ColmenaError, ColmenaResult};
use super::job::JobHandle;
use super::nix::deployment::TargetNodeMap;
use super::nix::StorePath;
const NEWLINE: u8 = 0xa;
/// Non-interactive execution of an arbitrary command.
pub struct CommandExecution {
command: Command,
job: Option<JobHandle>,
hide_stdout: bool,
stdout: Option<String>,
stderr: Option<String>,
}
/// Helper extensions for Commands.
#[async_trait]
pub trait CommandExt {
/// Runs the command with stdout and stderr passed through to the user.
async fn passthrough(&mut self) -> ColmenaResult<()>;
/// Runs the command, capturing the output as a String.
async fn capture_output(&mut self) -> ColmenaResult<String>;
/// Runs the command, capturing deserialized output from JSON.
async fn capture_json<T>(&mut self) -> ColmenaResult<T>
where
T: DeserializeOwned;
/// Runs the command, capturing a single store path.
async fn capture_store_path(&mut self) -> ColmenaResult<StorePath>;
}
impl CommandExecution {
pub fn new(command: Command) -> Self {
Self {
command,
job: None,
hide_stdout: false,
stdout: None,
stderr: None,
}
}
/// Sets the job associated with this execution.
pub fn set_job(&mut self, job: Option<JobHandle>) {
self.job = job;
}
/// Sets whether to hide stdout.
pub fn set_hide_stdout(&mut self, hide_stdout: bool) {
self.hide_stdout = hide_stdout;
}
/// Returns logs from the last invocation.
pub fn get_logs(&self) -> (Option<&String>, Option<&String>) {
(self.stdout.as_ref(), self.stderr.as_ref())
}
/// Runs the command.
pub async fn run(&mut self) -> ColmenaResult<()> {
self.command.stdin(Stdio::null());
self.command.stdout(Stdio::piped());
self.command.stderr(Stdio::piped());
self.stdout = Some(String::new());
self.stderr = Some(String::new());
let mut child = self.command.spawn()?;
let stdout = BufReader::new(child.stdout.take().unwrap());
let stderr = BufReader::new(child.stderr.take().unwrap());
let stdout_job = if self.hide_stdout {
None
} else {
self.job.clone()
};
let futures = join3(
capture_stream(stdout, stdout_job, false),
capture_stream(stderr, self.job.clone(), true),
child.wait(),
);
let (stdout, stderr, wait) = futures.await;
self.stdout = Some(stdout?);
self.stderr = Some(stderr?);
let exit = wait?;
if exit.success() {
Ok(())
} else {
Err(exit.into())
}
}
}
#[async_trait]
impl CommandExt for Command {
/// Runs the command with stdout and stderr passed through to the user.
async fn passthrough(&mut self) -> ColmenaResult<()> {
let exit = self.spawn()?.wait().await?;
if exit.success() {
Ok(())
} else {
Err(exit.into())
}
}
/// Captures output as a String.
async fn capture_output(&mut self) -> ColmenaResult<String> {
// We want the user to see the raw errors
let output = self
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()?
.wait_with_output()
.await?;
if output.status.success() {
// FIXME: unwrap
Ok(String::from_utf8(output.stdout).unwrap())
} else {
Err(output.status.into())
}
}
/// Captures deserialized output from JSON.
async fn capture_json<T>(&mut self) -> ColmenaResult<T>
where
T: DeserializeOwned,
{
let output = self.capture_output().await?;
serde_json::from_str(&output).map_err(|_| ColmenaError::BadOutput {
output: output.clone(),
})
}
/// Captures a single store path.
async fn capture_store_path(&mut self) -> ColmenaResult<StorePath> {
let output = self.capture_output().await?;
let path = output.trim_end().to_owned();
StorePath::try_from(path)
}
}
#[async_trait]
impl CommandExt for CommandExecution {
async fn passthrough(&mut self) -> ColmenaResult<()> {
self.run().await
}
/// Captures output as a String.
async fn capture_output(&mut self) -> ColmenaResult<String> {
self.run().await?;
let (stdout, _) = self.get_logs();
Ok(stdout.unwrap().to_owned())
}
/// Captures deserialized output from JSON.
async fn capture_json<T>(&mut self) -> ColmenaResult<T>
where
T: DeserializeOwned,
{
let output = self.capture_output().await?;
serde_json::from_str(&output).map_err(|_| ColmenaError::BadOutput {
output: output.clone(),
})
}
/// Captures a single store path.
async fn capture_store_path(&mut self) -> ColmenaResult<StorePath> {
let output = self.capture_output().await?;
let path = output.trim_end().to_owned();
StorePath::try_from(path)
}
}
pub async fn capture_stream<R>(
mut stream: BufReader<R>,
job: Option<JobHandle>,
stderr: bool,
) -> ColmenaResult<String>
where
R: AsyncRead + Unpin,
{
let mut log = String::new();
loop {
let mut line = Vec::new();
let len = stream.read_until(NEWLINE, &mut line).await?;
let line = String::from_utf8_lossy(&line);
if len == 0 {
break;
}
let trimmed = line.trim_end();
if let Some(job) = &job {
if stderr {
job.stderr(trimmed.to_string())?;
} else {
job.stdout(trimmed.to_string())?;
}
}
log += trimmed;
log += "\n";
}
Ok(log)
}
pub fn get_label_width(targets: &TargetNodeMap) -> Option<usize> {
targets.keys().map(|n| n.len()).max()
}
#[cfg(test)]
mod tests {
use super::*;
use tokio::io::BufReader;
use tokio_test::block_on;
#[test]
fn test_capture_stream() {
let expected = "Hello\nWorld\n";
let stream = BufReader::new(expected.as_bytes());
let captured = block_on(async { capture_stream(stream, None, false).await.unwrap() });
assert_eq!(expected, captured);
}
#[test]
fn test_capture_stream_with_invalid_utf8() {
let stream = BufReader::new([0x80, 0xa].as_slice());
let captured = block_on(async { capture_stream(stream, None, false).await.unwrap() });
assert_eq!("\u{fffd}\n", captured);
}
}