From a6ca22c73036be412dafa04265506eb9135c6fd1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 2 Jan 2020 13:44:41 +0100 Subject: Add imag-mail-scan command for scanning directory for new mail Signed-off-by: Matthias Beyer --- bin/domain/imag-mail/Cargo.toml | 1 + bin/domain/imag-mail/src/lib.rs | 32 ++++++++++++++++++++++++++++++++ bin/domain/imag-mail/src/ui.rs | 20 ++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/bin/domain/imag-mail/Cargo.toml b/bin/domain/imag-mail/Cargo.toml index ed88d064..13a60a50 100644 --- a/bin/domain/imag-mail/Cargo.toml +++ b/bin/domain/imag-mail/Cargo.toml @@ -24,6 +24,7 @@ log = "0.4.6" failure = "0.1.5" indoc = "0.3.3" resiter = "0.4" +walkdir = "2" libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" } libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" } diff --git a/bin/domain/imag-mail/src/lib.rs b/bin/domain/imag-mail/src/lib.rs index 0dd7f2bb..e290d7d8 100644 --- a/bin/domain/imag-mail/src/lib.rs +++ b/bin/domain/imag-mail/src/lib.rs @@ -40,6 +40,7 @@ extern crate clap; extern crate toml_query; #[macro_use] extern crate indoc; extern crate resiter; +extern crate walkdir; extern crate libimagrt; extern crate libimagmail; @@ -58,6 +59,7 @@ use toml_query::read::TomlValueReadTypeExt; use clap::App; use resiter::AndThen; use resiter::IterInnerOkOrElse; +use resiter::Map; use libimagmail::mail::Mail; use libimagmail::store::MailStore; @@ -81,6 +83,7 @@ pub enum ImagMail {} impl ImagApplication for ImagMail { fn run(rt: Runtime) -> Result<()> { match rt.cli().subcommand_name().ok_or_else(|| err_msg("No subcommand called"))? { + "scan" => scan(&rt), "import-mail" => import_mail(&rt), "list" => list(&rt), "mail-store" => mail_store(&rt), @@ -112,6 +115,35 @@ impl ImagApplication for ImagMail { } } +fn scan(rt: &Runtime) -> Result<()> { + let collection_name = get_ref_collection_name(rt)?; + let refconfig = get_ref_config(rt, "imag-mail")?; + let scmd = rt.cli().subcommand_matches("scan").unwrap(); + let store = rt.store(); + + scmd.values_of("path") + .unwrap() // enforced by clap + .map(PathBuf::from) + .map(|path| { + walkdir::WalkDir::new(path) + .follow_links(false) + .into_iter() + .filter_entry(|entry| entry.file_type().is_file()) + .map_ok(|entry| entry.into_path()) + .map_err(Error::from) + }) + .flatten() + .and_then_ok(|path| { + if scmd.is_present("ignore_existing_ids") { + store.retrieve_mail_from_path(path, &collection_name, &refconfig) + } else { + store.create_mail_from_path(path, &collection_name, &refconfig) + } + }) + .and_then_ok(|e| rt.report_touched(e.get_location()).map_err(Error::from)) + .collect::>>() + .map(|_| ()) +} fn import_mail(rt: &Runtime) -> Result<()> { let collection_name = get_ref_collection_name(rt)?; diff --git a/bin/domain/imag-mail/src/ui.rs b/bin/domain/imag-mail/src/ui.rs index e1fa88e6..de0cd0a7 100644 --- a/bin/domain/imag-mail/src/ui.rs +++ b/bin/domain/imag-mail/src/ui.rs @@ -47,6 +47,26 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .value_name("PATH")) ) + .subcommand(SubCommand::with_name("scan") + .about("Scan a directory for mails") + .version("0.1") + .arg(Arg::with_name("ignore-existing-ids") + .long("ignore-existing") + .short("I") + .takes_value(false) + .required(false) + .help("Ignore errors that might occur when store entries exist already")) + + .arg(Arg::with_name("path") + .index(1) + .takes_value(true) + .multiple(true) + .required(true) + .validator(libimagutil::cli_validators::is_directory) + .value_name("DIR") + .help("Path to the directory containing mails")) + ) + .subcommand(SubCommand::with_name("list") .about("List all stored references to mails") .version("0.1") -- cgit v1.2.3