don't use unwrap() kids
This commit is contained in:
parent
b7f27cfa17
commit
134bfab8cd
2 changed files with 24 additions and 9 deletions
|
@ -68,6 +68,7 @@ impl Handler {
|
||||||
temp_path: PathBuf,
|
temp_path: PathBuf,
|
||||||
input: JsonValue,
|
input: JsonValue,
|
||||||
) -> impl Future<Item = (PathBuf, JsonValue), Error = Error> {
|
) -> impl Future<Item = (PathBuf, JsonValue), Error = Error> {
|
||||||
|
let temp_path_cp = temp_path.clone();
|
||||||
let config = {
|
let config = {
|
||||||
let mut buf: Vec<u8> = Vec::new();
|
let mut buf: Vec<u8> = Vec::new();
|
||||||
{
|
{
|
||||||
|
@ -77,14 +78,19 @@ impl Handler {
|
||||||
String::from_utf8(buf).unwrap()
|
String::from_utf8(buf).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let command_helper = move |command: &mut Command| {
|
||||||
|
command
|
||||||
|
.current_dir(&temp_path)
|
||||||
|
.env("DIP_WORKDIR", &temp_path);
|
||||||
|
};
|
||||||
|
|
||||||
let output: Box<Future<Item = JsonValue, Error = Error> + Send> = match action {
|
let output: Box<Future<Item = JsonValue, Error = Error> + Send> = match 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 mut child = Command::new("/bin/bash");
|
let mut command = Command::new("/bin/bash");
|
||||||
let child = child
|
command_helper(&mut command);
|
||||||
.current_dir(&temp_path)
|
let child = command
|
||||||
.env("DIP_ROOT", "lol")
|
.env("DIP_ROOT", "lol")
|
||||||
.env("DIP_WORKDIR", &temp_path)
|
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg(cmd)
|
.arg(cmd)
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
|
@ -106,10 +112,10 @@ impl Handler {
|
||||||
Box::new(result)
|
Box::new(result)
|
||||||
}
|
}
|
||||||
Action::Exec(ref path) => {
|
Action::Exec(ref path) => {
|
||||||
let mut child = Command::new(&path)
|
let mut command = Command::new(&path);
|
||||||
.current_dir(&temp_path)
|
command_helper(&mut command);
|
||||||
|
let mut child = command
|
||||||
.env("DIP_ROOT", "")
|
.env("DIP_ROOT", "")
|
||||||
.env("DIP_WORKDIR", &temp_path)
|
|
||||||
.arg("--config")
|
.arg("--config")
|
||||||
.arg(config)
|
.arg(config)
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
|
@ -145,6 +151,6 @@ impl Handler {
|
||||||
Box::new(result)
|
Box::new(result)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
output.map(|x| (temp_path, x))
|
output.map(|x| (temp_path_cp, x))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,16 @@ pub fn dip_service(req: Request<Body>) -> Box<Future<Item = Response<Body>, Erro
|
||||||
let temp_path = temp_dir.to_path_buf();
|
let temp_path = temp_dir.to_path_buf();
|
||||||
assert!(temp_path.exists());
|
assert!(temp_path.exists());
|
||||||
|
|
||||||
let hook = hooks.get(&name).unwrap();
|
let hook = match hooks.get(&name) {
|
||||||
|
Some(hook) => hook,
|
||||||
|
None => {
|
||||||
|
temp_dir.release();
|
||||||
|
return Response::builder()
|
||||||
|
.status(StatusCode::NOT_FOUND)
|
||||||
|
.body(Body::from("not found"))
|
||||||
|
.unwrap_or_else(|_| Response::new(Body::from("not found")));
|
||||||
|
}
|
||||||
|
};
|
||||||
let (code, msg) = match hook.handle(req_obj, temp_path) {
|
let (code, msg) = match hook.handle(req_obj, temp_path) {
|
||||||
Ok(msg) => (StatusCode::ACCEPTED, msg),
|
Ok(msg) => (StatusCode::ACCEPTED, msg),
|
||||||
Err(msg) => (StatusCode::BAD_REQUEST, msg),
|
Err(msg) => (StatusCode::BAD_REQUEST, msg),
|
||||||
|
|
Loading…
Reference in a new issue