Implement cli's own wrapper handler

That will let us hook in watchexec-cli specific stuff
This commit is contained in:
Félix Saparelli 2021-07-31 04:24:05 +12:00
parent 3208652ed8
commit a3173194a1
No known key found for this signature in database
GPG Key ID: B948C4BAE44FC474
3 changed files with 47 additions and 11 deletions

View File

@ -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<CliHandler> {
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

37
cli/src/handler.rs Normal file
View File

@ -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<Self> {
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<bool> {
self.inner.on_manual()
}
fn on_update(&self, ops: &[PathOp]) -> Result<bool> {
self.inner.on_update(ops)
}
}

View File

@ -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(())
}