feat: handle multiple positional file arguments

This commit is contained in:
2026-09-04 19:07:03 +02:00
parent 5fd47b445e
commit e1e56f7e27
2 changed files with 33 additions and 26 deletions
+30 -23
View File
@@ -21,7 +21,8 @@ struct Args {
#[derive(Subcommand)] #[derive(Subcommand)]
enum Commands { enum Commands {
Import { Import {
file: PathBuf, #[arg(num_args = 1..)]
files: Vec<PathBuf>,
#[arg(short, long, default_value_t = false)] #[arg(short, long, default_value_t = false)]
copy: bool, copy: bool,
}, },
@@ -29,7 +30,7 @@ enum Commands {
file: PathBuf, file: PathBuf,
}, },
Edit { Edit {
file: PathBuf, files: PathBuf,
}, },
} }
@@ -38,31 +39,37 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let extra_files = ["jpg", "png", "pdf", "log", "cue", "mp4", "lrc", "txt"].map(str::to_string); let extra_files = ["jpg", "png", "pdf", "log", "cue", "mp4", "lrc", "txt"].map(str::to_string);
match &args.command { match &args.command {
Commands::Import { file, copy } => { Commands::Import { files, copy } => {
if file.is_dir() { for file in files {
println!("Importing folder: {:?}", file); if file.is_dir() {
for music_file in read_dir(file)? { println!("Importing folder: {:?}", file);
let path = music_file?.path(); for music_file in read_dir(file)? {
if path let path = music_file?.path();
.extension()
.and_then(|ext| ext.to_str()) if path
.is_some_and(|ext| extra_files.iter().any(|x| x == ext)) .extension()
{ .and_then(|ext| ext.to_str())
continue; .is_some_and(|ext| extra_files.iter().any(|x| x == ext))
} {
let tag = get_metadata(&path); continue;
if let Err(e) = }
move_files(path.clone(), tag.clone(), extra_files.clone(), *copy, true)
{ let tag = get_metadata(&path);
eprintln!("Failed to import {:?}: {}", path, e);
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 } => { Commands::Show { file } => {
println!("Show file: {:?}", file); println!("Show file: {:?}", file);
let tag = get_metadata(file); let tag = get_metadata(file);
+3 -3
View File
@@ -12,7 +12,7 @@ pub fn move_files(
should_copy: bool, should_copy: bool,
has_parent: bool, has_parent: bool,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
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 artist = tag.artist();
let album = tag.album(); let album = tag.album();
@@ -67,8 +67,8 @@ pub fn move_files(
} }
} }
let is_empty = file_path.parent().unwrap().read_dir()?.next().is_none(); let is_empty = has_parent && file_path.parent().unwrap().read_dir()?.next().is_none();
if has_parent && is_empty { if is_empty {
remove_dir(file_path.parent().unwrap())?; remove_dir(file_path.parent().unwrap())?;
} }