From d3b60956a965f422283fe186ba894bf90a6fc4b7 Mon Sep 17 00:00:00 2001 From: Craig Drummond Date: Mon, 21 Feb 2022 12:05:25 +0000 Subject: [PATCH] Support reading config from config.ini --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 1ac3b63..903edfa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -120,6 +120,7 @@ dependencies = [ "argparse", "bliss-audio", "chrono", + "configparser", "dirs", "env_logger", "indicatif", @@ -282,6 +283,12 @@ dependencies = [ "libloading", ] +[[package]] +name = "configparser" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06821ea598337a8412cf47c5b71c3bc694a7f0aed188ac28b836fab164a2c202" + [[package]] name = "console" version = "0.15.0" diff --git a/Cargo.toml b/Cargo.toml index d8bc0a9..8fdd236 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,3 +23,4 @@ chrono = "0.4.19" regex = "1" substring = "1.4.5" ureq = "2.4.0" +configparser = "3.0.0" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 71ed65f..aea998f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ **/ use argparse::{ArgumentParser, Store, StoreTrue}; use chrono::Local; +use configparser::ini::Ini; use dirs; use log::LevelFilter; use std::io::Write; @@ -17,9 +18,12 @@ mod db; mod tags; mod upload; + const VERSION: &'static str = env!("CARGO_PKG_VERSION"); +const TOP_LEVEL_INI_TAG:&str = "Bliss"; fn main() { + let mut config_file = "config.ini".to_string(); let mut db_path = "bliss.db".to_string(); let mut logging = "info".to_string(); let mut music_path = ".".to_string(); @@ -35,6 +39,7 @@ fn main() { } { + let config_file_help = format!("config file (default: {})", &config_file); 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); @@ -47,6 +52,7 @@ fn main() { // borrow per scope, hence this section is enclosed in { } let mut arg_parse = ArgumentParser::new(); arg_parse.set_description(&description); + arg_parse.refer(&mut config_file).add_option(&["-c", "--config"], Store, &config_file_help); 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, &logging_help); @@ -71,6 +77,37 @@ fn main() { process::exit(-1); } + if !config_file.is_empty() { + let path = PathBuf::from(&config_file); + if path.exists() && path.is_file() { + let mut config = Ini::new(); + match config.load(&config_file) { + Ok(_) => { + match config.get(TOP_LEVEL_INI_TAG, "music") { + Some(val) => { music_path = val; }, + None => { } + } + match config.get(TOP_LEVEL_INI_TAG, "db") { + Some(val) => { db_path = val; }, + None => { } + } + match config.get(TOP_LEVEL_INI_TAG, "lms") { + Some(val) => { lms_host = val; }, + None => { } + } + match config.get(TOP_LEVEL_INI_TAG, "ignore") { + Some(val) => { ignore_file = val; }, + None => { } + } + }, + Err(e) => { + log::error!("Failed to load config file. {}", e); + process::exit(-1); + } + } + } + } + if db_path.len() < 3 { log::error!("Invalid DB path ({}) supplied", db_path); process::exit(-1);