diff options
| author | jakka <jakkadoujin@gmail.com> | 2025-06-15 14:12:50 +0300 |
|---|---|---|
| committer | jakka <jakkadoujin@gmail.com> | 2025-06-15 14:12:50 +0300 |
| commit | d239e5dbe1c35553c7663b8079719f327a1ddfaa (patch) | |
| tree | d4e766b6c0e38c745baf06d30c60a122c8dd1b72 /src/main.rs | |
| parent | 11d428265b3f7f5541779ef2df24bfb17d7250aa (diff) | |
added final logic and cli flags
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 82 |
1 files changed, 78 insertions, 4 deletions
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::<usize>("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::<PathBuf>("db") { + db::Database::new(path).await? + } else { + db::open_default_db().await? + }; + let path = matches.get_one::<PathBuf>("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(()) } |
