···
use std::path::{Path, PathBuf};
6
-
use tokio::process::Command;
use tokio_stream::wrappers::ReadDirStream;
9
-
use futures::prelude::*;
use crate::filters::Filter;
11
+
use crate::actions::Action;
/// Definition of the job files
#[derive(Debug, Deserialize)]
18
-
filters: Vec<Box<dyn Filter>>,
16
+
filter: Box<dyn Filter>,
24
-
pub async fn run(&self) -> Result<impl tokio_stream::Stream<Item = (Action, PathBuf)> + '_> {
22
+
/// Returns stream of actions that should be executed
23
+
pub async fn actions(&self) -> Result<impl tokio_stream::Stream<Item = (Action, PathBuf)> + '_> {
let loc = normalise_path(&self.location);
let dir = ReadDirStream::new(fs::read_dir(&loc).await?);
···
Ok(async_stream::stream! {
let entry = entry.unwrap();
32
-
if self.matches_filters(&entry.path()).await {
31
+
if self.filter.matches(&entry.path()).await {
for action in &self.actions {
yield (action.clone(), entry.path())
···
41
-
async fn matches_filters(&self, path: &Path) -> bool {
42
-
stream::iter(&self.filters).all(|f| f.matches(path)).await
46
-
/// Actions available for file
47
-
#[derive(Clone, Debug, Deserialize)]
50
-
/// Run given script with 1st argument. It will be ran in parent directory for given file
51
-
Script { script: Box<Path> },
52
-
/// Move given file to new destination
53
-
Move { move_to: Box<Path> },
54
-
/// Print message and do nothing
55
-
Echo { message: String },
61
-
pub async fn execute(self, source: PathBuf) {
63
-
Action::Script { ref script } => {
64
-
Command::new(script.as_ref())
66
-
.current_dir(source.parent().unwrap())
68
-
.expect("Couldnt spawn process")
71
-
.expect("Child exited abnormally");
75
-
move_to: ref dest_dir,
77
-
let dest = normalise_path(dest_dir).join(source.file_name().unwrap());
78
-
if let Err(err) = fs::rename(&source, &dest).await {
79
-
if err.raw_os_error() == Some(libc::EXDEV) {
82
-
panic!("Cannot move {source:?} -> {dest:?}: {err:?}");
87
-
Action::Echo { ref message } => println!("{source:?} - {message}"),
89
-
Action::Trash => trash::delete(source).unwrap(),
94
-
fn normalise_path(path: &Path) -> PathBuf {
41
+
pub(crate) fn normalise_path(path: &Path) -> PathBuf {
match path.strip_prefix("~") {
Ok(prefix) => std::env::home_dir().unwrap().join(prefix),
Err(_) => path.to_owned(),