Canonicalize watched paths

This commit is contained in:
Matt Green 2016-09-27 09:43:28 -04:00
parent cd8e43baaf
commit c990c9c4d4
3 changed files with 38 additions and 11 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "watchexec"
version = "0.10.0"
version = "0.10.1"
authors = ["Matt Green <mattgreenrocks@gmail.com>"]
[profile.release]

View File

@ -72,7 +72,7 @@ fn wait(rx: &Receiver<Event>, 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();

View File

@ -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<Pattern>
}
#[derive(Debug)]
pub enum NotificationError {
BadPattern(PatternError),
Io(io::Error)
}
impl From<io::Error> for NotificationError {
fn from(err: io::Error) -> NotificationError {
NotificationError::Io(err)
}
}
impl From<PatternError> 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<NotificationFilter, io::Error> {
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);