From e1e56f7e272aefda86f8363cb0072251b7f12412 Mon Sep 17 00:00:00 2001 From: KeksNino Date: Fri, 4 Sep 2026 19:07:03 +0200 Subject: [PATCH] feat: handle multiple positional file arguments --- src/main.rs | 53 +++++++++++++++++++++++++++-------------------- src/move_files.rs | 6 +++--- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1b9f0bb..b556559 100755 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,8 @@ struct Args { #[derive(Subcommand)] enum Commands { Import { - file: PathBuf, + #[arg(num_args = 1..)] + files: Vec, #[arg(short, long, default_value_t = false)] copy: bool, }, @@ -29,7 +30,7 @@ enum Commands { file: PathBuf, }, Edit { - file: PathBuf, + files: PathBuf, }, } @@ -38,31 +39,37 @@ fn main() -> Result<(), Box> { let extra_files = ["jpg", "png", "pdf", "log", "cue", "mp4", "lrc", "txt"].map(str::to_string); match &args.command { - Commands::Import { file, copy } => { - if file.is_dir() { - println!("Importing folder: {:?}", file); - for music_file in read_dir(file)? { - let path = music_file?.path(); - if path - .extension() - .and_then(|ext| ext.to_str()) - .is_some_and(|ext| extra_files.iter().any(|x| x == ext)) - { - continue; - } - let tag = get_metadata(&path); - if let Err(e) = - move_files(path.clone(), tag.clone(), extra_files.clone(), *copy, true) - { - eprintln!("Failed to import {:?}: {}", path, e); + Commands::Import { files, copy } => { + for file in files { + if file.is_dir() { + println!("Importing folder: {:?}", file); + for music_file in read_dir(file)? { + let path = music_file?.path(); + + if path + .extension() + .and_then(|ext| ext.to_str()) + .is_some_and(|ext| extra_files.iter().any(|x| x == ext)) + { + continue; + } + + let tag = get_metadata(&path); + + if let Err(e) = + move_files(path.clone(), tag, extra_files.clone(), *copy, true) + { + eprintln!("Failed to import {:?}: {}", path, e); + } } + } else { + println!("Importing file: {:?}", file); + let tag = get_metadata(file); + move_files(file.clone(), tag.clone(), extra_files.clone(), *copy, false)?; } - } else { - println!("Importing file: {:?}", file); - let tag = get_metadata(file); - move_files(file.clone(), tag.clone(), extra_files, *copy, false)?; } } + Commands::Show { file } => { println!("Show file: {:?}", file); let tag = get_metadata(file); diff --git a/src/move_files.rs b/src/move_files.rs index afa82ab..88ad34e 100644 --- a/src/move_files.rs +++ b/src/move_files.rs @@ -12,7 +12,7 @@ pub fn move_files( should_copy: bool, has_parent: bool, ) -> Result<(), Box> { - let target_dir = PathBuf::from("/home/user/Desktop/coding/keks-sort"); + let target_dir = PathBuf::from("/home/user/Music/Tracks"); let artist = tag.artist(); let album = tag.album(); @@ -67,8 +67,8 @@ pub fn move_files( } } - let is_empty = file_path.parent().unwrap().read_dir()?.next().is_none(); - if has_parent && is_empty { + let is_empty = has_parent && file_path.parent().unwrap().read_dir()?.next().is_none(); + if is_empty { remove_dir(file_path.parent().unwrap())?; }