Add a minimal CLI

This commit is contained in:
Rodolphe Bréard 2023-03-26 16:07:49 +02:00
parent 3cd5071cd3
commit cb7c4d4263
4 changed files with 56 additions and 3 deletions

View file

@ -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"

40
src/config.rs Normal file
View file

@ -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<String>,
#[arg(short = 'D', long, value_name = "FILE")]
domain_file: Option<PathBuf>,
#[arg(short, long, value_name = "FILE")]
revocation_list: Option<PathBuf>,
}
impl Config {
pub fn init() -> Result<Self, String> {
let mut cnf = Self::parse();
let mut domain_set: HashSet<String> = 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::<Vec<_>>();
Ok(cnf)
}
pub fn domains(&self) -> &[String] {
&self.domain
}
}

View file

@ -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<String, Message> = 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);
}
}
}

View file

@ -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) => {