From 1eca577fa44549c553923d88d64e1c82dfd0da5a Mon Sep 17 00:00:00 2001 From: Vince Buffalo Date: Mon, 2 Oct 2023 15:48:54 -0700 Subject: [PATCH] fixed bug with sdf update and added --all option --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib/data.rs | 3 ++- src/lib/project.rs | 31 +++++++++++++++++++++++++++++-- src/main.rs | 20 ++++++++++++++++---- tests/test_project.rs | 3 ++- 6 files changed, 51 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d98beb5..7c5621f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2015,7 +2015,7 @@ dependencies = [ [[package]] name = "scidataflow" -version = "0.8.5" +version = "0.8.6" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 6a694ab..9f35f17 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "scidataflow" -version = "0.8.5" +version = "0.8.6" edition = "2021" exclude = ["logo.png", "tests/test_data/**"] license = "MIT" diff --git a/src/lib/data.rs b/src/lib/data.rs index b1ee703..e147070 100644 --- a/src/lib/data.rs +++ b/src/lib/data.rs @@ -388,7 +388,6 @@ impl DataFile { path_context.join(&self.path).exists() } - // Returns true if the file does not exist. pub async fn is_changed(&self, path_context: &Path) -> Result { match self.get_md5(path_context).await? { @@ -571,6 +570,8 @@ impl DataCollection { if let Some(data_file) = self.files.get_mut(file) { data_file.update(path_context).await?; debug!("rehashed file {:?}", data_file.path); + } else { + return Err(anyhow!("File '{}' does not exist.", file)); } } None => { diff --git a/src/lib/project.rs b/src/lib/project.rs index 27afca3..89092b6 100644 --- a/src/lib/project.rs +++ b/src/lib/project.rs @@ -337,12 +337,39 @@ impl Project { self.save() } - pub async fn update(&mut self, filepath: Option<&String>) -> Result<()> { + pub async fn update(&mut self, files: Option<&Vec>) -> Result<()> { let path_context = self.path_context(); - self.data.update(filepath, &path_context).await?; + let mut num_updated = 0; + + let filepaths: Result> = match files { + None => Ok(self.data.files.keys().cloned().collect::>()), + Some(file_list) => { + file_list.iter() + .map(|f| { + Ok(self.relative_path(Path::new(&f))?.to_string_lossy().to_string()) + }) + .collect() + } + }; + + let filepaths = filepaths?; // Use ? here to propagate any errors + + for filepath in filepaths { + match self.data.update(Some(&filepath), &path_context).await { + Ok(_) => { + info!("Updated file '{}'.", filepath); + num_updated += 1; + } + Err(e) => { + return Err(anyhow!("Failed to update file '{}': {}", filepath, e)); + } + } + } + println!("Updated {}.", pluralize(num_updated as u64, "file")); self.save() } + pub async fn link(&mut self, dir: &str, service: &str, key: &str, name: &Option, link_only: &bool) -> Result<()> { // (0) get the relative directory path diff --git a/src/main.rs b/src/main.rs index 6784782..e9b82e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -140,7 +140,11 @@ enum Commands { /// Update MD5s Update { /// Which file to update (if not set, all tracked files are update). - filename: Option, + #[arg(required = false)] + filenames: Vec, + /// Update all files presently registered in the manifest. + #[arg(long)] + all: bool }, /// Remove a file from the manifest Rm { @@ -219,7 +223,7 @@ enum Commands { // multiple optional directories //directories: Vec, }, - /// Update the project metadata. + /// Change the project metadata. Metadata { /// The project name. #[arg(long)] @@ -294,9 +298,17 @@ async fn run() -> Result<()> { let mut proj = Project::new()?; proj.remove(filenames).await } - Some(Commands::Update { filename }) => { + Some(Commands::Update { filenames, all }) => { let mut proj = Project::new()?; - proj.update(filename.as_ref()).await + if !*all && filenames.is_empty() { + return Err(anyhow!("Specify --all or one or more file to update.")); + } + let filepaths = if *all { + None + } else { + Some(filenames) + }; + proj.update(filepaths).await } Some(Commands::Link { dir, service, key, name, link_only }) => { let mut proj = Project::new()?; diff --git a/tests/test_project.rs b/tests/test_project.rs index 6f8e09d..da66535 100644 --- a/tests/test_project.rs +++ b/tests/test_project.rs @@ -100,7 +100,8 @@ mod tests { let re_add_files = vec![file_to_check.to_string_lossy().to_string()]; for file in &re_add_files { - let result = fixture.project.update(Some(&file)).await; + let files = vec![file.clone()]; + let result = fixture.project.update(Some(&files)).await; assert!(result.is_ok(), "re-adding raised Error!"); }