Skip to content

Commit ad95463

Browse files
authored
Merge pull request #880 from cgwalters/cmdext-run-capture-output
utils: Add a helper to run command + capture output
2 parents 431df01 + 5025faa commit ad95463

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

utils/src/command.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ pub trait CommandRunExt {
1515
/// Execute the child process.
1616
fn run(&mut self) -> Result<()>;
1717

18-
/// Execute the child process, parsing its stdout as JSON.
18+
/// Execute the child process and capture its output. This uses `run` internally
19+
/// and will return an error if the child process exits abnormally.
20+
fn run_get_output(&mut self) -> Result<Box<dyn std::io::BufRead>>;
21+
22+
/// Execute the child process, parsing its stdout as JSON. This uses `run` internally
23+
/// and will return an error if the child process exits abnormally.
1924
fn run_and_parse_json<T: serde::de::DeserializeOwned>(&mut self) -> Result<T>;
2025
}
2126

@@ -88,14 +93,18 @@ impl CommandRunExt for Command {
8893
self
8994
}
9095

91-
/// Synchronously execute the child, and parse its stdout as JSON.
92-
fn run_and_parse_json<T: serde::de::DeserializeOwned>(&mut self) -> Result<T> {
96+
fn run_get_output(&mut self) -> Result<Box<dyn std::io::BufRead>> {
9397
let mut stdout = tempfile::tempfile()?;
9498
self.stdout(stdout.try_clone()?);
9599
self.run()?;
96100
stdout.seek(std::io::SeekFrom::Start(0)).context("seek")?;
97-
let stdout = std::io::BufReader::new(stdout);
98-
serde_json::from_reader(stdout).map_err(Into::into)
101+
Ok(Box::new(std::io::BufReader::new(stdout)))
102+
}
103+
104+
/// Synchronously execute the child, and parse its stdout as JSON.
105+
fn run_and_parse_json<T: serde::de::DeserializeOwned>(&mut self) -> Result<T> {
106+
let output = self.run_get_output()?;
107+
serde_json::from_reader(output).map_err(Into::into)
99108
}
100109
}
101110

0 commit comments

Comments
 (0)