feat: handle multiple positional file arguments
This commit is contained in:
+12
-5
@@ -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,11 +39,13 @@ 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 } => {
|
||||||
|
for file in files {
|
||||||
if file.is_dir() {
|
if file.is_dir() {
|
||||||
println!("Importing folder: {:?}", file);
|
println!("Importing folder: {:?}", file);
|
||||||
for music_file in read_dir(file)? {
|
for music_file in read_dir(file)? {
|
||||||
let path = music_file?.path();
|
let path = music_file?.path();
|
||||||
|
|
||||||
if path
|
if path
|
||||||
.extension()
|
.extension()
|
||||||
.and_then(|ext| ext.to_str())
|
.and_then(|ext| ext.to_str())
|
||||||
@@ -50,9 +53,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let tag = get_metadata(&path);
|
let tag = get_metadata(&path);
|
||||||
|
|
||||||
if let Err(e) =
|
if let Err(e) =
|
||||||
move_files(path.clone(), tag.clone(), extra_files.clone(), *copy, true)
|
move_files(path.clone(), tag, extra_files.clone(), *copy, true)
|
||||||
{
|
{
|
||||||
eprintln!("Failed to import {:?}: {}", path, e);
|
eprintln!("Failed to import {:?}: {}", path, e);
|
||||||
}
|
}
|
||||||
@@ -60,9 +65,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
} else {
|
} else {
|
||||||
println!("Importing file: {:?}", file);
|
println!("Importing file: {:?}", file);
|
||||||
let tag = get_metadata(file);
|
let tag = get_metadata(file);
|
||||||
move_files(file.clone(), tag.clone(), extra_files, *copy, false)?;
|
move_files(file.clone(), tag.clone(), extra_files.clone(), *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
@@ -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())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user