Skip to content

Commit

Permalink
feat(ark-cli): add watch command to monitor directory changes and upd…
Browse files Browse the repository at this point in the history
…ate index

Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser committed Nov 20, 2024
1 parent 83892a3 commit 66c9362
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 5 deletions.
3 changes: 2 additions & 1 deletion ark-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ serde_json = "1.0.82"
chrono = "0.4.34"
anyhow = "1.0.80"
thiserror = "1.0.57"
futures = "0.3"

# REGISTRAR
log = { version = "0.4.17", features = ["release_max_level_off"] }
lazy_static = "1.4.0"
canonical-path = "2.0.2"


fs-index = { path = "../fs-index" }
fs-index = { path = "../fs-index", features = ["watch"] }
fs-atomic-versions = { path = "../fs-atomic-versions" }
fs-metadata = { path = "../fs-metadata" }
fs-properties = { path = "../fs-properties" }
Expand Down
10 changes: 10 additions & 0 deletions ark-cli/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ $ /tmp/ark-cli list -t --filter=search
22-207093268 search,engine
```

### Watch a Directory for Changes

You can watch a directory for changes and automatically update the index by running the following command:

```sh
ark-cli watch [PATH]
```

If you don't provide a path, the current directory (`.`) will be used by default. This command continuously monitors the specified directory for file changes (create, modify, or remove) and updates the index accordingly. It's useful for keeping your index in sync with the latest changes in the folder.

## :zap: Low-level utilities :zap:

There are commands which could be useful with time, when you grasp the basic concepts. Some of these commands also can be useful for debugging [ArkLib](https://github.com/ARK-Builders/ark-rust).
Expand Down
2 changes: 2 additions & 0 deletions ark-cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod list;
mod monitor;
mod render;
pub mod storage;
mod watch;

pub use file::{file_append, file_insert, format_file, format_line};

Expand All @@ -18,6 +19,7 @@ pub enum Commands {
Monitor(monitor::Monitor),
Render(render::Render),
List(list::List),
Watch(watch::Watch),
#[command(about = "Manage links")]
Link {
#[clap(subcommand)]
Expand Down
74 changes: 74 additions & 0 deletions ark-cli/src/commands/watch.rs
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(())
}
}
1 change: 1 addition & 0 deletions ark-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ async fn run() -> Result<()> {
Monitor(monitor) => monitor.run()?,
Render(render) => render.run()?,
List(list) => list.run()?,
Watch(watch) => watch.run().await?,
Link { subcommand } => match subcommand {
Create(create) => create.run().await?,
Load(load) => load.run()?,
Expand Down
7 changes: 3 additions & 4 deletions ark-cli/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,9 @@ pub fn translate_storage(
root: &Option<PathBuf>,
storage: &str,
) -> Option<(PathBuf, Option<StorageType>)> {
if let Ok(path) = PathBuf::from_str(storage) {
if path.exists() && path.is_dir() {
return Some((path, None));
}
let Ok(path) = PathBuf::from_str(storage);
if path.exists() && path.is_dir() {
return Some((path, None));
}

match storage.to_lowercase().as_str() {
Expand Down

0 comments on commit 66c9362

Please sign in to comment.