diff --git a/src/config.rs b/src/config.rs index e56942d..fd8c5a5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,11 @@ use std::path::{Path, PathBuf}; +use failure::{err_msg, Error}; +use walkdir::WalkDir; + +use Hook; +use {HOOKS, PROGRAMS}; + #[derive(Debug, StructOpt)] pub struct Config { /// The root configuration directory for dip. This argument is required. @@ -34,3 +40,72 @@ impl Config { return self; } } + +pub fn load_config

(root: P) +where + P: AsRef, +{ + println!("Reloading config..."); + // hold on to the lock while config is being reloaded + { + let mut programs = PROGRAMS.lock().unwrap(); + // TODO: some kind of smart diff + programs.clear(); + let programs_dir = { + let mut p = root.as_ref().to_path_buf(); + p.push("handlers"); + p + }; + if programs_dir.exists() { + for entry in WalkDir::new(programs_dir) { + let path = match entry.as_ref().map(|e| e.path()) { + Ok(path) => path, + _ => continue, + }; + if !path.is_file() { + continue; + } + match path.file_name() + .and_then(|s| s.to_str()) + .ok_or(err_msg("???")) + .map(|s| { + let filename = s.to_owned(); + programs.insert(filename, path.to_path_buf()) + }) { + _ => (), // don't care + } + } + } + } + { + 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 hook = Hook::from_file(path)?; + let name = hook.get_name(); + hooks.insert(name, hook); + Ok(()) + })(path) + { + Ok(_) => (), + Err(err) => eprintln!("Failed to read config from {:?}: {}", path, err), + } + } + } + } + println!("Done loading config."); +} diff --git a/src/lib.rs b/src/lib.rs index 3f312dc..8088266 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,13 +29,12 @@ use std::sync::{mpsc, Mutex}; use std::thread; use std::time::Duration; -use failure::{err_msg, Error}; +use failure::Error; use hyper::rt::Future; use hyper::service::service_fn; use hyper::Server; use notify::{RecommendedWatcher, RecursiveMode, Watcher}; use regex::Regex; -use walkdir::WalkDir; pub use config::Config; pub use handler::*; @@ -53,75 +52,6 @@ lazy_static! { const NOTFOUND: &str = "

Looks like you took a wrong turn!

There's nothing to see here.

"; -fn load_config

(root: P) -where - P: AsRef, -{ - println!("Reloading config..."); - // hold on to the lock while config is being reloaded - { - let mut programs = PROGRAMS.lock().unwrap(); - // TODO: some kind of smart diff - programs.clear(); - let programs_dir = { - let mut p = root.as_ref().to_path_buf(); - p.push("handlers"); - p - }; - if programs_dir.exists() { - for entry in WalkDir::new(programs_dir) { - let path = match entry.as_ref().map(|e| e.path()) { - Ok(path) => path, - _ => continue, - }; - if !path.is_file() { - continue; - } - match path.file_name() - .and_then(|s| s.to_str()) - .ok_or(err_msg("???")) - .map(|s| { - let filename = s.to_owned(); - programs.insert(filename, path.to_path_buf()) - }) { - _ => (), // don't care - } - } - } - } - { - 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 hook = Hook::from_file(path)?; - let name = hook.get_name(); - hooks.insert(name, hook); - Ok(()) - })(path) - { - Ok(_) => (), - Err(err) => eprintln!("Failed to read config from {:?}: {}", path, err), - } - } - } - } - println!("Done loading config."); -} - fn watch

(root: P) -> notify::Result<()> where P: AsRef, @@ -135,7 +65,7 @@ where Ok(_) => { // for now, naively reload entire config every time // TODO: don't do this - load_config(root.as_ref()) + config::load_config(root.as_ref()) } Err(e) => println!("watch error: {:?}", e), } @@ -144,7 +74,7 @@ where /// Main entry point of the entire application. pub fn run(config: &Config) -> Result<(), Error> { - load_config(&config.root); + config::load_config(&config.root); let v = config.root.clone(); thread::spawn(|| watch(v));