commit - e2466bf6fd47d5436c2b6319d83df6690c4423bb
commit + ee004920e3e72e46c80c7f6c4da6ff3b48cf6b46
blob - e5bcae02851e98b73e1f059ee4e76f222418e799
blob + abd6eaefdb548d8a5121acd417b285fcf4a8381e
--- src/cli.rs
+++ src/cli.rs
#[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<PathBuf>,
+ // TODO: replace with default value instead of specifying an `Option`
/// Custom path to configuration
#[clap(long = "config", value_parser)]
pub config_path: Option<PathBuf>,
+ // 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<PathBuf>,
}
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<Self> {
+ 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() {
}
}
+/// Returns the base directory where all the configuration files are stored.
fn config_dir() -> PathBuf {
directories::ProjectDirs::from("", "", "rss-email")
.unwrap()
.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")
}