Parse the output from nix.
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
use sqlx::Row;
|
||||
use tokio::io::AsyncBufReadExt;
|
||||
use tokio::io::BufReader;
|
||||
use tokio::io::Lines;
|
||||
use tokio::process::Child;
|
||||
use tokio::process::ChildStderr;
|
||||
use tokio::process::ChildStdout;
|
||||
|
||||
use crate::Result;
|
||||
use crate::database::db_handle::DbHandle;
|
||||
use crate::nix_util::nix_output_stream::NixOutputStream;
|
||||
use crate::nix_util::output_stream::OutputStream;
|
||||
|
||||
use super::nix_output_stream::NixMessage;
|
||||
|
||||
pub(crate) struct RunningBuild<'db> {
|
||||
db_handle: &'db DbHandle,
|
||||
@@ -34,7 +33,8 @@ impl<'db> RunningBuild<'db> {
|
||||
.await?
|
||||
.try_get("id")?;
|
||||
|
||||
let mut output_stream = OutputStream::from_child(&mut child)?;
|
||||
let output_stream = OutputStream::from_child(&mut child)?;
|
||||
let mut nix_output_stream = NixOutputStream::new(output_stream);
|
||||
|
||||
let exit_status_handle = tokio::spawn(async move {
|
||||
let status = child
|
||||
@@ -44,14 +44,9 @@ impl<'db> RunningBuild<'db> {
|
||||
status
|
||||
});
|
||||
|
||||
loop {
|
||||
let next_line = output_stream.next_line().await?;
|
||||
match next_line {
|
||||
OutputLine::Stdout(line) | OutputLine::Stderr(line) => {
|
||||
self.handle_line(line)?;
|
||||
}
|
||||
OutputLine::Done => break,
|
||||
};
|
||||
while let Some(message) = nix_output_stream.next().await? {
|
||||
// foo
|
||||
self.handle_message(message)?;
|
||||
}
|
||||
|
||||
let exit_status = exit_status_handle.await?;
|
||||
@@ -73,84 +68,8 @@ impl<'db> RunningBuild<'db> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_line(&mut self, line: String) -> Result<()> {
|
||||
fn handle_message(&mut self, message: NixMessage) -> Result<()> {
|
||||
// println!("OUT: {:?}", message);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct OutputStream {
|
||||
stdout: Option<Lines<BufReader<ChildStdout>>>,
|
||||
stderr: Option<Lines<BufReader<ChildStderr>>>,
|
||||
}
|
||||
|
||||
impl OutputStream {
|
||||
pub(crate) fn from_child(child: &mut Child) -> Result<Self> {
|
||||
let stdout = child
|
||||
.stdout
|
||||
.take()
|
||||
.expect("child did not have a handle to stdout");
|
||||
let stderr = child
|
||||
.stderr
|
||||
.take()
|
||||
.expect("child did not have a handle to stderr");
|
||||
let stdout = BufReader::new(stdout).lines();
|
||||
let stderr = BufReader::new(stderr).lines();
|
||||
Ok(OutputStream {
|
||||
stdout: Some(stdout),
|
||||
stderr: Some(stderr),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn next_line(&mut self) -> Result<OutputLine> {
|
||||
loop {
|
||||
match (&mut self.stdout, &mut self.stderr) {
|
||||
(None, None) => {
|
||||
return Ok(OutputLine::Done);
|
||||
}
|
||||
(None, Some(err)) => {
|
||||
if let Some(line) = err.next_line().await? {
|
||||
return Ok(OutputLine::Stderr(line));
|
||||
} else {
|
||||
return Ok(OutputLine::Done);
|
||||
}
|
||||
}
|
||||
(Some(out), None) => {
|
||||
if let Some(line) = out.next_line().await? {
|
||||
return Ok(OutputLine::Stdout(line));
|
||||
} else {
|
||||
return Ok(OutputLine::Done);
|
||||
}
|
||||
}
|
||||
(Some(out), Some(err)) => {
|
||||
tokio::select! {
|
||||
Ok(line) = out.next_line() => match line {
|
||||
Some(line) => {
|
||||
return Ok(OutputLine::Stdout(line));
|
||||
},
|
||||
None => {
|
||||
self.stdout.take();
|
||||
},
|
||||
},
|
||||
Ok(line) = err.next_line() => match line {
|
||||
Some(line) => {
|
||||
return Ok(OutputLine::Stderr(line));
|
||||
},
|
||||
None => {
|
||||
self.stderr.take();
|
||||
},
|
||||
},
|
||||
else => {
|
||||
return Ok(OutputLine::Done);
|
||||
},
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum OutputLine {
|
||||
Stdout(String),
|
||||
Stderr(String),
|
||||
Done,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user