Clean things up a bit

This commit is contained in:
Marc Plano-Lesay 2025-05-01 13:40:33 +10:00
parent 3f2b002f52
commit 774a5ed4ac
Signed by: kernald
GPG key ID: 66A41B08CC62A6CF
9 changed files with 223 additions and 172 deletions

View file

@ -1,48 +1,27 @@
use crate::actions::transmission::{TransmissionAction, TransmissionConfig};
use crate::actions::transmission::TransmissionAction;
use crate::args::Args;
use crate::config::{get_db_path, load_config};
use crate::db::Database;
use crate::magnet::{extract_magnet_links, Magnet};
use chrono::{DateTime, Utc};
use clap::Parser;
use clap_verbosity_flag::{InfoLevel, Verbosity};
use color_eyre::eyre::{eyre, Result, WrapErr};
use directories::ProjectDirs;
use figment::providers::Env;
use figment::{
providers::{Format, Toml},
Figment,
};
use figment_file_provider_adapter::FileAdapter;
use log::{debug, info, warn};
use multimap::MultiMap;
use reddit_client::RedditClient;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::collections::HashSet;
use std::fs::create_dir_all;
use std::path::{Path, PathBuf};
mod actions;
mod args;
mod config;
mod db;
mod magnet;
mod models;
mod reddit_client;
mod schema;
#[derive(Debug, Serialize, Deserialize)]
struct SourceConfig {
username: String,
title_filter: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
struct Config {
#[serde(default)]
transmission: Option<TransmissionConfig>,
#[serde(default)]
sources: HashMap<String, SourceConfig>,
}
#[derive(Debug)]
struct PostInfo {
title: String,
@ -52,21 +31,6 @@ struct PostInfo {
timestamp: DateTime<Utc>,
}
#[derive(Parser, Debug)]
#[command(author, version, about = "Display recent posts from a Reddit user")]
struct Args {
/// Path to the configuration file
#[arg(short, long)]
config: Option<String>,
/// Path to the database file
#[arg(short, long)]
db: Option<String>,
#[command(flatten)]
verbose: Verbosity<InfoLevel>,
}
/// Filters posts based on a title filter pattern
fn filter_posts(title: &str, title_filter: Option<Regex>) -> bool {
match title_filter {
@ -123,12 +87,7 @@ async fn main() -> Result<()> {
.init();
// Initialize database
let db_path = match args.db {
Some(path) => PathBuf::from(path),
None => ProjectDirs::from("fr", "enoent", "reddit-magnet")
.map(|p| p.data_dir().join("reddit-magnet.db"))
.ok_or_else(|| eyre!("Could not determine data directory"))?,
};
let db_path = get_db_path(&args)?;
// Create parent directory if it doesn't exist
if let Some(parent) = db_path.parent() {
@ -139,30 +98,7 @@ async fn main() -> Result<()> {
let mut db = Database::new(&db_path)
.wrap_err_with(|| format!("Failed to initialize database at {:?}", db_path))?;
let mut conf_extractor = Figment::new();
let config_file_path: Option<PathBuf> = match args.config {
Some(path) => Some(Path::new(&path).to_path_buf()),
None => ProjectDirs::from("fr", "enoent", "reddit-magnet")
.map(|p| p.config_dir().join("config.toml")),
};
match config_file_path {
Some(path) => {
if path.exists() {
debug!("Reading configuration from {:?}", path);
conf_extractor = conf_extractor.merge(FileAdapter::wrap(Toml::file_exact(path)));
} else {
debug!("Configuration file doesn't exist at {:?}", path);
}
}
None => {
debug!("No configuration file specified, using default configuration");
}
}
let conf: Config = conf_extractor
.merge(FileAdapter::wrap(Env::prefixed("REDDIT_MAGNET_")))
.extract()
.wrap_err_with(|| "Invalid configuration or insufficient command line arguments")?;
let conf = load_config(&args)?;
if conf.sources.is_empty() {
return Err(eyre!("No sources found in configuration. Please add at least one source to your configuration file.").into());