Add empty-dirs verb to find movie directories with no movies.
This commit is contained in:
parent
6a52f3c5b1
commit
334d2a5e53
23
src/lib.rs
23
src/lib.rs
@ -1,5 +1,6 @@
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::env;
|
||||
use std::ffi::OsStr;
|
||||
use std::fmt;
|
||||
@ -678,6 +679,28 @@ impl MovieLibrary {
|
||||
Ok(serde_json::from_reader(r)
|
||||
.context(format!("serde_json::from_reader {}", path.display()))?)
|
||||
}
|
||||
|
||||
pub fn empty_dirs(&self) -> Result<Vec<String>, Error> {
|
||||
let mut all_dirs = HashSet::new();
|
||||
let root_len = self.root.len() + 1; // +1 for trailing slash
|
||||
for de in Path::new(&self.root).read_dir()? {
|
||||
let de = de?;
|
||||
if de.metadata()?.is_dir() {
|
||||
let path = de.path().to_string_lossy().to_string();
|
||||
all_dirs.insert(path[root_len..].to_string());
|
||||
}
|
||||
}
|
||||
for path in self.videos()?.keys() {
|
||||
let dir = match path.find("/") {
|
||||
Some(idx) => path[..idx].to_string(),
|
||||
None => path.to_string(),
|
||||
};
|
||||
all_dirs.remove(&dir);
|
||||
}
|
||||
let mut empty_dirs: Vec<_> = all_dirs.into_iter().collect();
|
||||
empty_dirs.sort();
|
||||
Ok(empty_dirs)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
12
src/main.rs
12
src/main.rs
@ -171,6 +171,8 @@ enum Command {
|
||||
about = "Write full metadata files and update compact file on changes"
|
||||
)]
|
||||
UpdateAndCompactMetadata,
|
||||
#[structopt(about = "Print directories with no movie files in them")]
|
||||
EmptyDirs,
|
||||
}
|
||||
|
||||
#[derive(StructOpt)]
|
||||
@ -248,6 +250,16 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
lib.update_metadata()?;
|
||||
lib.compact_metadata()?;
|
||||
}
|
||||
Command::EmptyDirs => {
|
||||
let lib = MovieLibrary::new(MOVIE_DIR);
|
||||
let dirs = lib.empty_dirs()?;
|
||||
if !dirs.is_empty() {
|
||||
println!("Empty directories:");
|
||||
for d in dirs {
|
||||
println!(" {}", d);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user