From f1ec71431e50c796d7c73ad9250b3a1a658c551d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 3 Oct 2019 19:33:56 +0200 Subject: [PATCH] Implement show subcommand Signed-off-by: Matthias Beyer --- bin/domain/imag-calendar/src/main.rs | 52 ++++++++++++++++++++++++++++ bin/domain/imag-calendar/src/ui.rs | 19 ++++++++++ bin/domain/imag-calendar/src/util.rs | 45 ++++++++++++++++++++++++ 3 files changed, 116 insertions(+) diff --git a/bin/domain/imag-calendar/src/main.rs b/bin/domain/imag-calendar/src/main.rs index 21258d17..c87d77d0 100644 --- a/bin/domain/imag-calendar/src/main.rs +++ b/bin/domain/imag-calendar/src/main.rs @@ -87,6 +87,7 @@ fn main() { match name { "import" => import(&rt), "list" => list(&rt), + "show" => show(&rt), other => { warn!("Right now, only the 'import' command is available"); debug!("Unknown command"); @@ -250,6 +251,57 @@ fn list(rt: &Runtime) { }); } +fn show(rt: &Runtime) { + let scmd = rt.cli().subcommand_matches("show").unwrap(); // safe by clap + let ref_config = rt.config() + .ok_or_else(|| format_err!("No configuration, cannot continue!")) + .map_err_trace_exit_unwrap() + .read_partial::() + .map_err(Error::from) + .map_err_trace_exit_unwrap() + .ok_or_else(|| format_err!("Configuration missing: {}", libimagentryref::reference::Config::LOCATION)) + .map_err_trace_exit_unwrap(); + + let list_format = util::get_event_print_format("calendar.show_format", rt, &scmd) + .map_err_trace_exit_unwrap(); + + let mut shown_events = 0; + + scmd.values_of("show-ids") + .unwrap() // safe by clap + .into_iter() + .filter_map(|id| { + util::find_event_by_id(rt.store(), id, &ref_config) + .map(|entry| { debug!("Found => {:?}", entry); entry }) + .map_err_trace_exit_unwrap() + .map(|parsed| (parsed, id)) + }) + .for_each(|(parsed_entry, id)| { + parsed_entry + .get_data() + .events() + .filter_map(RResult::ok) + .filter(|pent| { + let relevant = pent.uid().map(|uid| uid.raw().starts_with(id)).unwrap_or(false); + debug!("Relevant {} => {}", parsed_entry.get_entry().get_location(), relevant); + relevant + }) + .for_each(|event| { + shown_events = shown_events + 1; + let data = util::build_data_object_for_handlebars(shown_events, &event); + + let rendered = list_format + .render("format", &data) + .map_err(Error::from) + .map_err_trace_exit_unwrap(); + + writeln!(rt.stdout(), "{}", rendered).to_exit_code().unwrap_or_exit() + }); + + rt.report_touched(parsed_entry.get_entry().get_location()).unwrap_or_exit(); + }); +} + /// helper function to check whether a DirEntry points to something hidden (starting with dot) fn is_not_hidden(entry: &DirEntry) -> bool { !entry.file_name().to_str().map(|s| s.starts_with(".")).unwrap_or(false) diff --git a/bin/domain/imag-calendar/src/ui.rs b/bin/domain/imag-calendar/src/ui.rs index 76a565ec..f62680ba 100644 --- a/bin/domain/imag-calendar/src/ui.rs +++ b/bin/domain/imag-calendar/src/ui.rs @@ -89,6 +89,25 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .multiple(false) .help("List events which are dated after certain date")) ) + + .subcommand(SubCommand::with_name("show") + .about("Show one or several calendar entries") + .version("0.1") + .arg(Arg::with_name("show-ids") + .index(1) + .takes_value(true) + .required(true) + .multiple(true) + .help("UIDs of Calendar entries to show")) + + .arg(Arg::with_name("format") + .long("format") + .short("F") + .takes_value(true) + .required(false) + .multiple(false) + .help("Override the format used to show events")) + ) } fn import_validator>(s: A) -> Result<(), String> { diff --git a/bin/domain/imag-calendar/src/util.rs b/bin/domain/imag-calendar/src/util.rs index f0145de2..e51b0a04 100644 --- a/bin/domain/imag-calendar/src/util.rs +++ b/bin/domain/imag-calendar/src/util.rs @@ -31,11 +31,13 @@ use chrono::NaiveDateTime; use libimagrt::runtime::Runtime; use libimagstore::store::FileLockEntry; +use libimagstore::store::Store; use libimagentryref::reference::fassade::RefFassade; use libimagentryref::reference::Ref; use libimagentryref::reference::Config; use libimagentryref::hasher::default::DefaultHasher; use libimagerror::trace::MapErrTrace; +use crate::libimagcalendar::store::EventStore; #[derive(Debug)] pub struct ParsedEventFLE<'a> { @@ -143,3 +145,46 @@ pub fn kairos_parse(spec: &str) -> Result { } } } + +pub fn find_event_by_id<'a>(store: &'a Store, id: &str, refconfig: &Config) -> Result>> { + if let Some(entry) = store.get_event_by_uid(id)? { + debug!("Found directly: {} -> {}", id, entry.get_location()); + return ParsedEventFLE::parse(entry, refconfig).map(Some) + } + + for sid in store.all_events()? { + let sid = sid?; + + let event = store.get(sid.clone())?.ok_or_else(|| { + format_err!("Cannot get {}, which should be there.", sid) + })?; + + trace!("Checking whether {} is represented by {}", id, event.get_location()); + let parsed = ParsedEventFLE::parse(event, refconfig)?; + + if parsed + .get_data() + .events() + .filter_map(|event| if event + .as_ref() + .map(|e| { + trace!("Checking whether {:?} starts with {}", e.uid(), id); + e.uid().map(|uid| uid.raw().starts_with(id)).unwrap_or(false) + }) + .unwrap_or(false) + { + trace!("Seems to be relevant"); + Some(event) + } else { + None + }) + .next() + .is_some() + { + return Ok(Some(parsed)) + } + } + + Ok(None) +} +