From cb7c4d42632dc3284157e56a2daafbcf34a3c534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sun, 26 Mar 2023 16:07:49 +0200 Subject: [PATCH] Add a minimal CLI --- Cargo.toml | 1 + src/config.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/main.rs | 15 +++++++++++++-- src/message.rs | 3 ++- 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 src/config.rs diff --git a/Cargo.toml b/Cargo.toml index ce75883..45e6340 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ license = "MIT OR Apache-2.0" publish = false [dependencies] +clap = { version = "4.1.13", default-features = false, features = ["std", "derive", "help", "usage"] } env_logger = "^0.10" log = "^0.4" mailparse = "^0.14" diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..76beea1 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,40 @@ +use clap::Parser; +use std::collections::HashSet; +use std::fs::File; +//use std::io::BufReader; +use std::io::{BufRead, BufReader}; +use std::path::PathBuf; + +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +pub struct Config { + #[arg(short, long)] + domain: Vec, + #[arg(short = 'D', long, value_name = "FILE")] + domain_file: Option, + #[arg(short, long, value_name = "FILE")] + revocation_list: Option, +} + +impl Config { + pub fn init() -> Result { + let mut cnf = Self::parse(); + let mut domain_set: HashSet = cnf.domain.into_iter().collect(); + if let Some(path) = &cnf.domain_file { + let f = File::open(path).map_err(|e| format!("{}: {e}", path.display()))?; + for line in BufReader::new(f).lines() { + let line = line.map_err(|e| format!("{}: {e}", path.display()))?; + let domain = line.trim(); + if !domain.is_empty() && !domain.starts_with('#') { + domain_set.insert(domain.to_string()); + } + } + } + cnf.domain = domain_set.into_iter().collect::>(); + Ok(cnf) + } + + pub fn domains(&self) -> &[String] { + &self.domain + } +} diff --git a/src/main.rs b/src/main.rs index fe5c3f4..cdb75a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ mod canonicalization; +mod config; mod entry; mod handshake; mod logs; @@ -43,6 +44,16 @@ macro_rules! log_messages { fn main() { logs::init_log_system(); + match config::Config::init() { + Ok(cnf) => { + log::debug!("{cnf:?}"); + main_loop(&cnf) + } + Err(e) => log::error!("{e}"), + } +} + +fn main_loop(cnf: &config::Config) { let mut reader = StdinReader::new(); let mut messages: HashMap = HashMap::new(); handshake::read_config(&mut reader); @@ -59,7 +70,7 @@ fn main() { msg.append_line(entry.get_data()); } else { log::debug!("message ready: {msg_id}"); - msg.sign_and_return(); + msg.sign_and_return(&cnf); messages.remove(&msg_id); log::debug!("message removed: {msg_id}"); } @@ -71,7 +82,7 @@ fn main() { messages.insert(msg_id, msg); } else { log::debug!("empty new message: {msg_id}"); - msg.sign_and_return(); + msg.sign_and_return(&cnf); } } } diff --git a/src/message.rs b/src/message.rs index 4cb78e1..2276f94 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,3 +1,4 @@ +use crate::config::Config; use crate::entry::Entry; use mailparse::parse_mail; use std::io::{BufWriter, Write}; @@ -50,7 +51,7 @@ impl Message { self.nb_lines } - pub fn sign_and_return(&self) { + pub fn sign_and_return(&self, cnf: &Config) { log::trace!("content:\n{}", crate::display_bytes!(&self.content)); match parse_mail(&self.content) { Ok(parsed_msg) => {