2016-03-13 20:04:06 +00:00
|
|
|
use std::process::exit;
|
2016-01-25 19:09:48 +00:00
|
|
|
|
|
|
|
use clap::ArgMatches;
|
|
|
|
use toml::Value;
|
|
|
|
|
|
|
|
use libimagstore::store::FileLockEntry;
|
2016-03-13 19:49:26 +00:00
|
|
|
use libimagstore::storeid::build_entry_path;
|
2016-01-25 19:09:48 +00:00
|
|
|
use libimagrt::runtime::Runtime;
|
2016-05-16 16:59:02 +00:00
|
|
|
use libimagerror::trace::trace_error;
|
2016-01-25 19:09:48 +00:00
|
|
|
|
|
|
|
pub fn retrieve(rt: &Runtime) {
|
|
|
|
rt.cli()
|
|
|
|
.subcommand_matches("retrieve")
|
|
|
|
.map(|scmd| {
|
2016-03-13 20:04:06 +00:00
|
|
|
scmd.value_of("id")
|
|
|
|
.map(|id| {
|
|
|
|
let path = build_entry_path(rt.store(), id);
|
|
|
|
if path.is_err() {
|
2016-04-17 19:07:39 +00:00
|
|
|
trace_error(&path.unwrap_err());
|
2016-03-13 20:04:06 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
let path = path.unwrap();
|
|
|
|
debug!("path = {:?}", path);
|
2016-01-25 19:09:48 +00:00
|
|
|
|
2016-03-13 20:04:06 +00:00
|
|
|
rt.store()
|
|
|
|
// "id" must be present, enforced via clap spec
|
|
|
|
.retrieve(path)
|
|
|
|
.map(|e| print_entry(rt, scmd, e))
|
|
|
|
.map_err(|e| {
|
|
|
|
debug!("No entry.");
|
2016-03-25 18:08:52 +00:00
|
|
|
debug!("{}:", e);
|
|
|
|
trace_error(&e);
|
2016-03-13 20:04:06 +00:00
|
|
|
})
|
|
|
|
})
|
2016-01-25 19:09:48 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_entry(rt: &Runtime, scmd: &ArgMatches, e: FileLockEntry) {
|
|
|
|
if do_print_raw(scmd) {
|
|
|
|
debug!("Printing raw content...");
|
2016-04-21 11:23:01 +00:00
|
|
|
println!("{}", e.to_str());
|
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...");
|
|
|
|
// We have to Value::Table() for Display
|
2016-04-21 11:23:01 +00:00
|
|
|
println!("{}", Value::Table(e.get_header().clone().into()))
|
2016-01-25 19:09:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if do_print_content(scmd) {
|
|
|
|
debug!("Printing content...");
|
2016-04-21 11:23:01 +00:00
|
|
|
println!("{}", e.get_content());
|
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()
|
|
|
|
}
|
|
|
|
|