diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/src/main.rs b/src/main.rs index ced1391..387f1c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,14 +2,14 @@ mod channel; mod config; mod retrieve; -use std::future; +use std::{fs::File, future, io::{BufRead, BufReader}, path::{Path, PathBuf}}; use anyhow::anyhow; use clap::Parser; use futures::{StreamExt, executor::block_on}; use rss::Channel; -use crate::{channel::make_new_empty_channel, config::{ChannelConfig, Config}, retrieve::retrieve}; +use crate::{channel::make_new_empty_channel, config::Config, retrieve::retrieve}; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] @@ -21,22 +21,41 @@ struct Args { fn main() { let args = Args::parse(); - let config = load_config(&args.config_file).unwrap(); - let channel = generate_channel(config.urls, config.channel); + let (urls, config) = load_config(&args.config_file).unwrap(); + let channel = generate_channel(urls, config); let _ = channel.pretty_write_to(std::io::stdout(), b' ', 4).unwrap(); } -fn load_config(config_file_arg: &str) -> anyhow::Result<Config> { - match config_file_arg { +fn load_config(config_file_arg: &str) -> anyhow::Result<(Vec<String>, Config)> { + let (config, config_path) = match config_file_arg { "" => Config::find_config().ok_or_else(|| anyhow!("Failed to find config. Paths searched: {:#?}", config::default_config_file_locations())), - path => Config::parse_config_file_at_path(path).map_err(|err| + path => Config::parse_config_file_at_path(path).map(|config| (config, String::from(path))).map_err(|err| anyhow!("Failed to parse config at {}. Error: {}", path, err)), - } + }?; + + let subscriptions_path: PathBuf = config.subscriptions_path.as_ref() + .map(|p| Path::new(&p).to_path_buf()) + .unwrap_or_else( + || Path::new(&config_path) + .parent() + .unwrap_or_else(|| Path::new("/")) + .join("subscriptions.txt")); + let urls: Vec<String> = if subscriptions_path.is_file() { + let subscriptions_file = File::open(subscriptions_path)?; + BufReader::new(subscriptions_file).lines() + .filter(|line| line.is_err() || line.as_ref().is_ok_and(|line| + line.trim().len() > 0 && !line.trim().starts_with("#"))) + .collect::<Result<Vec<String>, std::io::Error>>()? + } else { + Vec::new() + }; + + Ok((urls, config)) } -fn generate_channel(urls: Vec<String>, channel_config: ChannelConfig) -> Channel { - let mut channel = make_new_empty_channel(channel_config); +fn generate_channel(urls: Vec<String>, config: Config) -> Channel { + let mut channel = make_new_empty_channel(config); block_on(async { retrieve(urls.iter().map(AsRef::as_ref)).await.for_each(|c| { channel.items.append(&mut c.into_items()); |
