From d2eb982403f74d41fbde0072ac43d1313b3e7a24 Mon Sep 17 00:00:00 2001 From: Craig Drummond Date: Mon, 21 Feb 2022 12:04:46 +0000 Subject: [PATCH] Simplify commandline syntax, make task optional argument --- src/main.rs | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 10c1670..71ed65f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,11 +23,11 @@ fn main() { let mut db_path = "bliss.db".to_string(); let mut logging = "info".to_string(); let mut music_path = ".".to_string(); - let mut ignore_file = String::new(); + let mut ignore_file = "ignore.txt".to_string(); let mut keep_old:bool = false; let mut dry_run:bool = false; - let mut tags_only:bool = false; - let mut upload = "".to_string(); + let mut task = "analyse".to_string(); + let mut lms_host = "127.0.0.1".to_string(); match dirs::home_dir() { Some(path) => { music_path = String::from(path.join("Music").to_string_lossy()); } @@ -37,6 +37,10 @@ fn main() { { let music_path_help = format!("Music folder (default: {})", &music_path); let db_path_help = format!("Database location (default: {})", &db_path); + let logging_help = format!("Log level; trace, debug, info, warn, error. (default: {})", logging); + let ignore_file_help = format!("File containg items to mark as ignored. (default: {})", ignore_file); + let task_help = format!("Task to perform; analyse, tags, ignore, upload. (default: {})", task); + let lms_host_help = format!("LMS hostname or IP address (default: {})", &lms_host); let description = format!("Bliss Analyser v{}", VERSION); // arg_parse.refer 'borrows' db_path, etc, and can only have one @@ -45,12 +49,12 @@ fn main() { arg_parse.set_description(&description); arg_parse.refer(&mut music_path).add_option(&["-m", "--music"], Store, &music_path_help); arg_parse.refer(&mut db_path).add_option(&["-d", "--db"], Store, &db_path_help); - arg_parse.refer(&mut logging).add_option(&["-l", "--logging"], Store, "Log level (trace, debug, info, warn, error)"); - arg_parse.refer(&mut keep_old).add_option(&["-k", "--keep-old"], StoreTrue, "Don't remove tracks from DB if they don't exist"); - arg_parse.refer(&mut dry_run).add_option(&["-r", "--dry-run"], StoreTrue, "Dry run, only show what needs to be done"); - arg_parse.refer(&mut tags_only).add_option(&["-t", "--tags-only"], StoreTrue, "Re-read tags"); - arg_parse.refer(&mut ignore_file).add_option(&["-i", "--ignore"], Store, "Update ignore status in DB"); - arg_parse.refer(&mut upload).add_option(&["-u", "--upload"], Store, "Upload database to LMS (specify hostname or IP address)"); + arg_parse.refer(&mut logging).add_option(&["-l", "--logging"], Store, &logging_help); + arg_parse.refer(&mut keep_old).add_option(&["-k", "--keep-old"], StoreTrue, "Don't remove tracks from DB if they don't exist (used with analyse task)"); + arg_parse.refer(&mut dry_run).add_option(&["-r", "--dry-run"], StoreTrue, "Dry run, only show what needs to be done (used with analyse task)"); + arg_parse.refer(&mut ignore_file).add_option(&["-i", "--ignore"], Store, &ignore_file_help); + arg_parse.refer(&mut lms_host).add_option(&["-L", "--lms"], Store, &lms_host_help); + arg_parse.refer(&mut task).add_argument("task", Store, &task_help); arg_parse.parse_args_or_exit(); } @@ -62,6 +66,11 @@ fn main() { builder.format(|buf, record| writeln!(buf, "[{} {:.1}] {}", Local::now().format("%Y-%m-%d %H:%M:%S"), record.level(), record.args())); builder.init(); + if !task.eq_ignore_ascii_case("analyse") && !task.eq_ignore_ascii_case("tags") && !task.eq_ignore_ascii_case("ignore") && !task.eq_ignore_ascii_case("upload") { + log::error!("Invalid task ({}) supplied", task); + process::exit(-1); + } + if db_path.len() < 3 { log::error!("Invalid DB path ({}) supplied", db_path); process::exit(-1); @@ -73,9 +82,9 @@ fn main() { process::exit(-1); } - if !upload.is_empty() { + if task.eq_ignore_ascii_case("upload") { if path.exists() { - upload::upload_db(&db_path, &upload); + upload::upload_db(&db_path, &lms_host); } else { log::error!("DB ({}) does not exist", db_path); process::exit(-1); @@ -91,9 +100,9 @@ fn main() { process::exit(-1); } - if tags_only { + if task.eq_ignore_ascii_case("tags") { analyse::read_tags(&db_path, &mpath); - } else if !ignore_file.is_empty() { + } else if task.eq_ignore_ascii_case("ignore") { let ignore_path = PathBuf::from(&ignore_file); if !ignore_path.exists() { log::error!("Ignore file ({}) does not exist", ignore_file);