Skip to content

Commit

Permalink
feat: restart watcher upon config changes
Browse files Browse the repository at this point in the history
relates to #106
  • Loading branch information
cristianoliveira committed Jul 6, 2024
1 parent e91cb70 commit a620511
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ mod yaml;

use cli::*;
use nix::{
libc::signal,
sys::signal::{self, Signal},
unistd::Pid,
};
Expand Down Expand Up @@ -172,8 +171,8 @@ fn main() {
pub fn execute_watch_command(watches: Watches, args: Args) {
let config_file_paths = if args.flag_config.is_empty() {
vec![
cli::watch::DEFAULT_FILENAME.replace("yaml", "yml"),
cli::watch::DEFAULT_FILENAME.to_string(),
format!("**/{}", cli::watch::DEFAULT_FILENAME),
format!("**/{}", cli::watch::DEFAULT_FILENAME.replace("yaml", "yml"),),
]
} else {
vec![format!("{}", &args.flag_config)]
Expand All @@ -188,7 +187,6 @@ pub fn execute_watch_command(watches: Watches, args: Args) {
stdout::warn(
&vec![
"The config file has changed while an instance was running.",
"Terminating the current watcher instance...",
&format!("Config file: {}", file_changed),
]
.join("\n"),
Expand All @@ -197,7 +195,14 @@ pub fn execute_watch_command(watches: Watches, args: Args) {
println!("Watcher PID: {}", watcher_pid);

match signal::kill(Pid::from_raw(watcher_pid as i32), Signal::SIGINT) {
Ok(_) => stdout::info("Terminating watcher..."),
Ok(_) => {
stdout::info("Restarting funzzy...");
let _child = std::process::Command::new("fzz")
.arg("-c")
.arg(file_changed)
.args(std::env::args().skip(1))
.spawn();
}
Err(err) => panic!("Failed to terminate watcher forcefully.\nCause: {:?}", err),
}
},
Expand Down
2 changes: 1 addition & 1 deletion tests/watcher_reloads_config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn it_terminates_the_current_running_watcher_when_config_changes() {
.expect("failed to read from file");

output.contains("The config file has changed")
&& output.contains("Terminating watcher...")
&& output.contains("Restarting funzzy...")
},
"No task in the example was configured with run_on_init {}",
output
Expand Down

0 comments on commit a620511

Please sign in to comment.