summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/config.rs30
-rw-r--r--src/main.rs17
2 files changed, 45 insertions, 2 deletions
diff --git a/src/config.rs b/src/config.rs
new file mode 100644
index 0000000..518a82a
--- /dev/null
+++ b/src/config.rs
@@ -0,0 +1,30 @@
+use std::{fs::File, io::{Read, read_to_string}, path::Path};
+
+use serde::Deserialize;
+
+#[derive(Deserialize)]
+pub struct Config {
+}
+
+pub const DEFAULT_CONFIG_FILE_LOCATIONS: [&str; 1] = [
+ "/etc/rss-aggregator.toml",
+];
+
+impl Config {
+ pub fn parse_config_file_at_path(path: &str) -> anyhow::Result<Config> {
+ let path = Path::new(path);
+ let file = File::open(&path)?;
+ let contents = read_to_string(file)?;
+ Ok(toml::from_str(&contents)?)
+ }
+
+ pub fn find_config() -> Option<Config> {
+ for path in DEFAULT_CONFIG_FILE_LOCATIONS {
+ match Self::parse_config_file_at_path(path) {
+ Ok(config) => { return Some(config); },
+ Err(_) => {},
+ };
+ };
+ None
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 705b33b..b445285 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,11 +1,24 @@
+mod config;
+
use clap::Parser;
+use crate::config::Config;
+
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
+ /// Path to the config file. If blank, searches default config locations.
+ #[arg(short = 'c', long = "config", default_value = "")]
+ config_file: String,
}
fn main() {
- let _args = Args::parse();
- println!("Hello, world!");
+ let args = Args::parse();
+ let _config = match args.config_file.as_str() {
+ "" => Config::find_config().unwrap_or_else(||
+ panic!("Failed to find config. Paths searched: {:#?}", config::DEFAULT_CONFIG_FILE_LOCATIONS)),
+ path => Config::parse_config_file_at_path(&path).unwrap_or_else(|err|
+ panic!("Failed to parse config at {}. Error: {}", path, err)),
+ };
+ println!("Found config file.");
}