summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2020-01-02 13:44:41 +0100
committerMatthias Beyer <mail@beyermatthias.de>2020-01-03 10:19:49 +0100
commita6ca22c73036be412dafa04265506eb9135c6fd1 (patch)
treee63671d926611e4b1b6c3934222bfb89875e981d
parent0122e59bb073a4bee0e74f6d1e09c32e077dc249 (diff)
Add imag-mail-scan command for scanning directory for new mail
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--bin/domain/imag-mail/Cargo.toml1
-rw-r--r--bin/domain/imag-mail/src/lib.rs32
-rw-r--r--bin/domain/imag-mail/src/ui.rs20
3 files changed, 53 insertions, 0 deletions
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::<Result<Vec<()>>>()
+ .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")