watchexec/cli/src/main.rs

62 lines
1.4 KiB
Rust
Raw Normal View History

2021-10-09 07:45:32 +02:00
use std::{env::var};
use miette::{IntoDiagnostic, Result};
2021-10-09 07:45:32 +02:00
use watchexec::{Watchexec, event::Event, filter::tagged::{Filter, Matcher, Op, TaggedFilterer}};
2021-05-10 12:44:35 +02:00
mod args;
mod config;
2016-09-14 15:30:59 +02:00
#[tokio::main]
async fn main() -> Result<()> {
2021-09-28 14:47:18 +02:00
#[cfg(feature = "dev-console")]
console_subscriber::init();
if var("RUST_LOG").is_ok() && cfg!(not(feature = "dev-console")) {
tracing_subscriber::fmt::init();
}
2021-05-10 12:44:35 +02:00
let args = args::get_args()?;
if args.is_present("verbose") {
tracing_subscriber::fmt()
.with_env_filter(match args.occurrences_of("verbose") {
0 => unreachable!(),
1 => "watchexec=debug",
2 => "watchexec=trace",
_ => "trace",
})
.try_init()
.ok();
}
let mut filters = Vec::new();
2021-09-28 11:24:06 +02:00
2021-10-09 07:45:32 +02:00
// TODO: move into config?
2021-09-28 11:24:06 +02:00
for filter in args.values_of("filter").unwrap_or_default() {
filters.push(filter.parse()?);
2021-09-28 11:24:06 +02:00
}
2021-10-09 07:45:32 +02:00
for ext in args.values_of("extensions").unwrap_or_default().map(|s| s.split(',').map(|s| s.trim())).flatten() {
filters.push(Filter {
2021-10-09 07:45:32 +02:00
in_path: None,
on: Matcher::Path,
op: Op::Glob,
pat: TaggedFilterer::glob(&format!("**/*.{}", ext))?,
negate: false,
});
2021-10-09 07:45:32 +02:00
}
let (init, runtime, filterer) = config::new(&args)?;
filterer.add_filters(&filters).await?;
let wx = Watchexec::new(init, runtime)?;
if !args.is_present("postpone") {
wx.send_event(Event::default()).await?;
}
wx.main().await.into_diagnostic()??;
Ok(())
}