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-08-26 10:52:57 +00:00
|
|
|
use std::path::PathBuf;
|
2018-02-14 22:45:28 +00:00
|
|
|
use std::io::Write;
|
2016-08-26 10:52:57 +00:00
|
|
|
|
2016-01-25 19:09:48 +00:00
|
|
|
use clap::ArgMatches;
|
|
|
|
|
|
|
|
use libimagstore::store::FileLockEntry;
|
2016-08-26 10:52:57 +00:00
|
|
|
use libimagstore::storeid::StoreId;
|
2016-01-25 19:09:48 +00:00
|
|
|
use libimagrt::runtime::Runtime;
|
2016-09-06 09:27:42 +00:00
|
|
|
use libimagerror::trace::MapErrTrace;
|
2018-02-14 22:45:28 +00:00
|
|
|
use libimagerror::io::ToExitCode;
|
|
|
|
use libimagerror::exit::ExitUnwrap;
|
2016-09-06 09:27:42 +00:00
|
|
|
use libimagutil::debug_result::*;
|
2016-01-25 19:09:48 +00:00
|
|
|
|
|
|
|
pub fn retrieve(rt: &Runtime) {
|
|
|
|
rt.cli()
|
|
|
|
.subcommand_matches("retrieve")
|
|
|
|
.map(|scmd| {
|
2017-09-04 14:42:31 +00:00
|
|
|
// unwrap() is safe as arg is required
|
|
|
|
let id = scmd.value_of("id").unwrap();
|
|
|
|
let path = PathBuf::from(id);
|
|
|
|
let store = Some(rt.store().path().clone());
|
2018-01-22 11:48:28 +00:00
|
|
|
let path = StoreId::new(store, path).map_err_trace_exit_unwrap(1);
|
2017-09-04 14:42:31 +00:00
|
|
|
debug!("path = {:?}", path);
|
2016-01-25 19:09:48 +00:00
|
|
|
|
2017-09-04 14:42:31 +00:00
|
|
|
rt.store()
|
|
|
|
.retrieve(path)
|
|
|
|
.map(|e| print_entry(rt, scmd, e))
|
|
|
|
.map_dbg_str("No entry")
|
|
|
|
.map_dbg(|e| format!("{:?}", e))
|
|
|
|
.map_err_trace()
|
2016-01-25 19:09:48 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-27 09:26:06 +00:00
|
|
|
pub fn print_entry(rt: &Runtime, scmd: &ArgMatches, e: FileLockEntry) {
|
2016-01-25 19:09:48 +00:00
|
|
|
if do_print_raw(scmd) {
|
|
|
|
debug!("Printing raw content...");
|
2018-03-23 14:39:52 +00:00
|
|
|
let _ = writeln!(rt.stdout(), "{}", e.to_str().map_err_trace_exit_unwrap(1))
|
2018-02-14 22:45:28 +00:00
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
2016-01-25 19:09:48 +00:00
|
|
|
} else if do_filter(scmd) {
|
|
|
|
debug!("Filtering...");
|
|
|
|
warn!("Filtering via header specs is currently now supported.");
|
|
|
|
warn!("Will fail now!");
|
|
|
|
unimplemented!()
|
|
|
|
} else {
|
|
|
|
debug!("Printing structured...");
|
|
|
|
if do_print_header(scmd) {
|
|
|
|
debug!("Printing header...");
|
|
|
|
if do_print_header_as_json(rt.cli()) {
|
|
|
|
debug!("Printing header as json...");
|
|
|
|
warn!("Printing as JSON currently not supported.");
|
|
|
|
warn!("Will fail now!");
|
|
|
|
unimplemented!()
|
|
|
|
} else {
|
|
|
|
debug!("Printing header as TOML...");
|
2018-02-26 21:57:17 +00:00
|
|
|
let _ = writeln!(rt.stdout(), "{}", e.get_header())
|
2018-02-14 22:45:28 +00:00
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
2016-01-25 19:09:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if do_print_content(scmd) {
|
|
|
|
debug!("Printing content...");
|
2018-02-26 21:57:17 +00:00
|
|
|
let _ = writeln!(rt.stdout(), "{}", e.get_content())
|
2018-02-14 22:45:28 +00:00
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
2016-01-25 19:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_print_header(m: &ArgMatches) -> bool {
|
|
|
|
m.is_present("header")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_print_header_as_json(m: &ArgMatches) -> bool {
|
|
|
|
m.is_present("header-json")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_print_content(m: &ArgMatches) -> bool {
|
|
|
|
m.is_present("content")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_print_raw(m: &ArgMatches) -> bool {
|
|
|
|
m.is_present("raw")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_filter(m: &ArgMatches) -> bool {
|
|
|
|
m.subcommand_matches("filter-header").is_some()
|
|
|
|
}
|
|
|
|
|