commit 0b00f842a93119c9db440d2c8b9d95863a804933 from: witcher date: Sat Dec 17 19:53:37 2022 UTC Supply defaults for optional command line args Implements: https://todo.sr.ht/~witcher/rss-email/18 commit - 22f3db115f6e65409686585dec21726d86af2078 commit + 0b00f842a93119c9db440d2c8b9d95863a804933 blob - 0d15980c1d692de242b85fa01246d6a27b7e0b54 blob + ec6b70468754100e7acd479eefaf2770236cb2b8 --- src/cli.rs +++ src/cli.rs @@ -10,18 +10,15 @@ use super::{AUTHORS, VERSION}; #[clap(version = VERSION)] #[clap(about = "Fetch RSS Feeds and send them via E-Mail")] pub struct Cli { - // TODO: replace with default value instead of specifying an `Option` /// Custom path to database - #[clap(long = "database", value_parser)] - pub database_path: Option, - // TODO: replace with default value instead of specifying an `Option` + #[clap(long = "database", value_parser, default_value_t = database_path().into_os_string().into_string().unwrap())] + pub database_path: String, /// Custom path to configuration - #[clap(long = "config", value_parser)] - pub config_path: Option, - // TODO: replace with default value instead of specifying an `Option` + #[clap(long = "config", value_parser, default_value_t = config_path().into_os_string().into_string().unwrap())] + pub config_path: String, /// Custom path to URLs with RSS feeds being fetched - #[clap(long = "urls", value_parser)] - pub urls_path: Option, + #[clap(long = "urls", value_parser, default_value_t = urls_path().into_os_string().into_string().unwrap())] + pub urls_path: String, /// Don't send E-Mails #[clap(long, value_parser)] pub dry_run: bool, @@ -33,7 +30,7 @@ impl Cli { pub fn build_app() -> anyhow::Result { use std::fs::{self, File}; - let mut args = Cli::parse(); + let args = Cli::parse(); let c_dir = config_dir(); // ensure project config directory exists @@ -45,24 +42,13 @@ impl Cli { fs::create_dir(c_dir)?; } - if args.database_path.is_none() { - let db_path = database_path(); - args.database_path = Some(db_path.clone()); - - if File::open(&db_path).is_err() { - debug!( - "No database file exists, creating a new one: {:?}", - &db_path - ); - File::create(&db_path)?; - } + if File::open(&args.database_path).is_err() { + debug!( + "No database file exists, creating a new one: {:?}", + &args.database_path + ); + File::create(&args.database_path)?; } - if args.config_path.is_none() { - args.config_path = Some(config_path()); - } - if args.urls_path.is_none() { - args.urls_path = Some(urls_path()); - } Ok(args) } @@ -77,16 +63,19 @@ fn config_dir() -> PathBuf { } /// Returns the path to the database. +#[allow(dead_code)] fn database_path() -> PathBuf { config_dir().join("cache.db") } /// Returns the path to the configuration file. +#[allow(dead_code)] fn config_path() -> PathBuf { config_dir().join("config.toml") } /// Returns the path to the urls file. +#[allow(dead_code)] fn urls_path() -> PathBuf { config_dir().join("urls") } blob - 71b2582db946f14d1e77d93c552355e4bd452be9 blob + 69948f7fe6a4e1dd33ce2ce838b005b6803751ab --- src/main.rs +++ src/main.rs @@ -30,27 +30,21 @@ const AUTHORS: &str = env!("CARGO_PKG_AUTHORS"); async fn main() -> anyhow::Result<()> { env_logger::init(); - // NOTE: `build_app` ensures all `Option`s in `args` to be `Some`, so unwrapping the values - // should be fine let args = cli::Cli::build_app()?; - - let config = Arc::new(Config::new(args.config_path.unwrap())?); - + let config = Arc::new(Config::new(args.config_path)?); let urls = BufReader::new( - File::open(args.urls_path.as_ref().unwrap()).context(format!( - "File {:?} does not exist", - &args.urls_path.unwrap() - ))?, + File::open(args.urls_path.as_str()) + .context(format!("File {:?} does not exist", &args.urls_path))?, ) .lines() .map(|l| l.unwrap()) .filter(|l| !l.starts_with('#')); - let db_path = args.database_path.unwrap(); + let db_path = args.database_path; debug!("Establishing connection to database at {:?}", db_path); let pool = SqlitePoolOptions::new() .max_connections(5) - .connect(db_path.to_str().unwrap()) + .connect(db_path.as_str()) .await?; sqlx::migrate!("./migrations").run(&pool).await?;