2 Commits

Author SHA1 Message Date
KeksNino deeddb525a feat: add cli flag to rescan/update mpd database after import 2026-11-24 18:54:22 +01:00
KeksNino 11e56e15f0 feat: add verbose log 2026-11-22 01:54:24 +01:00
2 changed files with 52 additions and 45 deletions
+30 -29
View File
@@ -3,8 +3,8 @@ use lofty::file::TaggedFileExt;
use lofty::probe::Probe; use lofty::probe::Probe;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fs::read_dir; use std::fs::read_dir;
use std::io;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Command;
mod move_files; mod move_files;
use move_files::move_files; use move_files::move_files;
@@ -27,6 +27,8 @@ enum Commands {
copy: bool, copy: bool,
#[arg(short, long, default_value_t = false)] #[arg(short, long, default_value_t = false)]
new: bool, new: bool,
#[arg(short, long, default_value_t = false)]
update_mpd: bool,
}, },
Show { Show {
files: Vec<PathBuf>, files: Vec<PathBuf>,
@@ -44,7 +46,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.map(str::to_string); .map(str::to_string);
match &args.command { match &args.command {
Commands::Import { files, copy, new } => { Commands::Import {
files,
copy,
new,
update_mpd,
} => {
for file in files { for file in files {
if file.is_dir() { if file.is_dir() {
println!("Importing folder: {:?}", file); println!("Importing folder: {:?}", file);
@@ -57,45 +64,39 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.is_some_and(|ext| extra_files.iter().any(|x| x == ext)) .is_some_and(|ext| extra_files.iter().any(|x| x == ext))
{ {
continue; continue;
} else {
let mut input = String::new();
eprintln!("Found files that aren't music files: {}", path.display());
eprintln!(" [1] Copy all files");
eprintln!(" [2] Skip non-music files");
eprintln!(" [3] Cancel import");
eprint!("> ");
io::stdin()
.read_line(&mut input)
.expect("failed to read input");
let mut other_files: Vec<PathBuf>;
match input.trim() {
"1" => {
other_files.extend(&path);
}
"2" => {}
_ => {
println!("Invalid option.");
}
}
} }
let tag = get_metadata(&path); let tag = get_metadata(&path);
if let Err(e) = if let Err(e) = move_files(
move_files(path.clone(), tag?, &extra_files, *copy, true, *new) path.clone(),
{ tag?,
&extra_files,
*copy,
file.to_path_buf(),
true,
*new,
) {
eprintln!("Failed to import {:?}: {}", path, e); eprintln!("Failed to import {:?}: {}", path, e);
} }
} }
} 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, *new)?; move_files(
file.clone(),
tag?.clone(),
&extra_files,
*copy,
file.to_path_buf(),
false,
*new,
)?;
} }
} }
if *update_mpd {
Command::new("mpc").arg("update").spawn()?;
}
} }
Commands::Show { files } => { Commands::Show { files } => {
+22 -16
View File
@@ -5,7 +5,7 @@ use lofty::tag::Accessor;
use lofty::tag::Tag; use lofty::tag::Tag;
use std::env::var; use std::env::var;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::fs::{copy, create_dir, create_dir_all, remove_dir, rename}; use std::fs::{File, copy, create_dir, create_dir_all, remove_dir, rename};
use std::io::Write; use std::io::Write;
use std::path::PathBuf; use std::path::PathBuf;
@@ -14,6 +14,7 @@ pub fn move_files(
tag: Tag, tag: Tag,
extra_files: &[String], extra_files: &[String],
should_copy: bool, should_copy: bool,
folder_path: PathBuf,
has_parent: bool, has_parent: bool,
add_new_playlist: bool, add_new_playlist: bool,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
@@ -60,10 +61,6 @@ pub fn move_files(
rename(&file_path, target.join(&file_name))?; rename(&file_path, target.join(&file_name))?;
} }
if add_new_playlist {
new_playlist(target.join(file_name))?;
}
for ext in extra_files { for ext in extra_files {
let pattern = file_path.parent().unwrap().join(format!("*.{}", ext)); let pattern = file_path.parent().unwrap().join(format!("*.{}", ext));
@@ -89,7 +86,17 @@ pub fn move_files(
remove_dir(file_path.parent().unwrap())?; remove_dir(file_path.parent().unwrap())?;
} }
write_log(file_path)?; if add_new_playlist {
new_playlist(target.join(file_name))?;
}
let home_dir: PathBuf = PathBuf::from(var("HOME").expect("HOME is not set"));
let data_dir = home_dir.join(".local").join("share").join("music-sort");
let log_file_path = data_dir.join("verbose.log");
write_log(file_path.clone(), log_file_path, data_dir.clone())?;
let log_file_path = data_dir.join("music-sort.log");
write_log(folder_path, log_file_path, data_dir)?;
Ok(()) Ok(())
} }
@@ -112,20 +119,19 @@ fn new_playlist(path: PathBuf) -> std::result::Result<(), std::io::Error> {
writeln!(new_playlist, "{}", path.display()) writeln!(new_playlist, "{}", path.display())
} }
fn write_log(file_path: PathBuf) -> Result<(), Box<dyn std::error::Error>> { fn write_log(
let home_dir: PathBuf = PathBuf::from(var("HOME").expect("HOME is not set")); file_path: PathBuf,
let data_dir = home_dir.join(".local").join("share").join("music-sort"); log_path: PathBuf,
let log_file_path = data_dir.join("music-sort.log"); data_dir: PathBuf,
) -> Result<(), Box<dyn std::error::Error>> {
if !data_dir.exists() { if !data_dir.exists() {
create_dir(&data_dir)?; create_dir(&data_dir)?;
} }
if !log_path.exists() {
File::create(&log_path)?;
}
let mut log_file = OpenOptions::new() let mut log_file = OpenOptions::new().append(true).open(log_path).unwrap();
.append(true)
.create(true)
.open(log_file_path)
.unwrap();
if let Err(e) = writeln!( if let Err(e) = writeln!(
log_file, log_file,