From 1989b48d99a4b57d3f1c042588321fe6beb7a7c7 Mon Sep 17 00:00:00 2001 From: Dave Gallant Date: Mon, 22 Jun 2020 22:54:06 -0400 Subject: [PATCH] Add optional --dbpath flag (#6) * Use common config with lower cache capacity * Add command line argument parser * Add optional --dbpath flag --- Cargo.lock | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + README.md | 2 +- src/db.rs | 15 ++++++++++---- src/main.rs | 56 +++++++++++++++++++++++++++++++---------------------- src/rfd.rs | 6 +++--- 6 files changed, 101 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c52cdcb..160aff7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,6 +18,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi 0.3.8", +] + [[package]] name = "arc-swap" version = "0.4.7" @@ -102,6 +111,21 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "clap" +version = "2.33.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdfa80d47f954d53a35a64987ca1422f495b8d6483c0fe9f7117b36c2a792129" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "cloudabi" version = "0.0.3" @@ -1045,6 +1069,7 @@ dependencies = [ name = "rfd-notify" version = "0.1.0" dependencies = [ + "clap", "log", "pretty_env_logger", "regex", @@ -1280,6 +1305,12 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + [[package]] name = "syn" version = "1.0.33" @@ -1326,6 +1357,15 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "thread_local" version = "1.0.1" @@ -1464,6 +1504,12 @@ dependencies = [ "smallvec", ] +[[package]] +name = "unicode-width" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" + [[package]] name = "unicode-xid" version = "0.2.0" @@ -1493,6 +1539,12 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c" +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "version_check" version = "0.9.2" diff --git a/Cargo.toml b/Cargo.toml index f837b89..a45abef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,4 @@ pretty_env_logger = "0.4.0" regex = "1.3.9" rust-crypto = "^0.2" sled = "0.31.0" +clap = "2.33.1" diff --git a/README.md b/README.md index 9890b03..d165374 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ This tool looks for regular expressions from [RedFlagDeals.com forums](https://f Declare a configuration. An example can found in [config.toml](./examples/config.toml) ```shell -rfd-notify ./examples/config.toml +rfd-notify --config ./examples/config.toml ``` ## cross compile diff --git a/src/db.rs b/src/db.rs index cba116b..be5afc5 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,5 +1,12 @@ -pub fn hash_exists(hash: &str) -> bool { - let tree = sled::open("./deals_db").expect("open"); +pub fn get_config(dbpath: &str) -> sled::Config { + sled::Config::default() + .path(dbpath) + .cache_capacity(100_000_000) + .flush_every_ms(Some(1000)) +} + +pub fn hash_exists(hash: &str, config: sled::Config) -> bool { + let tree = config.open().unwrap(); let result = tree.get(hash); if result.is_err() { error!("{:?}", &result); @@ -10,8 +17,8 @@ pub fn hash_exists(hash: &str) -> bool { true } -pub fn insert(hash: &str) { - let tree = sled::open("./deals_db").expect("open"); +pub fn insert(hash: &str, config: sled::Config) { + let tree = config.open().unwrap(); let result = tree.insert(&hash, ""); if result.is_err() { error!("{:?}", &result); diff --git a/src/main.rs b/src/main.rs index 9cefb77..8b0bfd1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,38 +1,48 @@ extern crate pretty_env_logger; #[macro_use] extern crate log; +extern crate clap; extern crate crypto; mod config; mod db; mod mail; mod rfd; -use std::env; - -fn help() { - println!( - "usage:\n -rfd-notify - Specify the filepath of the config." - ); -} +use clap::{App, Arg}; fn main() { pretty_env_logger::init(); - let args: Vec = env::args().collect(); - match args.len() { - 2 => { - let config_path = &args[1]; - let config = config::parse(config_path); - debug!("{:?}\n", config); - let hot_deals = rfd::get_hot_deals().map_err(|err| error!("{:?}", err)).ok(); - let parsed_deals = rfd::parse_hot_deals(&hot_deals.unwrap()); - rfd::match_deals(parsed_deals, config) - } - _ => { - help(); - } - } + let app = App::new("rfd-notify") + .version("0.1.0") + .about("Send emails based on regular expressions") + .args(&[ + Arg::with_name("config") + .required(true) + .takes_value(true) + .short("c") + .help("Specify path to config") + .long("config"), + Arg::with_name("dbpath") + .default_value("./deals_db") + .takes_value(true) + .short("d") + .help("Specify path to where the embedded database is stored") + .long("dbpath"), + ]); + + let matches = app.get_matches(); + + let config = matches.value_of("config").unwrap(); + let parsed_config = config::parse(config); + + debug!("{:?}\n", parsed_config); + let hot_deals = rfd::get_hot_deals().map_err(|err| error!("{:?}", err)).ok(); + let parsed_deals = rfd::parse_hot_deals(&hot_deals.unwrap()); + rfd::match_deals( + parsed_deals, + parsed_config, + matches.value_of("dbpath").unwrap(), + ); info!("Complete") } diff --git a/src/rfd.rs b/src/rfd.rs index 7dee795..7b5cd53 100644 --- a/src/rfd.rs +++ b/src/rfd.rs @@ -73,7 +73,7 @@ fn hash_deal(topic: &Topic) -> String { hasher.result_str() } -pub fn match_deals(deals: Deals, config: Config) { +pub fn match_deals(deals: Deals, config: Config, dbpath: &str) { for topic in deals.topics.iter() { for expression in config.expressions.iter() { let mut found_match = false; @@ -101,7 +101,7 @@ pub fn match_deals(deals: Deals, config: Config) { continue; } let deal_hash = hash_deal(topic); - if db::hash_exists(&deal_hash) { + if db::hash_exists(&deal_hash, db::get_config(dbpath)) { debug!("deal hash '{}' already exists", &deal_hash); } else { let posts = parse_posts( @@ -110,7 +110,7 @@ pub fn match_deals(deals: Deals, config: Config) { .ok() .unwrap(), ); - db::insert(&deal_hash); + db::insert(&deal_hash, db::get_config(dbpath)); mail::send(topic, &posts, expression, &config); } }