From c990c9c4d45967674bffa3b8737d201fe108382c Mon Sep 17 00:00:00 2001 From: Matt Green Date: Tue, 27 Sep 2016 09:43:28 -0400 Subject: [PATCH] Canonicalize watched paths --- Cargo.toml | 2 +- src/main.rs | 12 +++++++++--- src/notification_filter.rs | 35 ++++++++++++++++++++++++++++------- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ae43f6d..e47d62b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "watchexec" -version = "0.10.0" +version = "0.10.1" authors = ["Matt Green "] [profile.release] diff --git a/src/main.rs b/src/main.rs index 793f039..e3a88b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,7 +72,7 @@ fn wait(rx: &Receiver, filter: &NotificationFilter, verbose: bool) -> Res fn main() { let args = App::new("watchexec") - .version("0.10.0") + .version("0.10.1") .about("Execute commands when watched files change") .arg(Arg::with_name("path") .help("Path to watch") @@ -124,7 +124,7 @@ fn main() { let verbose = args.is_present("verbose"); let cwd = env::current_dir().unwrap(); - let mut filter = NotificationFilter::new(&cwd); + let mut filter = NotificationFilter::new(&cwd).expect("unable to create notification filter"); // Add default ignore list let dotted_dirs = Path::new(".*").join("*"); @@ -156,7 +156,13 @@ fn main() { let paths = args.values_of("path").unwrap(); for path in paths { - watcher.watch(path).expect("unable to watch path"); + match Path::new(path).canonicalize() { + Ok(canonicalized) => watcher.watch(canonicalized).expect("unable to watch path"), + Err(_) => { + println!("invalid path: {}", path); + return; + } + } } let cmd_parts: Vec<&str> = args.values_of("command").unwrap().collect(); diff --git a/src/notification_filter.rs b/src/notification_filter.rs index d8dde9c..87e6745 100644 --- a/src/notification_filter.rs +++ b/src/notification_filter.rs @@ -1,5 +1,6 @@ extern crate glob; +use std::io; use std::path::{Path,PathBuf}; use self::glob::{Pattern,PatternError}; @@ -10,16 +11,36 @@ pub struct NotificationFilter { ignores: Vec } +#[derive(Debug)] +pub enum NotificationError { + BadPattern(PatternError), + Io(io::Error) +} + +impl From for NotificationError { + fn from(err: io::Error) -> NotificationError { + NotificationError::Io(err) + } +} + +impl From for NotificationError { + fn from(err: PatternError) -> NotificationError { + NotificationError::BadPattern(err) + } +} + impl NotificationFilter { - pub fn new(current_dir: &Path) -> NotificationFilter { - NotificationFilter { - cwd: current_dir.to_path_buf(), + pub fn new(current_dir: &Path) -> Result { + let canonicalized = try!(current_dir.canonicalize()); + + Ok(NotificationFilter { + cwd: canonicalized, filters: vec![], ignores: vec![] - } + }) } - pub fn add_extension(&mut self, extension: &str) -> Result<(), PatternError> { + pub fn add_extension(&mut self, extension: &str) -> Result<(), NotificationError> { let mut pattern = String::new(); for ext in extension.split(",") { @@ -37,14 +58,14 @@ impl NotificationFilter { Ok(()) } - pub fn add_filter(&mut self, pattern: &str) -> Result<(), PatternError> { + pub fn add_filter(&mut self, pattern: &str) -> Result<(), NotificationError> { let compiled = try!(self.pattern_for(pattern)); self.filters.push(compiled); Ok(()) } - pub fn add_ignore(&mut self, pattern: &str) -> Result<(), PatternError> { + pub fn add_ignore(&mut self, pattern: &str) -> Result<(), NotificationError> { let compiled = try!(self.pattern_for(pattern)); self.ignores.push(compiled);