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