hawk_cli/
watchers.rs

1//! src/main.rs
2use colored::*;
3use notify::event::{CreateKind, DataChange, ModifyKind, RenameMode};
4use notify::{EventKind, RecommendedWatcher, RecursiveMode, Watcher};
5use std::path::Path;
6
7use crate::log;
8use crate::models::workspace::Workspace;
9use crate::utils;
10
11pub fn watch_config(path: &str) -> notify::Result<()> {
12    let (tx, rx) = std::sync::mpsc::channel();
13    let mut watcher = RecommendedWatcher::new(tx, notify::Config::default())?;
14
15    watcher.watch(Path::new(path).as_ref(), RecursiveMode::Recursive)?;
16
17    for res in rx {
18        match res {
19            Ok(event) => {
20                if let EventKind::Modify(ModifyKind::Data(DataChange::Content)) = event.kind {
21                    log::warn("Changes detected in the config file. Please restart")
22                }
23            }
24            Err(err) => log::error("Something went wrong:", err),
25        }
26    }
27
28    Ok(())
29}
30
31pub fn watch_sync(workspace: Workspace, target: &str) -> notify::Result<()> {
32    println!(
33        "[{}] {} for {}",
34        "WATCH".bold().blue(),
35        workspace.path.bright_yellow().bold(),
36        workspace.name.bold().cyan()
37    );
38
39    let (tx, rx) = std::sync::mpsc::channel();
40    let mut watcher = RecommendedWatcher::new(tx, notify::Config::default())?;
41    let path = Path::new(&workspace.path);
42
43    watcher.watch(path.as_ref(), RecursiveMode::Recursive)?;
44
45    for res in rx {
46        match res {
47            Ok(event) => {
48                if event.paths.first().is_none() {
49                    log::warn("No paths...");
50                    continue;
51                }
52
53                let path = event.paths.first().unwrap();
54
55                if path.is_dir() {
56                    continue;
57                }
58
59                match event.kind {
60                    EventKind::Remove(_) => utils::remove_file(path, target, &workspace.name)?,
61                    EventKind::Create(CreateKind::File)
62                    | EventKind::Modify(ModifyKind::Data(DataChange::Content)) => {
63                        utils::copy_file(path, target, &workspace.name)?
64                    }
65                    EventKind::Modify(ModifyKind::Name(RenameMode::Both)) => {
66                        let mut iter = event.paths.iter();
67                        let from = iter.next().unwrap();
68                        let to = iter.next().unwrap();
69
70                        utils::copy_file(to, target, &workspace.name)?;
71                        utils::remove_file(from, target, &workspace.name)?;
72                    }
73                    EventKind::Modify(ModifyKind::Name(RenameMode::Any)) => {
74                        log::warn("Renaming is not supported yet! Please see https://github.com/notify-rs/notify/issues/261");
75                    }
76                    _ => continue,
77                }
78            }
79            Err(e) => log::error("watch error:", e),
80        }
81    }
82
83    Ok(())
84}