feat: add new -n flag to write imported file path to m3u file

This commit is contained in:
2026-10-28 16:42:44 +01:00
parent 5e3bc38a11
commit b4941442cd
2 changed files with 28 additions and 9 deletions
+5 -2
View File
@@ -24,6 +24,8 @@ enum Commands {
files: Vec<PathBuf>, files: Vec<PathBuf>,
#[arg(short, long, default_value_t = false)] #[arg(short, long, default_value_t = false)]
copy: bool, copy: bool,
#[arg(short, long, default_value_t = false)]
new: bool,
}, },
Show { Show {
files: Vec<PathBuf>, files: Vec<PathBuf>,
@@ -41,7 +43,7 @@ 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 } => { Commands::Import { files, copy, new } => {
for file in files { for file in files {
if file.is_dir() { if file.is_dir() {
println!("Importing folder: {:?}", file); println!("Importing folder: {:?}", file);
@@ -59,7 +61,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let tag = get_metadata(&path); let tag = get_metadata(&path);
if let Err(e) = if let Err(e) =
move_files(path.clone(), tag?, extra_files.clone(), *copy, true) move_files(path.clone(), tag?, extra_files.clone(), *copy, true, *new)
{ {
eprintln!("Failed to import {:?}: {}", path, e); eprintln!("Failed to import {:?}: {}", path, e);
} }
@@ -73,6 +75,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
extra_files.clone(), extra_files.clone(),
*copy, *copy,
false, false,
*new,
)?; )?;
} }
} }
+23 -7
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, File}; use std::fs::{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;
@@ -15,6 +15,7 @@ pub fn move_files(
extra_files: [String; 9], extra_files: [String; 9],
should_copy: bool, should_copy: bool,
has_parent: bool, has_parent: bool,
add_new_playlist: bool,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
let target_dir = PathBuf::from("/home/user/Music/Tracks"); let target_dir = PathBuf::from("/home/user/Music/Tracks");
@@ -54,9 +55,13 @@ pub fn move_files(
}; };
if should_copy { if should_copy {
copy(&file_path, target.join(file_name))?; copy(&file_path, target.join(&file_name))?;
} else { } else {
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 {
@@ -98,6 +103,16 @@ fn fix_filename(name: &str) -> String {
.collect() .collect()
} }
fn new_playlist(path: PathBuf) -> std::result::Result<(), std::io::Error> {
let new_playlist_path = PathBuf::from("/home/user/Music/Playlists/active - new.m3u");
let mut new_playlist = OpenOptions::new()
.append(true)
.open(new_playlist_path)
.unwrap();
println!("{:?}", path);
writeln!(new_playlist, "{}", path.display())
}
fn write_log(file_path: PathBuf) -> Result<(), Box<dyn std::error::Error>> { 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 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 data_dir = home_dir.join(".local").join("share").join("music-sort");
@@ -106,11 +121,12 @@ fn write_log(file_path: 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_file_path.exists() {
File::create(&log_file_path)?;
}
let mut log_file = OpenOptions::new().append(true).open(log_file_path).unwrap(); let mut log_file = OpenOptions::new()
.append(true)
.create(true)
.open(log_file_path)
.unwrap();
if let Err(e) = writeln!( if let Err(e) = writeln!(
log_file, log_file,