forked from ARK-Builders/ark-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ark-cli): add watch command to monitor directory changes and upd…
…ate index Signed-off-by: Tarek <[email protected]>
- Loading branch information
1 parent
83892a3
commit 66c9362
Showing
6 changed files
with
92 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use std::path::PathBuf; | ||
|
||
use futures::{pin_mut, StreamExt}; | ||
|
||
use fs_index::{watch_index, WatchEvent}; | ||
|
||
use crate::{AppError, DateTime, ResourceId, Utc}; | ||
|
||
#[derive(Clone, Debug, clap::Args)] | ||
#[clap( | ||
name = "watch", | ||
about = "Watch the ark managed folder for changes and update the index accordingly" | ||
)] | ||
pub struct Watch { | ||
#[clap( | ||
help = "Path to the directory to watch for changes", | ||
default_value = ".", | ||
value_parser | ||
)] | ||
path: PathBuf, | ||
} | ||
|
||
impl Watch { | ||
pub async fn run(&self) -> Result<(), AppError> { | ||
let stream = watch_index::<_, ResourceId>(&self.path); | ||
pin_mut!(stream); | ||
|
||
while let Some(value) = stream.next().await { | ||
match value { | ||
WatchEvent::UpdatedOne(update) => { | ||
println!("Index updated with a single file change"); | ||
|
||
let added = update.added(); | ||
let removed = update.removed(); | ||
for file in added { | ||
let time_stamped_path = file.1.iter().next().unwrap(); | ||
let file_path = time_stamped_path.item(); | ||
let last_modified = time_stamped_path.last_modified(); | ||
let last_modified: DateTime<Utc> = last_modified.into(); | ||
println!( | ||
"\tAdded file: {:?} (last modified: {})", | ||
file_path, | ||
last_modified.format("%d/%m/%Y %T") | ||
); | ||
} | ||
for file in removed { | ||
println!("\tRemoved file with hash: {:?}", file); | ||
} | ||
} | ||
WatchEvent::UpdatedAll(update) => { | ||
println!("Index fully updated"); | ||
|
||
let added = update.added(); | ||
let removed = update.removed(); | ||
|
||
for file in added { | ||
let time_stamped_path = file.1.iter().next().unwrap(); | ||
let file_path = time_stamped_path.item(); | ||
let last_modified = time_stamped_path.last_modified(); | ||
println!( | ||
"\tAdded file: {:?} (last modified: {:?})", | ||
file_path, last_modified | ||
); | ||
} | ||
for file in removed { | ||
println!("\tRemoved file with hash: {:?}", file); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters