This commit is contained in:
Michael Zhang 2018-08-15 06:41:09 -07:00
parent fef04344ce
commit ea731f78bf
No known key found for this signature in database
GPG key ID: A1B65B603268116B
2 changed files with 48 additions and 37 deletions

View file

@ -1,6 +1,12 @@
use std::collections::HashMap;
use std::path::Path;
use std::fs::File;
use std::io::Read;
use failure::{err_msg, Error};
use hyper::{Body, Request, Response};
use toml::Value;
use walkdir::WalkDir;
pub struct Hook {
handler_type: String,
@ -20,3 +26,44 @@ impl Hook {
Ok(Response::new(Body::from("lol")))
}
}
pub fn load_from_config<P>(path: P, hooks: &mut HashMap<String, Hook>) where P: AsRef<Path> {
let hooks_dir = {
let mut p = path.as_ref().to_path_buf();
p.push("hooks");
p
};
if hooks_dir.exists() {
for entry in WalkDir::new(hooks_dir) {
let path = match entry.as_ref().map(|e| e.path()) {
Ok(path) => path,
_ => continue,
};
if !path.is_file() {
continue;
}
match (|path: &Path| -> Result<(), Error> {
let filename = path
.file_name()
.ok_or(err_msg("what the fuck bro"))?
.to_str()
.ok_or(err_msg("???"))?
.to_owned();
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let config = contents.parse::<Value>()?;
let hook = Hook::from(&config)?;
println!("Added hook '{}'", filename);
hooks.insert(filename, hook);
Ok(())
})(path)
{
Ok(_) => (),
Err(err) => eprintln!("Failed to read config from {:?}: {}", path, err),
}
}
}
}

View file

@ -114,43 +114,7 @@ where
{
let mut hooks = HOOKS.lock().unwrap();
hooks.clear();
let hooks_dir = {
let mut p = root.as_ref().to_path_buf();
p.push("hooks");
p
};
if hooks_dir.exists() {
for entry in WalkDir::new(hooks_dir) {
let path = match entry.as_ref().map(|e| e.path()) {
Ok(path) => path,
_ => continue,
};
if !path.is_file() {
continue;
}
match (|path: &Path| -> Result<(), Error> {
let filename = path
.file_name()
.ok_or(err_msg("what the fuck bro"))?
.to_str()
.ok_or(err_msg("???"))?
.to_owned();
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let config = contents.parse::<Value>()?;
let hook = Hook::from(&config)?;
println!("Added hook '{}'", filename);
hooks.insert(filename, hook);
Ok(())
})(path)
{
Ok(_) => (),
Err(err) => eprintln!("Failed to read config from {:?}: {}", path, err),
}
}
}
hook::load_from_config(root, &mut hooks);
}
}