Implement show subcommand

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-10-03 19:33:56 +02:00
parent d40bf2956f
commit f1ec71431e
3 changed files with 116 additions and 0 deletions

View file

@ -87,6 +87,7 @@ fn main() {
match name { match name {
"import" => import(&rt), "import" => import(&rt),
"list" => list(&rt), "list" => list(&rt),
"show" => show(&rt),
other => { other => {
warn!("Right now, only the 'import' command is available"); warn!("Right now, only the 'import' command is available");
debug!("Unknown command"); 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::<libimagentryref::reference::Config>()
.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) /// helper function to check whether a DirEntry points to something hidden (starting with dot)
fn is_not_hidden(entry: &DirEntry) -> bool { fn is_not_hidden(entry: &DirEntry) -> bool {
!entry.file_name().to_str().map(|s| s.starts_with(".")).unwrap_or(false) !entry.file_name().to_str().map(|s| s.starts_with(".")).unwrap_or(false)

View file

@ -89,6 +89,25 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.multiple(false) .multiple(false)
.help("List events which are dated after certain date")) .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<A: AsRef<str>>(s: A) -> Result<(), String> { fn import_validator<A: AsRef<str>>(s: A) -> Result<(), String> {

View file

@ -31,11 +31,13 @@ use chrono::NaiveDateTime;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::store::Store;
use libimagentryref::reference::fassade::RefFassade; use libimagentryref::reference::fassade::RefFassade;
use libimagentryref::reference::Ref; use libimagentryref::reference::Ref;
use libimagentryref::reference::Config; use libimagentryref::reference::Config;
use libimagentryref::hasher::default::DefaultHasher; use libimagentryref::hasher::default::DefaultHasher;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use crate::libimagcalendar::store::EventStore;
#[derive(Debug)] #[derive(Debug)]
pub struct ParsedEventFLE<'a> { pub struct ParsedEventFLE<'a> {
@ -143,3 +145,46 @@ pub fn kairos_parse(spec: &str) -> Result<NaiveDateTime> {
} }
} }
} }
pub fn find_event_by_id<'a>(store: &'a Store, id: &str, refconfig: &Config) -> Result<Option<ParsedEventFLE<'a>>> {
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)
}