From 134bfab8cd84f13fefe685bf662ea4c056b599f7 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Sun, 26 Aug 2018 22:33:22 -0500 Subject: [PATCH] don't use unwrap() kids --- src/handler.rs | 22 ++++++++++++++-------- src/service.rs | 11 ++++++++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/handler.rs b/src/handler.rs index d303f08..b2c110c 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -68,6 +68,7 @@ impl Handler { temp_path: PathBuf, input: JsonValue, ) -> impl Future { + let temp_path_cp = temp_path.clone(); let config = { let mut buf: Vec = Vec::new(); { @@ -77,14 +78,19 @@ impl Handler { 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 + Send> = match action { Action::Command(ref cmd) => { // TODO: allow some kind of simple variable replacement - let mut child = Command::new("/bin/bash"); - let child = child - .current_dir(&temp_path) + let mut command = Command::new("/bin/bash"); + command_helper(&mut command); + let child = command .env("DIP_ROOT", "lol") - .env("DIP_WORKDIR", &temp_path) .arg("-c") .arg(cmd) .stdin(Stdio::piped()) @@ -106,10 +112,10 @@ impl Handler { Box::new(result) } Action::Exec(ref path) => { - let mut child = Command::new(&path) - .current_dir(&temp_path) + let mut command = Command::new(&path); + command_helper(&mut command); + let mut child = command .env("DIP_ROOT", "") - .env("DIP_WORKDIR", &temp_path) .arg("--config") .arg(config) .stdin(Stdio::piped()) @@ -145,6 +151,6 @@ impl Handler { Box::new(result) } }; - output.map(|x| (temp_path, x)) + output.map(|x| (temp_path_cp, x)) } } diff --git a/src/service.rs b/src/service.rs index 36fdbc4..7f97b67 100644 --- a/src/service.rs +++ b/src/service.rs @@ -57,7 +57,16 @@ pub fn dip_service(req: Request) -> Box, Erro let temp_path = temp_dir.to_path_buf(); 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) { Ok(msg) => (StatusCode::ACCEPTED, msg), Err(msg) => (StatusCode::BAD_REQUEST, msg),