Add basic listing functionality

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-10-03 12:54:44 +02:00
parent 61f71d67cc
commit 8dbb2f1590
4 changed files with 135 additions and 3 deletions

View file

@ -21,9 +21,10 @@ is-it-maintained-open-issues = { repository = "matthiasbeyer/imag" }
maintenance = { status = "actively-developed" } maintenance = { status = "actively-developed" }
[dependencies] [dependencies]
log = "0.4" log = "0.4"
failure = "0.1" failure = "0.1"
walkdir = "2.2.8" walkdir = "2.2.8"
vobject = "0.7"
libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" } libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" }
libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" } libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" }

View file

@ -47,8 +47,11 @@ extern crate libimagstore;
extern crate libimagutil; extern crate libimagutil;
use std::path::PathBuf; use std::path::PathBuf;
use std::result::Result as RResult;
use std::io::Write;
use failure::Error; use failure::Error;
use failure::err_msg;
use failure::Fallible as Result; use failure::Fallible as Result;
use toml_query::read::Partial; use toml_query::read::Partial;
use toml_query::read::TomlValueReadExt; use toml_query::read::TomlValueReadExt;
@ -56,6 +59,7 @@ use walkdir::DirEntry;
use walkdir::WalkDir; use walkdir::WalkDir;
use libimagcalendar::store::EventStore; use libimagcalendar::store::EventStore;
use libimagerror::io::ToExitCode;
use libimagerror::exit::ExitUnwrap; use libimagerror::exit::ExitUnwrap;
use libimagerror::iter::TraceIterator; use libimagerror::iter::TraceIterator;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
@ -63,6 +67,7 @@ use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup; use libimagrt::setup::generate_runtime_setup;
mod ui; mod ui;
mod util;
fn main() { fn main() {
let version = make_imag_version!(); let version = make_imag_version!();
@ -76,6 +81,7 @@ fn main() {
debug!("Call {}", name); debug!("Call {}", name);
match name { match name {
"import" => import(&rt), "import" => import(&rt),
"list" => list(&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");
@ -154,6 +160,66 @@ fn import(rt: &Runtime) {
.for_each(|fle| rt.report_touched(fle.get_location()).unwrap_or_exit()); .for_each(|fle| rt.report_touched(fle.get_location()).unwrap_or_exit());
} }
fn list(rt: &Runtime) {
use util::*;
let scmd = rt.cli().subcommand_matches("list").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 event_filter = |pefle: &ParsedEventFLE| true; // TODO: impl filtering
rt.store()
.all_events()
.map_err_trace_exit_unwrap()
.trace_unwrap_exit()
.map(|sid| rt.store().get(sid))
.trace_unwrap_exit()
.map(|oe| oe.ok_or_else(|| err_msg("Missing entry while calling all_events()")))
.trace_unwrap_exit()
.map(|ev| ParsedEventFLE::parse(ev, &ref_config))
.trace_unwrap_exit()
.filter(|e| event_filter(e))
.for_each(|parsed_event| {
for event in parsed_event.get_data().events().filter_map(RResult::ok) {
macro_rules! get_data {
($t:expr, $text:expr) => {
($t).map(|obj| obj.into_raw()).unwrap_or_else(|| String::from($text))
}
}
let summary = get_data!(event.summary(), "<no summary>");
let uid = get_data!(event.uid(), "<no uid>");
let description = get_data!(event.description(), "<no description>");
let dtstart = get_data!(event.dtstart(), "<no start date>");
let dtend = get_data!(event.dtend(), "<no end date>");
let location = get_data!(event.location(), "<no location>");
let summary_underline = std::iter::repeat('-').take(summary.len()).collect::<String>();
writeln!(rt.stdout(),
"{summary}\n{summary_underline}\n\n{uid}\n{description}\n{dtstart}\n{dtend}\n{location}\n\n",
summary = summary,
summary_underline = summary_underline,
uid = uid,
description = description,
dtstart = dtstart,
dtend = dtend,
location = location)
.to_exit_code()
.unwrap_or_exit();
}
rt.report_touched(parsed_event.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

@ -56,6 +56,11 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.multiple(false) .multiple(false)
.help("Override if entry for event already exists")) .help("Override if entry for event already exists"))
) )
.subcommand(SubCommand::with_name("list")
.about("List calendar entries")
.version("0.1")
)
} }
fn import_validator<A: AsRef<str>>(s: A) -> Result<(), String> { fn import_validator<A: AsRef<str>>(s: A) -> Result<(), String> {

View file

@ -0,0 +1,60 @@
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> 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 vobject::icalendar::ICalendar;
use failure::Fallible as Result;
use failure::Error;
use libimagstore::store::FileLockEntry;
use libimagentryref::reference::fassade::RefFassade;
use libimagentryref::reference::Ref;
use libimagentryref::reference::Config;
use libimagentryref::hasher::default::DefaultHasher;
pub struct ParsedEventFLE<'a> {
inner: FileLockEntry<'a>,
data: ICalendar,
}
impl<'a> ParsedEventFLE<'a> {
/// Because libimagcalendar only links to the actual calendar data, we need to read the data and
/// parse it.
/// With this function, a FileLockEntry can be parsed to a ParsedEventFileLockEntry
/// (ParsedEventFLE).
pub fn parse(fle: FileLockEntry<'a>, refconfig: &Config) -> Result<Self> {
fle.as_ref_with_hasher::<DefaultHasher>()
.get_path(refconfig)
.and_then(|p| ::std::fs::read_to_string(p).map_err(Error::from))
.and_then(|s| ICalendar::build(&s).map_err(Error::from))
.map(|cal| ParsedEventFLE {
inner: fle,
data: cal,
})
}
pub fn get_entry(&self) -> &FileLockEntry<'a> {
&self.inner
}
pub fn get_data(&self) -> &ICalendar {
&self.data
}
}