2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2018-02-07 01:48:53 +00:00
|
|
|
// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
|
2016-04-16 20:50:42 +00:00
|
|
|
#![deny(
|
|
|
|
non_camel_case_types,
|
|
|
|
non_snake_case,
|
|
|
|
path_statements,
|
|
|
|
trivial_numeric_casts,
|
|
|
|
unstable_features,
|
|
|
|
unused_allocation,
|
|
|
|
unused_import_braces,
|
|
|
|
unused_imports,
|
|
|
|
unused_must_use,
|
|
|
|
unused_mut,
|
|
|
|
unused_qualifications,
|
|
|
|
while_true,
|
|
|
|
)]
|
|
|
|
|
2016-02-20 14:46:06 +00:00
|
|
|
extern crate clap;
|
2016-02-20 19:51:32 +00:00
|
|
|
#[macro_use] extern crate log;
|
2017-10-08 10:55:41 +00:00
|
|
|
extern crate handlebars;
|
|
|
|
extern crate tempfile;
|
|
|
|
extern crate toml;
|
|
|
|
extern crate toml_query;
|
2016-02-20 19:51:32 +00:00
|
|
|
|
2017-02-04 10:34:17 +00:00
|
|
|
extern crate libimagentryview;
|
|
|
|
extern crate libimagerror;
|
2018-01-31 10:09:00 +00:00
|
|
|
#[macro_use] extern crate libimagrt;
|
2016-02-20 19:51:32 +00:00
|
|
|
extern crate libimagstore;
|
2018-04-19 19:55:27 +00:00
|
|
|
extern crate libimagutil;
|
2016-02-20 19:51:32 +00:00
|
|
|
|
2018-04-19 19:55:27 +00:00
|
|
|
use std::str::FromStr;
|
2017-10-08 10:55:41 +00:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::io::Write;
|
2018-04-06 13:40:34 +00:00
|
|
|
use std::io::Read;
|
2016-07-16 21:37:15 +00:00
|
|
|
use std::path::PathBuf;
|
2017-10-08 10:55:41 +00:00
|
|
|
use std::process::Command;
|
|
|
|
use std::process::exit;
|
|
|
|
|
|
|
|
use handlebars::Handlebars;
|
2018-01-12 15:29:36 +00:00
|
|
|
use toml_query::read::TomlValueReadTypeExt;
|
2016-02-20 19:51:32 +00:00
|
|
|
|
2016-05-18 17:06:05 +00:00
|
|
|
use libimagrt::setup::generate_runtime_setup;
|
2018-04-06 13:29:58 +00:00
|
|
|
use libimagrt::runtime::Runtime;
|
2018-02-12 20:25:33 +00:00
|
|
|
use libimagerror::str::ErrFromStr;
|
2017-10-08 10:55:41 +00:00
|
|
|
use libimagerror::trace::MapErrTrace;
|
2018-04-06 13:29:58 +00:00
|
|
|
use libimagerror::iter::TraceIterator;
|
2018-04-24 20:47:46 +00:00
|
|
|
use libimagerror::io::ToExitCode;
|
|
|
|
use libimagerror::exit::ExitUnwrap;
|
2016-07-16 20:53:12 +00:00
|
|
|
use libimagentryview::builtin::stdout::StdoutViewer;
|
2018-04-24 13:23:32 +00:00
|
|
|
use libimagentryview::builtin::md::MarkdownViewer;
|
2016-07-16 20:53:12 +00:00
|
|
|
use libimagentryview::viewer::Viewer;
|
2017-10-08 10:55:41 +00:00
|
|
|
use libimagentryview::error::ViewError as VE;
|
2018-04-06 13:29:58 +00:00
|
|
|
use libimagstore::storeid::IntoStoreId;
|
2018-05-01 15:42:47 +00:00
|
|
|
use libimagstore::storeid::StoreIdIterator;
|
2018-04-06 13:29:58 +00:00
|
|
|
use libimagstore::error::StoreError;
|
|
|
|
use libimagstore::iter::get::StoreIdGetIteratorExtension;
|
|
|
|
use libimagstore::store::FileLockEntry;
|
2016-02-20 14:46:06 +00:00
|
|
|
|
|
|
|
mod ui;
|
2016-02-20 19:51:32 +00:00
|
|
|
use ui::build_ui;
|
2016-02-20 14:46:06 +00:00
|
|
|
|
2016-02-18 16:19:56 +00:00
|
|
|
fn main() {
|
2018-01-31 10:09:00 +00:00
|
|
|
let version = make_imag_version!();
|
2016-05-18 17:06:05 +00:00
|
|
|
let rt = generate_runtime_setup( "imag-view",
|
2018-01-31 10:09:00 +00:00
|
|
|
&version,
|
2016-05-18 17:06:05 +00:00
|
|
|
"View entries (readonly)",
|
|
|
|
build_ui);
|
2016-02-20 19:51:32 +00:00
|
|
|
|
2018-04-06 13:29:58 +00:00
|
|
|
let entry_ids = entry_ids(&rt);
|
2016-07-16 21:37:15 +00:00
|
|
|
let view_header = rt.cli().is_present("view-header");
|
2018-02-01 19:36:49 +00:00
|
|
|
let hide_content = rt.cli().is_present("not-view-content");
|
2016-02-20 19:51:32 +00:00
|
|
|
|
2017-10-08 10:55:41 +00:00
|
|
|
if rt.cli().is_present("in") {
|
2018-04-06 13:29:58 +00:00
|
|
|
let files = entry_ids
|
|
|
|
.into_get_iter(rt.store())
|
2018-05-01 15:42:47 +00:00
|
|
|
.trace_unwrap_exit(1)
|
2018-04-06 13:29:58 +00:00
|
|
|
.map(|e| {
|
2018-05-01 15:42:47 +00:00
|
|
|
e.ok_or_else(|| String::from("Entry not found"))
|
2018-04-06 13:29:58 +00:00
|
|
|
.map_err(StoreError::from)
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
|
|
|
})
|
|
|
|
.map(|entry| create_tempfile_for(&entry, view_header, hide_content))
|
|
|
|
.collect::<Vec<_>>();
|
2017-10-08 10:55:41 +00:00
|
|
|
|
2018-04-06 13:29:58 +00:00
|
|
|
let mut command = {
|
|
|
|
let viewer = rt
|
|
|
|
.cli()
|
|
|
|
.value_of("in")
|
|
|
|
.ok_or_else::<VE, _>(|| "No viewer given".to_owned().into())
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-08 10:55:41 +00:00
|
|
|
|
2018-04-06 13:29:58 +00:00
|
|
|
let config = rt
|
|
|
|
.config()
|
|
|
|
.ok_or_else::<VE, _>(|| "No configuration, cannot continue".to_owned().into())
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
2018-01-12 15:29:36 +00:00
|
|
|
|
2018-04-06 13:29:58 +00:00
|
|
|
let query = format!("view.viewers.{}", viewer);
|
2018-01-12 15:29:36 +00:00
|
|
|
|
2018-04-06 13:29:58 +00:00
|
|
|
let viewer_template = config
|
|
|
|
.read_string(&query)
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
error!("Cannot find '{}' in config", query);
|
|
|
|
exit(1)
|
|
|
|
});
|
2017-10-08 10:55:41 +00:00
|
|
|
|
2018-04-06 13:29:58 +00:00
|
|
|
let mut handlebars = Handlebars::new();
|
|
|
|
handlebars.register_escape_fn(::handlebars::no_escape);
|
2017-10-08 10:55:41 +00:00
|
|
|
|
2018-04-06 13:29:58 +00:00
|
|
|
let _ = handlebars
|
|
|
|
.register_template_string("template", viewer_template)
|
|
|
|
.err_from_str()
|
|
|
|
.map_err(VE::from)
|
2018-01-12 15:29:36 +00:00
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-08 10:55:41 +00:00
|
|
|
|
2018-04-06 13:29:58 +00:00
|
|
|
let mut data = BTreeMap::new();
|
2017-10-08 10:55:41 +00:00
|
|
|
|
2018-04-06 13:29:58 +00:00
|
|
|
let file_paths = files
|
|
|
|
.iter()
|
|
|
|
.map(|&(_, ref path)| path.clone())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(" ");
|
2018-01-12 15:29:36 +00:00
|
|
|
|
2018-04-06 13:29:58 +00:00
|
|
|
data.insert("entries", file_paths);
|
2018-01-12 15:29:36 +00:00
|
|
|
|
2018-02-12 20:19:27 +00:00
|
|
|
let call = handlebars
|
|
|
|
.render("template", &data)
|
2018-02-12 20:25:33 +00:00
|
|
|
.err_from_str()
|
2018-02-12 20:19:27 +00:00
|
|
|
.map_err(VE::from)
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
2018-01-12 15:29:36 +00:00
|
|
|
let mut elems = call.split_whitespace();
|
|
|
|
let command_string = elems
|
|
|
|
.next()
|
|
|
|
.ok_or::<VE>("No command".to_owned().into())
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
|
|
|
let mut cmd = Command::new(command_string);
|
|
|
|
|
|
|
|
for arg in elems {
|
|
|
|
cmd.arg(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd
|
|
|
|
};
|
|
|
|
|
2018-04-06 13:29:58 +00:00
|
|
|
debug!("Calling: {:?}", command);
|
|
|
|
|
2018-02-12 20:19:27 +00:00
|
|
|
if !command
|
|
|
|
.status()
|
2018-02-12 20:25:33 +00:00
|
|
|
.err_from_str()
|
2018-02-12 20:19:27 +00:00
|
|
|
.map_err(VE::from)
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
|
|
|
.success()
|
|
|
|
{
|
2018-01-12 15:29:36 +00:00
|
|
|
exit(1)
|
2017-10-08 10:55:41 +00:00
|
|
|
}
|
2018-04-06 13:29:58 +00:00
|
|
|
|
|
|
|
drop(files);
|
2017-10-08 10:55:41 +00:00
|
|
|
} else {
|
2018-04-24 13:23:32 +00:00
|
|
|
let iter = entry_ids
|
2018-04-06 13:29:58 +00:00
|
|
|
.into_get_iter(rt.store())
|
|
|
|
.map(|e| {
|
|
|
|
e.map_err_trace_exit_unwrap(1)
|
2018-04-18 15:41:34 +00:00
|
|
|
.ok_or_else(|| String::from("Entry not found"))
|
2018-04-06 13:29:58 +00:00
|
|
|
.map_err(StoreError::from)
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
|
|
|
});
|
2018-04-24 13:23:32 +00:00
|
|
|
|
|
|
|
let out = rt.stdout();
|
|
|
|
let mut outlock = out.lock();
|
|
|
|
|
2018-04-24 20:47:46 +00:00
|
|
|
let basesep = if rt.cli().occurrences_of("seperator") != 0 { // checker for default value
|
|
|
|
rt.cli().value_of("seperator").map(String::from)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut sep_width = 80; // base width, automatically overridden by wrap width
|
|
|
|
|
|
|
|
// Helper to build the seperator with a base string `sep` and a `width`
|
|
|
|
let build_seperator = |sep: String, width: usize| -> String {
|
|
|
|
sep.repeat(width / sep.len())
|
|
|
|
};
|
|
|
|
|
2018-04-24 13:23:32 +00:00
|
|
|
if rt.cli().is_present("compile-md") {
|
2018-04-24 20:47:46 +00:00
|
|
|
let viewer = MarkdownViewer::new(&rt);
|
|
|
|
let seperator = basesep.map(|s| build_seperator(s, sep_width));
|
|
|
|
|
|
|
|
for (n, entry) in iter.enumerate() {
|
|
|
|
if n != 0 {
|
|
|
|
seperator
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| writeln!(outlock, "{}", s).to_exit_code().unwrap_or_exit());
|
|
|
|
}
|
|
|
|
|
|
|
|
viewer
|
|
|
|
.view_entry(&entry, &mut outlock)
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
2018-04-24 13:23:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut viewer = StdoutViewer::new(view_header, !hide_content);
|
|
|
|
|
|
|
|
if rt.cli().occurrences_of("autowrap") != 0 {
|
|
|
|
let width = rt.cli().value_of("autowrap").unwrap(); // ensured by clap
|
|
|
|
let width = usize::from_str(width).unwrap_or_else(|e| {
|
|
|
|
error!("Failed to parse argument to number: autowrap = {:?}",
|
|
|
|
rt.cli().value_of("autowrap").map(String::from));
|
|
|
|
error!("-> {:?}", e);
|
|
|
|
::std::process::exit(1)
|
|
|
|
});
|
|
|
|
|
2018-04-24 20:47:46 +00:00
|
|
|
// Copying this value over, so that the seperator has the right len as well
|
|
|
|
sep_width = width;
|
|
|
|
|
2018-04-24 13:23:32 +00:00
|
|
|
viewer.wrap_at(width);
|
|
|
|
}
|
|
|
|
|
2018-04-24 20:47:46 +00:00
|
|
|
let seperator = basesep.map(|s| build_seperator(s, sep_width));
|
|
|
|
for (n, entry) in iter.enumerate() {
|
|
|
|
if n != 0 {
|
|
|
|
seperator
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| writeln!(outlock, "{}", s).to_exit_code().unwrap_or_exit());
|
|
|
|
}
|
|
|
|
|
|
|
|
viewer
|
|
|
|
.view_entry(&entry, &mut outlock)
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
2018-04-24 13:23:32 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-06 13:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-01 15:42:47 +00:00
|
|
|
fn entry_ids(rt: &Runtime) -> StoreIdIterator {
|
2018-04-06 13:40:34 +00:00
|
|
|
match rt.cli().values_of("id") {
|
2018-05-01 15:42:47 +00:00
|
|
|
Some(p) => {
|
|
|
|
let pathes : Vec<String> = p.map(String::from).collect();
|
|
|
|
let iter = pathes.into_iter().map(PathBuf::from).map(PathBuf::into_storeid);
|
|
|
|
StoreIdIterator::new(Box::new(iter))
|
|
|
|
},
|
2018-04-06 13:40:34 +00:00
|
|
|
|
|
|
|
None => if rt.cli().is_present("entries-from-stdin") {
|
|
|
|
let stdin = rt.stdin().unwrap_or_else(|| {
|
|
|
|
error!("Cannot get handle to stdin");
|
|
|
|
::std::process::exit(1)
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut buf = String::new();
|
|
|
|
let _ = stdin.lock().read_to_string(&mut buf).unwrap_or_else(|_| {
|
|
|
|
error!("Failed to read from stdin");
|
|
|
|
::std::process::exit(1)
|
|
|
|
});
|
|
|
|
|
2018-05-01 15:42:47 +00:00
|
|
|
let lines : Vec<String> = buf.lines().map(String::from).collect();
|
|
|
|
let iter = lines.into_iter().map(PathBuf::from).map(PathBuf::into_storeid);
|
|
|
|
|
|
|
|
StoreIdIterator::new(Box::new(iter))
|
2018-04-06 13:40:34 +00:00
|
|
|
} else {
|
|
|
|
error!("Something weird happened. I was not able to find the path of the entries to edit");
|
|
|
|
::std::process::exit(1)
|
|
|
|
}
|
|
|
|
}
|
2018-04-06 13:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn create_tempfile_for<'a>(entry: &FileLockEntry<'a>, view_header: bool, hide_content: bool)
|
|
|
|
-> (tempfile::NamedTempFile, String)
|
|
|
|
{
|
|
|
|
let mut tmpfile = tempfile::NamedTempFile::new()
|
|
|
|
.err_from_str()
|
|
|
|
.map_err(VE::from)
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
|
|
|
|
|
|
|
if view_header {
|
|
|
|
let hdr = toml::ser::to_string_pretty(entry.get_header())
|
|
|
|
.err_from_str()
|
|
|
|
.map_err(VE::from)
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
|
|
|
let _ = tmpfile.write(format!("---\n{}---\n", hdr).as_bytes())
|
|
|
|
.err_from_str()
|
|
|
|
.map_err(VE::from)
|
2018-01-22 11:48:28 +00:00
|
|
|
.map_err_trace_exit_unwrap(1);
|
2016-02-24 13:10:31 +00:00
|
|
|
}
|
2018-04-06 13:29:58 +00:00
|
|
|
|
|
|
|
if !hide_content {
|
|
|
|
let _ = tmpfile.write(entry.get_content().as_bytes())
|
|
|
|
.err_from_str()
|
|
|
|
.map_err(VE::from)
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let file_path = tmpfile
|
|
|
|
.path()
|
|
|
|
.to_str()
|
|
|
|
.map(String::from)
|
|
|
|
.ok_or::<VE>("Cannot build path".to_owned().into())
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
|
|
|
|
|
|
|
(tmpfile, file_path)
|
2016-02-20 19:51:32 +00:00
|
|
|
}
|
|
|
|
|