feat: add verbose log

This commit is contained in:
2026-11-22 01:54:24 +01:00
parent 777e7055f1
commit 11e56e15f0
2 changed files with 36 additions and 17 deletions
+18 -5
View File
@@ -3,7 +3,6 @@ use lofty::file::TaggedFileExt;
use lofty::probe::Probe;
use std::ffi::OsStr;
use std::fs::read_dir;
use std::io;
use std::path::PathBuf;
mod move_files;
@@ -61,16 +60,30 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let tag = get_metadata(&path);
if let Err(e) =
move_files(path.clone(), tag?, &extra_files, *copy, true, *new)
{
if let Err(e) = move_files(
path.clone(),
tag?,
&extra_files,
*copy,
file.to_path_buf(),
true,
*new,
) {
eprintln!("Failed to import {:?}: {}", path, e);
}
}
} else {
println!("Importing file: {:?}", 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,
)?;
}
}
}
+18 -12
View File
@@ -5,7 +5,7 @@ use lofty::tag::Accessor;
use lofty::tag::Tag;
use std::env::var;
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::path::PathBuf;
@@ -14,6 +14,7 @@ pub fn move_files(
tag: Tag,
extra_files: &[String],
should_copy: bool,
folder_path: PathBuf,
has_parent: bool,
add_new_playlist: bool,
) -> Result<(), Box<dyn std::error::Error>> {
@@ -89,7 +90,13 @@ pub fn move_files(
remove_dir(file_path.parent().unwrap())?;
}
write_log(file_path)?;
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(())
}
@@ -112,20 +119,19 @@ fn new_playlist(path: PathBuf) -> std::result::Result<(), std::io::Error> {
writeln!(new_playlist, "{}", path.display())
}
fn write_log(file_path: PathBuf) -> Result<(), Box<dyn std::error::Error>> {
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("music-sort.log");
fn write_log(
file_path: PathBuf,
log_path: PathBuf,
data_dir: PathBuf,
) -> Result<(), Box<dyn std::error::Error>> {
if !data_dir.exists() {
create_dir(&data_dir)?;
}
if !log_path.exists() {
File::create(&log_path)?;
}
let mut log_file = OpenOptions::new()
.append(true)
.create(true)
.open(log_file_path)
.unwrap();
let mut log_file = OpenOptions::new().append(true).open(log_path).unwrap();
if let Err(e) = writeln!(
log_file,