From d239e5dbe1c35553c7663b8079719f327a1ddfaa Mon Sep 17 00:00:00 2001 From: jakka Date: Sun, 15 Jun 2025 14:12:50 +0300 Subject: added final logic and cli flags --- src/main.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 4 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 00a00c7..09bb966 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,83 @@ mod db; mod files; mod flac; -use anyhow::Result; +use anyhow::Error; +use clap::{Arg, ArgAction, command, value_parser}; +use std::path::PathBuf; -#[tokio::main] -async fn main() -> Result<()> { - todo!() +fn main() -> Result<(), Error> { + let matches = command!() + .help_expected(true) + .arg( + Arg::new("path") + .short('p') + .long("path") + .value_parser(value_parser!(PathBuf)) + .help("Path for indexing/reencoding"), + ) + .arg( + Arg::new("index") + .short('i') + .long("index") + .action(ArgAction::SetTrue) + .requires("path") + .help("Only index files"), + ) + .arg( + Arg::new("doit") + .long("doit") + .action(ArgAction::SetTrue) + .conflicts_with("index") + .help("Actually reencode"), + ) + .arg( + Arg::new("clean") + .short('c') + .long("clean") + .action(ArgAction::SetTrue) + .help("Clean and dedupe database"), + ) + .arg( + Arg::new("threads") + .short('t') + .long("threads") + .value_parser(value_parser!(usize)) + .default_value("4") + .help("Set number of reencoding threads (default: 4)"), + ) + .arg( + Arg::new("db") + .long("db") + .value_parser(value_parser!(PathBuf)) + .help("Path to database file"), + ) + .get_matches(); + + let threads = *matches.get_one::("threads").unwrap(); + let runtime = tokio::runtime::Builder::new_multi_thread() + .max_blocking_threads(threads) + .enable_all() + .build()?; + runtime.block_on(async move { + let conn = if let Some(path) = matches.get_one::("db") { + db::Database::new(path).await? + } else { + db::open_default_db().await? + }; + let path = matches.get_one::("path"); + if matches.get_flag("index") { + let folderpath = path.unwrap(); + files::index_files_recursively(folderpath, &conn).await + } else if matches.get_flag("doit") { + files::reencode_files(&conn, path).await + } else if matches.get_flag("clean") { + conn.clean_files().await + } else { + let count = files::count_reencode_files(&conn, path).await.unwrap(); + println!("Files to reencode:\t{count}"); + Ok(()) + } + })?; + + Ok(()) } -- cgit v1.3.1