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>,
#[arg(short, long, default_value_t = false)]
copy: bool,
#[arg(short, long, default_value_t = false)]
new: bool,
},
Show {
files: Vec<PathBuf>,
@@ -41,7 +43,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.map(str::to_string);
match &args.command {
Commands::Import { files, copy } => {
Commands::Import { files, copy, new } => {
for file in files {
if file.is_dir() {
println!("Importing folder: {:?}", file);
@@ -59,7 +61,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let tag = get_metadata(&path);
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);
}
@@ -73,6 +75,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
extra_files.clone(),
*copy,
false,
*new,
)?;
}
}
+23 -7
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, File};
use std::fs::{copy, create_dir, create_dir_all, remove_dir, rename};
use std::io::Write;
use std::path::PathBuf;
@@ -15,6 +15,7 @@ pub fn move_files(
extra_files: [String; 9],
should_copy: bool,
has_parent: bool,
add_new_playlist: bool,
) -> Result<(), Box<dyn std::error::Error>> {
let target_dir = PathBuf::from("/home/user/Music/Tracks");
@@ -54,9 +55,13 @@ pub fn move_files(
};
if should_copy {
copy(&file_path, target.join(file_name))?;
copy(&file_path, target.join(&file_name))?;
} 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 {
@@ -98,6 +103,16 @@ fn fix_filename(name: &str) -> String {
.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>> {
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");
@@ -106,11 +121,12 @@ fn write_log(file_path: PathBuf) -> Result<(), Box<dyn std::error::Error>> {
if !data_dir.exists() {
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!(
log_file,