feat: Add basic directory walker functionality
Walk a directory recursively finding all *.mp3 and *.flac files, exclue common hidden files and folders.
This commit is contained in:
parent
48ba1f9386
commit
6e4842a71a
2 changed files with 48 additions and 4 deletions
|
|
@ -3,6 +3,6 @@ name = "juno"
|
|||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.5.4", features = ["derive"] }
|
||||
ignore = "0.4.22"
|
||||
|
|
|
|||
48
src/main.rs
48
src/main.rs
|
|
@ -1,3 +1,47 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use ignore::types::TypesBuilder;
|
||||
use ignore::WalkBuilder;
|
||||
use std::{env, io, path::PathBuf};
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
#[arg(help = "Directory to scan for files")]
|
||||
path: Option<PathBuf>,
|
||||
}
|
||||
|
||||
fn walk_dir(path: &PathBuf) -> io::Result<()> {
|
||||
let mut types_builder = TypesBuilder::new();
|
||||
types_builder.add_defaults();
|
||||
|
||||
let accepted_filetypes = ["mp3", "flac"];
|
||||
|
||||
for filetype in accepted_filetypes {
|
||||
let _ = types_builder.add("sound", format!("*.{}", filetype).as_str());
|
||||
}
|
||||
|
||||
types_builder.select("sound");
|
||||
|
||||
let entries = WalkBuilder::new(path)
|
||||
.types(types_builder.build().unwrap())
|
||||
.build()
|
||||
.filter_map(|entry| entry.ok())
|
||||
.filter(|entry| !entry.path().is_dir());
|
||||
|
||||
for result in entries {
|
||||
let path = result.path();
|
||||
|
||||
println!("{}", path.display());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli = Args::parse();
|
||||
let path = cli
|
||||
.path
|
||||
.unwrap_or(env::current_dir().expect("Current directory is not available."));
|
||||
|
||||
walk_dir(&path).expect("error");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue