Skip to content

Commit

Permalink
Merge pull request #7 from vsbuffalo/update-bug-fix
Browse files Browse the repository at this point in the history
fixed bug with sdf update and added --all option
  • Loading branch information
vsbuffalo committed Oct 2, 2023
2 parents b23a8c9 + 1eca577 commit 470d14e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
3 changes: 2 additions & 1 deletion src/lib/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> {
match self.get_md5(path_context).await? {
Expand Down Expand Up @@ -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 => {
Expand Down
31 changes: 29 additions & 2 deletions src/lib/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>>) -> Result<()> {
let path_context = self.path_context();
self.data.update(filepath, &path_context).await?;
let mut num_updated = 0;

let filepaths: Result<Vec<String>> = match files {
None => Ok(self.data.files.keys().cloned().collect::<Vec<String>>()),
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<String>, link_only: &bool) -> Result<()> {
// (0) get the relative directory path
Expand Down
20 changes: 16 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ enum Commands {
/// Update MD5s
Update {
/// Which file to update (if not set, all tracked files are update).
filename: Option<String>,
#[arg(required = false)]
filenames: Vec<String>,
/// Update all files presently registered in the manifest.
#[arg(long)]
all: bool
},
/// Remove a file from the manifest
Rm {
Expand Down Expand Up @@ -219,7 +223,7 @@ enum Commands {
// multiple optional directories
//directories: Vec<PathBuf>,
},
/// Update the project metadata.
/// Change the project metadata.
Metadata {
/// The project name.
#[arg(long)]
Expand Down Expand Up @@ -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()?;
Expand Down
3 changes: 2 additions & 1 deletion tests/test_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}

Expand Down

0 comments on commit 470d14e

Please sign in to comment.