forward stdout and stderr into the response body

This commit is contained in:
Michael 2018-08-16 17:51:33 +00:00
parent 92e5168e3e
commit 046594bec7
2 changed files with 19 additions and 3 deletions

View file

@ -57,7 +57,7 @@ impl Handler {
String::from_utf8(buf).unwrap() String::from_utf8(buf).unwrap()
}; };
match &self.action { let output = match &self.action {
Action::Command(ref cmd) => { Action::Command(ref cmd) => {
// TODO: allow some kind of simple variable replacement // TODO: allow some kind of simple variable replacement
let output = Command::new("/bin/bash") let output = Command::new("/bin/bash")
@ -75,6 +75,7 @@ impl Handler {
String::from_utf8(output.stderr).unwrap_or_else(|_| String::new()) String::from_utf8(output.stderr).unwrap_or_else(|_| String::new())
))); )));
} }
output
} }
Action::Exec(ref path) => { Action::Exec(ref path) => {
let mut child = Command::new(&path) let mut child = Command::new(&path)
@ -104,8 +105,14 @@ impl Handler {
String::from_utf8(output.stderr).unwrap_or_else(|_| String::new()) String::from_utf8(output.stderr).unwrap_or_else(|_| String::new())
))); )));
} }
output
} }
}; };
Ok(json!({})) let stdout = String::from_utf8(output.stdout).unwrap_or_else(|_| String::new());
let stderr = String::from_utf8(output.stderr).unwrap_or_else(|_| String::new());
Ok(json!({
"stdout": stdout,
"stderr": stderr,
}))
} }
} }

View file

@ -56,7 +56,16 @@ pub fn dip_service(req: Request<Body>) -> Box<Future<Item = Response<Body>, Erro
.fold(Ok(req_obj), |prev, handler| { .fold(Ok(req_obj), |prev, handler| {
prev.and_then(|val| handler.run(val)) prev.and_then(|val| handler.run(val))
}) })
.map(|_| (StatusCode::ACCEPTED, "success".to_owned())) .map(|res| {
(
StatusCode::ACCEPTED,
format!(
"stdout:\n{}\n\nstderr:\n{}",
res.get("stdout").and_then(|v| v.as_str()).unwrap_or(""),
res.get("stderr").and_then(|v| v.as_str()).unwrap_or(""),
),
)
})
.unwrap_or_else(|err| (StatusCode::BAD_REQUEST, format!("Error: {}", err))); .unwrap_or_else(|err| (StatusCode::BAD_REQUEST, format!("Error: {}", err)));
Response::builder() Response::builder()
.status(code) .status(code)