commit ee004920e3e72e46c80c7f6c4da6ff3b48cf6b46 from: witcher date: Mon Aug 15 10:59:36 2022 UTC Create config dir if it doesn't exist commit - e2466bf6fd47d5436c2b6319d83df6690c4423bb commit + ee004920e3e72e46c80c7f6c4da6ff3b48cf6b46 blob - e5bcae02851e98b73e1f059ee4e76f222418e799 blob + abd6eaefdb548d8a5121acd417b285fcf4a8381e --- src/cli.rs +++ src/cli.rs @@ -8,12 +8,15 @@ use std::path::PathBuf; #[clap(version = "0.1.0")] #[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` /// Custom path to configuration #[clap(long = "config", value_parser)] pub config_path: Option, + // TODO: replace with default value instead of specifying an `Option` /// Custom path to URLs with RSS feeds being fetched #[clap(long = "urls", value_parser)] pub urls_path: Option, @@ -23,19 +26,30 @@ pub struct Cli { } impl Cli { + /// Parse the clap `Cli` struct with the command line arguments, create the directory for the + /// configuration files, and create the database if it does not exist yet. pub fn build_app() -> anyhow::Result { + use std::fs::{self, File}; + let mut args = Cli::parse(); + let c_dir = config_dir(); + // ensure project config directory exists + if !c_dir.exists() { + debug!("Config directory at {:?} does not exist, creating it.", c_dir); + fs::create_dir(c_dir)?; + } + if args.database_path.is_none() { let db_path = database_path(); args.database_path = Some(db_path.clone()); - if std::fs::File::open(&db_path).is_err() { + if File::open(&db_path).is_err() { debug!( "No database file exists, creating a new one: {:?}", &db_path ); - std::fs::File::create(&db_path)?; + File::create(&db_path)?; } } if args.config_path.is_none() { @@ -49,6 +63,7 @@ impl Cli { } } +/// Returns the base directory where all the configuration files are stored. fn config_dir() -> PathBuf { directories::ProjectDirs::from("", "", "rss-email") .unwrap() @@ -56,14 +71,17 @@ fn config_dir() -> PathBuf { .to_path_buf() } +/// Returns the path to the database. fn database_path() -> PathBuf { config_dir().join("cache.db") } +/// Returns the path to the configuration file. fn config_path() -> PathBuf { config_dir().join("config.toml") } +/// Returns the path to the urls file. fn urls_path() -> PathBuf { config_dir().join("urls") }