// // imag - the personal information management suite for the commandline // Copyright (C) 2015-2020 Matthias Beyer and contributors // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; version // 2.1 of the License. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // use anyhow::Result; use libimagrt::runtime::Runtime; use toml_query::read::TomlValueReadExt; use libimagmail::store::MailStore; use libimagmail::notmuch::connection::NotmuchConnection; use libimagentrytag::tag::is_tag_str; use config::MailConfig; #[allow(clippy::let_and_return)] pub fn import(rt: &Runtime) -> Result<()> { let config = rt.config() .ok_or_else(|| anyhow!("Configuration missing"))? .read_partial::()? .ok_or_else(|| anyhow!("Configuration for \"mail\" missing"))?; let scmd = rt.cli().subcommand_matches("import").unwrap(); let query = scmd.value_of("query").unwrap(); let quick = scmd.is_present("quick"); let store = rt.store(); let notmuch_path = config.get_notmuch_database_path_or_cli(rt); let notmuch_connection = NotmuchConnection::open(notmuch_path)?; let import_nm_tags = config.get_import_notmuch_tags(); if let Some(ref tag) = config.get_import_tag() { let _ = is_tag_str(tag)?; } debug!("Importing mail with query = '{}'", query); let r = store .with_connection(¬much_connection) .import_with_query(query, config.get_import_tag().clone(), *import_nm_tags, quick)? .into_iter() .map(|fle| rt.report_touched(fle.get_location())) .collect::>>() .map(|_| ()); r }