2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
|
|
|
// Copyright (C) 2015, 2016 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
|
|
|
|
//
|
|
|
|
|
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;
|
2016-02-20 19:51:32 +00:00
|
|
|
extern crate libimagrt;
|
|
|
|
extern crate libimagstore;
|
|
|
|
|
2017-10-08 10:55:41 +00:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::io::Write;
|
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;
|
|
|
|
use toml_query::read::TomlValueReadExt;
|
|
|
|
use toml::Value;
|
2016-02-20 19:51:32 +00:00
|
|
|
|
2016-05-18 17:06:05 +00:00
|
|
|
use libimagrt::setup::generate_runtime_setup;
|
2016-07-30 17:12:29 +00:00
|
|
|
use libimagerror::trace::trace_error_exit;
|
2017-10-08 10:55:41 +00:00
|
|
|
use libimagerror::trace::MapErrTrace;
|
2016-07-16 20:53:12 +00:00
|
|
|
use libimagentryview::builtin::stdout::StdoutViewer;
|
|
|
|
use libimagentryview::viewer::Viewer;
|
2017-10-08 10:55:41 +00:00
|
|
|
use libimagentryview::error::ViewError as VE;
|
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() {
|
2016-05-18 17:06:05 +00:00
|
|
|
let rt = generate_runtime_setup( "imag-view",
|
2018-01-11 21:22:26 +00:00
|
|
|
env!("CARGO_PKG_VERSION"),
|
2016-05-18 17:06:05 +00:00
|
|
|
"View entries (readonly)",
|
|
|
|
build_ui);
|
2016-02-20 19:51:32 +00:00
|
|
|
|
2016-07-16 21:37:15 +00:00
|
|
|
let entry_id = rt.cli().value_of("id").unwrap(); // enforced by clap
|
|
|
|
let view_header = rt.cli().is_present("view-header");
|
|
|
|
let view_content = rt.cli().is_present("view-content");
|
2016-02-20 19:51:32 +00:00
|
|
|
|
2016-07-26 13:36:49 +00:00
|
|
|
let entry = match rt.store().get(PathBuf::from(entry_id)) {
|
|
|
|
Ok(Some(fle)) => fle,
|
|
|
|
Ok(None) => {
|
|
|
|
error!("Cannot get {}, there is no such id in the store", entry_id);
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-07-16 21:37:15 +00:00
|
|
|
Err(e) => {
|
2016-07-30 17:12:29 +00:00
|
|
|
trace_error_exit(&e, 1);
|
2016-03-11 15:16:35 +00:00
|
|
|
}
|
2016-02-24 12:45:37 +00:00
|
|
|
};
|
|
|
|
|
2017-10-08 10:55:41 +00:00
|
|
|
if rt.cli().is_present("in") {
|
|
|
|
let viewer = rt
|
|
|
|
.cli()
|
|
|
|
.value_of("in")
|
|
|
|
.ok_or_else::<VE, _>(|| "No viewer given".to_owned().into())
|
2017-10-12 17:08:36 +00:00
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-08 10:55:41 +00:00
|
|
|
|
|
|
|
let config = rt
|
|
|
|
.config()
|
|
|
|
.ok_or_else::<VE, _>(|| "No configuration, cannot continue".to_owned().into())
|
2017-10-12 17:08:36 +00:00
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-08 10:55:41 +00:00
|
|
|
|
|
|
|
let query = format!("view.viewers.{}", viewer);
|
2017-10-28 17:19:46 +00:00
|
|
|
match config.read(&query) {
|
2017-10-08 10:55:41 +00:00
|
|
|
Err(e) => trace_error_exit(&e, 1),
|
|
|
|
Ok(None) => {
|
|
|
|
error!("Cannot find '{}' in config", query);
|
|
|
|
exit(1)
|
2016-09-21 15:07:23 +00:00
|
|
|
},
|
2017-10-08 10:55:41 +00:00
|
|
|
|
|
|
|
Ok(Some(&Value::String(ref viewer_template))) => {
|
|
|
|
let mut handlebars = Handlebars::new();
|
|
|
|
handlebars.register_escape_fn(::handlebars::no_escape);
|
|
|
|
|
|
|
|
let _ = handlebars.register_template_string("template", viewer_template)
|
2017-10-12 17:08:36 +00:00
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-08 10:55:41 +00:00
|
|
|
|
|
|
|
let file = {
|
|
|
|
let mut tmpfile = tempfile::NamedTempFile::new()
|
2017-10-12 17:08:36 +00:00
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-08 10:55:41 +00:00
|
|
|
if view_header {
|
|
|
|
let hdr = toml::ser::to_string_pretty(entry.get_header())
|
2017-10-12 17:08:36 +00:00
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-08 10:55:41 +00:00
|
|
|
let _ = tmpfile.write(format!("---\n{}---\n", hdr).as_bytes())
|
2017-10-12 17:08:36 +00:00
|
|
|
.map_err_trace_exit_unwrap(1);
|
2016-09-21 15:07:23 +00:00
|
|
|
}
|
2016-02-24 13:10:31 +00:00
|
|
|
|
2017-10-08 10:55:41 +00:00
|
|
|
if view_content {
|
|
|
|
let _ = tmpfile.write(entry.get_content().as_bytes())
|
2017-10-12 17:08:36 +00:00
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-08 10:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpfile
|
|
|
|
};
|
|
|
|
|
|
|
|
let file_path = file
|
|
|
|
.path()
|
|
|
|
.to_str()
|
|
|
|
.map(String::from)
|
|
|
|
.ok_or::<VE>("Cannot build path".to_owned().into())
|
2017-10-12 17:08:36 +00:00
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-08 10:55:41 +00:00
|
|
|
|
|
|
|
let mut command = {
|
|
|
|
let mut data = BTreeMap::new();
|
|
|
|
data.insert("entry", file_path);
|
|
|
|
|
2017-10-12 17:08:36 +00:00
|
|
|
let call = handlebars.render("template", &data).map_err_trace_exit_unwrap(1);
|
2017-10-08 10:55:41 +00:00
|
|
|
let mut elems = call.split_whitespace();
|
|
|
|
let command_string = elems
|
|
|
|
.next()
|
|
|
|
.ok_or::<VE>("No command".to_owned().into())
|
2017-10-12 17:08:36 +00:00
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-08 10:55:41 +00:00
|
|
|
let mut cmd = Command::new(command_string);
|
|
|
|
|
|
|
|
for arg in elems {
|
|
|
|
cmd.arg(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd
|
|
|
|
};
|
2016-02-24 13:10:31 +00:00
|
|
|
|
2017-10-12 17:08:36 +00:00
|
|
|
if !command.status().map_err_trace_exit_unwrap(1).success() {
|
2017-10-08 10:55:41 +00:00
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Ok(Some(_)) => {
|
|
|
|
error!("Type error: Expected String at {}, found non-string", query);
|
|
|
|
exit(1)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let _ = StdoutViewer::new(view_header, view_content)
|
|
|
|
.view_entry(&entry)
|
|
|
|
.map_err_trace_exit(1);
|
2016-02-24 13:10:31 +00:00
|
|
|
}
|
2016-02-20 19:51:32 +00:00
|
|
|
}
|
|
|
|
|