-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
165 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
use crate::config::{Command, Config}; | ||
use crate::util::transtion_to_string; | ||
use fwatch::{BasicTarget, Transition, Watcher}; | ||
use log::{debug, error, info}; | ||
use std::ffi::OsStr; | ||
use std::str; | ||
use std::{process::Stdio, time::Duration}; | ||
use tokio::{process, time}; | ||
|
||
pub async fn watch(cfg: &Config, filter: Option<Vec<String>>) { | ||
let sleep_interval = Duration::from_millis(cfg.check_interval_ms.unwrap_or(1000)); | ||
|
||
let mut watcher: Watcher<BasicTarget> = Watcher::new(); | ||
|
||
let actions: Vec<(_, _)> = cfg | ||
.actions | ||
.iter() | ||
.filter(|(name, _)| filter.is_none() || filter.as_ref().unwrap().contains(name)) | ||
.collect(); | ||
|
||
if actions.is_empty() { | ||
error!("No actions to watch matching the passed action names"); | ||
return; | ||
} | ||
|
||
actions | ||
.iter() | ||
.flat_map(|(_, action)| &action.targets) | ||
.for_each(|target| watcher.add_target(BasicTarget::new(target.path()))); | ||
|
||
info!("Watching targets ..."); | ||
loop { | ||
for (index, transition) in watcher | ||
.watch() | ||
.into_iter() | ||
.enumerate() | ||
.filter(|(_, transition)| !matches!(transition, Transition::None)) | ||
{ | ||
let Some(path) = watcher.get_path(index) else { | ||
error!("could not get path for index {index}"); | ||
continue; | ||
}; | ||
|
||
for (_, action) in &actions { | ||
for target in &action.targets { | ||
if target.path() != path.to_string_lossy() | ||
|| !target.matches_transition(transition) | ||
{ | ||
debug!("Change not tracked: {:?} -> {:?}", &path, &transition); | ||
continue; | ||
} | ||
|
||
info!("Change detected: {:?} -> {:?}", &path, &transition); | ||
|
||
let cmds = action.commands.clone(); | ||
let envmap = [ | ||
("FW_PATH", format!("{}", path.clone().to_string_lossy())), | ||
( | ||
"FW_TRANSITION", | ||
transtion_to_string(&transition).to_string(), | ||
), | ||
]; | ||
tokio::spawn(async move { | ||
execute_commands(&cmds, envmap).await; | ||
}); | ||
} | ||
} | ||
} | ||
|
||
time::sleep(sleep_interval).await; | ||
} | ||
} | ||
|
||
async fn execute_commands<E, K, V>(cmds: &[Command], env: E) | ||
where | ||
E: IntoIterator<Item = (K, V)> + Clone, | ||
K: AsRef<OsStr>, | ||
V: AsRef<OsStr>, | ||
{ | ||
for cmd in cmds { | ||
let args = cmd.split_command(); | ||
if args.is_empty() { | ||
continue; | ||
} | ||
|
||
let ex = args[0]; | ||
let args = &args[1..]; | ||
|
||
let mut exec = process::Command::new(ex); | ||
exec.args(args).current_dir(cmd.cwd()).envs(env.clone()); | ||
|
||
if cmd.is_async() { | ||
match exec.spawn() { | ||
Err(err) => { | ||
error!("Command execution failed ({}): {}", cmd.command(), err); | ||
return; | ||
} | ||
Ok(v) => { | ||
info!("Command spawend ({}): ID: {:?}", cmd.command(), v.id()); | ||
} | ||
} | ||
} else { | ||
match exec | ||
.stdin(Stdio::null()) | ||
.stdout(Stdio::inherit()) | ||
.output() | ||
.await | ||
{ | ||
Err(err) => { | ||
error!("Command execution failed ({}): {}", cmd.command(), err); | ||
return; | ||
} | ||
Ok(out) => info!( | ||
"Command executed ({}): {}", | ||
cmd.command(), | ||
str::from_utf8(out.stdout.as_slice()).unwrap_or_default() | ||
), | ||
} | ||
} | ||
} | ||
} |