diff --git a/cli/src/args.rs b/cli/src/args.rs index f3367d5..0fa5e83 100644 --- a/cli/src/args.rs +++ b/cli/src/args.rs @@ -10,13 +10,11 @@ use std::{ use clap::{crate_version, value_t, values_t, App, Arg}; use color_eyre::eyre::{Context, Report, Result}; use log::LevelFilter; -use watchexec::{ - config::{Config, ConfigBuilder}, - run::OnBusyUpdate, - Shell, -}; +use watchexec::{config::ConfigBuilder, run::OnBusyUpdate, Shell}; -pub fn get_args() -> Result<(Config, LevelFilter)> { +use crate::handler::CliHandler; + +pub fn get_args() -> Result { let app = App::new("watchexec") .version(crate_version!()) .about("Execute commands when watched files change") @@ -281,7 +279,7 @@ pub fn get_args() -> Result<(Config, LevelFilter)> { LevelFilter::Warn }; - Ok((config, loglevel)) + CliHandler::new(config, loglevel, false) } // until 2.0 diff --git a/cli/src/handler.rs b/cli/src/handler.rs new file mode 100644 index 0000000..260de6f --- /dev/null +++ b/cli/src/handler.rs @@ -0,0 +1,37 @@ +use color_eyre::eyre; +use log::LevelFilter; +use watchexec::{ + config::Config, + error::Result, + pathop::PathOp, + run::{ExecHandler, Handler}, +}; + +pub struct CliHandler { + pub inner: ExecHandler, + pub log_level: LevelFilter, + pub notify: bool, +} + +impl CliHandler { + pub fn new(config: Config, log_level: LevelFilter, notify: bool) -> eyre::Result { + Ok(Self { + inner: ExecHandler::new(config)?, + log_level, + notify, + }) + } +} + +impl Handler for CliHandler { + fn args(&self) -> Config { + self.inner.args() + } + + fn on_manual(&self) -> Result { + self.inner.on_manual() + } + fn on_update(&self, ops: &[PathOp]) -> Result { + self.inner.on_update(ops) + } +} diff --git a/cli/src/main.rs b/cli/src/main.rs index 833f965..21913be 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,19 +1,20 @@ use std::io::Write; use color_eyre::eyre::Result; -use watchexec::run; +use watchexec::watch; mod args; +mod handler; fn main() -> Result<()> { color_eyre::install()?; - let (args, loglevel) = args::get_args()?; + let handler = args::get_args()?; env_logger::Builder::new() .format(|buf, r| writeln!(buf, "*** {}", r.args())) - .filter(None, loglevel) + .filter(None, handler.log_level) .init(); - run(args)?; + watch(&handler)?; Ok(()) }