TODO: this should return a Result >, Error>
(&mut self, command: &str)
| 51 | |
| 52 | // TODO: this should return a Result<Option<Vec<u8>>, Error> |
| 53 | pub fn parse(&mut self, command: &str) -> Option<Vec<u8>> { |
| 54 | if command == EXIT_HANDLER_TOKEN { |
| 55 | return Some(command.as_bytes().to_owned()); |
| 56 | } |
| 57 | |
| 58 | if self.compiled.is_none() { |
| 59 | self.compiled = Some( |
| 60 | Regex::new(&self.parser) |
| 61 | .unwrap_or_else(|_| panic!("could not compile '{}'", &self.parser)), |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | // check command for matches |
| 66 | if let Some(captures) = self.compiled.as_ref().unwrap().captures(command) { |
| 67 | let handler = self.handle_with_captures(&captures); |
| 68 | |
| 69 | // check cache first |
| 70 | if let Some(out) = self.cache.get(&handler) { |
| 71 | debug!("'{}' from cache: {}", &handler, out.len()); |
| 72 | return Some(out.to_owned()); |
| 73 | } |
| 74 | |
| 75 | // docker exec? |
| 76 | if let Some(exec) = DOCKER_HANDLER_PARSER.captures(&handler) { |
| 77 | let (container_id, command) = (&exec[1], &exec[2]); |
| 78 | match docker::exec(container_id, command) { |
| 79 | Ok(data) => { |
| 80 | debug!("docker_exec('{}') -> {:?}", command, data); |
| 81 | self.cache.insert(handler, data.clone()); |
| 82 | return Some(data); |
| 83 | } |
| 84 | Err(e) => { |
| 85 | error!( |
| 86 | "error running '{}' inside container '{}': {}", |
| 87 | command, container_id, e |
| 88 | ); |
| 89 | return Some("".as_bytes().to_owned()); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return Some(handler.as_bytes().to_owned()); |
| 95 | } |
| 96 | // this is not the handler you're looking for ... |
| 97 | None |
| 98 | } |
| 99 | } |