Simple tool for automatic file management
1use clap::Parser;
2use color_eyre::eyre::Result;
3use futures::prelude::*;
4
5use std::io::Read;
6
7mod actions;
8mod filters;
9mod job;
10mod pattern;
11
12#[derive(Debug, Parser)]
13struct Args {
14 spec: Option<std::path::PathBuf>,
15 /// Run specified commands instead of printing them
16 #[arg(long = "no-dry-run", short = '!')]
17 execute: bool,
18}
19
20impl Args {
21 fn data(&self) -> std::io::Result<Box<dyn Read>> {
22 Ok(match self.spec {
23 Some(ref path) => Box::new(std::fs::File::open(path)?),
24 None => Box::new(std::io::stdin()),
25 })
26 }
27}
28
29#[tokio::main]
30async fn main() -> Result<()> {
31 let args = Args::parse();
32
33 let reader = args.data()?;
34
35 let jobs: Vec<job::Job> = serde_json::from_reader(reader)?;
36
37 stream::iter(&jobs)
38 .then(|job| job.actions())
39 .map(|result| result.unwrap())
40 .flatten()
41 .for_each_concurrent(None, |(action, path)| action.run(path, args.execute))
42 .await;
43
44 Ok(())
45}