Merge branch 'libimagentryref-override-base' into master

This commit is contained in:
Matthias Beyer 2019-04-22 14:40:30 +02:00
commit 26664a5aec
3 changed files with 45 additions and 22 deletions

View file

@ -36,6 +36,7 @@
#[macro_use] extern crate log; #[macro_use] extern crate log;
extern crate clap; extern crate clap;
extern crate failure;
extern crate libimagstore; extern crate libimagstore;
#[macro_use] extern crate libimagrt; #[macro_use] extern crate libimagrt;
@ -50,6 +51,8 @@ use ui::build_ui;
use std::process::exit; use std::process::exit;
use std::io::Write; use std::io::Write;
use failure::Error;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::exit::ExitUnwrap; use libimagerror::exit::ExitUnwrap;
use libimagrt::setup::generate_runtime_setup; use libimagrt::setup::generate_runtime_setup;
@ -86,27 +89,30 @@ fn main() {
} }
fn deref(rt: &Runtime) { fn deref(rt: &Runtime) {
let cmd = rt.cli().subcommand_matches("deref").unwrap(); let cmd = rt.cli().subcommand_matches("deref").unwrap();
let ids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(); let basepath = cmd.value_of("override-basepath");
let cfg = get_ref_config(&rt, "imag-ref").map_err_trace_exit_unwrap(); let ids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap();
let out = rt.stdout(); let cfg = get_ref_config(&rt, "imag-ref").map_err_trace_exit_unwrap();
let out = rt.stdout();
let mut outlock = out.lock(); let mut outlock = out.lock();
ids.into_iter() ids.into_iter()
.for_each(|id| { .for_each(|id| {
match rt.store().get(id.clone()).map_err_trace_exit_unwrap() { match rt.store().get(id.clone()).map_err_trace_exit_unwrap() {
Some(entry) => { Some(entry) => {
entry let r_entry = entry.as_ref_with_hasher::<DefaultHasher>();
.as_ref_with_hasher::<DefaultHasher>()
.get_path(&cfg) if let Some(alternative_basepath) = basepath {
.map_err_trace_exit_unwrap() r_entry.get_path_with_basepath_setting(&cfg, alternative_basepath)
.to_str() } else {
.ok_or_else(|| { r_entry.get_path(&cfg)
error!("Could not transform path into string!"); }
exit(1) .map_err_trace_exit_unwrap()
}) .to_str()
.map(|s| writeln!(outlock, "{}", s)) .ok_or_else(|| ::libimagerror::errors::ErrorMsg::UTF8Error)
.ok(); // safe here because we exited already in the error case .map_err(Error::from)
.and_then(|s| writeln!(outlock, "{}", s).map_err(Error::from))
.map_err_trace_exit_unwrap();
let _ = rt.report_touched(&id).unwrap_or_exit(); let _ = rt.report_touched(&id).unwrap_or_exit();
}, },

View file

@ -45,6 +45,14 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.required(false) .required(false)
.multiple(false) .multiple(false)
.help("Ignore store entries which are not refs and do not print error message")) .help("Ignore store entries which are not refs and do not print error message"))
.arg(Arg::with_name("override-basepath")
.long("basepath-setting")
.short("B")
.takes_value(true)
.required(false)
.multiple(false)
.help("Override the basepath key to look up in the configuration"))
) )
.subcommand(SubCommand::with_name("remove") .subcommand(SubCommand::with_name("remove")

View file

@ -28,6 +28,7 @@ use libimagerror::errors::ErrorMsg as EM;
use toml::Value; use toml::Value;
use toml_query::read::TomlValueReadExt; use toml_query::read::TomlValueReadExt;
use toml_query::read::TomlValueReadTypeExt;
use toml_query::read::Partial; use toml_query::read::Partial;
use toml_query::delete::TomlValueDeleteExt; use toml_query::delete::TomlValueDeleteExt;
use toml_query::insert::TomlValueInsertExt; use toml_query::insert::TomlValueInsertExt;
@ -146,6 +147,9 @@ pub trait Ref {
fn get_path(&self, config: &Config) -> Result<PathBuf>; fn get_path(&self, config: &Config) -> Result<PathBuf>;
fn get_path_with_basepath_setting<B>(&self, config: &Config, base: B) -> Result<PathBuf>
where B: AsRef<str>;
fn get_relative_path(&self) -> Result<PathBuf>; fn get_relative_path(&self) -> Result<PathBuf>;
/// Get the stored hash. /// Get the stored hash.
@ -180,20 +184,25 @@ impl<'a, H: Hasher> Ref for RefWithHasher<'a, H> {
/// Get the path of the actual file /// Get the path of the actual file
fn get_path(&self, config: &Config) -> Result<PathBuf> { fn get_path(&self, config: &Config) -> Result<PathBuf> {
use toml_query::read::TomlValueReadTypeExt; let basepath_name = self.0
.get_header()
.read_string("ref.basepath")?
.ok_or_else(|| Error::from(EM::EntryHeaderFieldMissing("ref.basepath")))?;
self.get_path_with_basepath_setting(config, basepath_name)
}
fn get_path_with_basepath_setting<B>(&self, config: &Config, base: B)
-> Result<PathBuf>
where B: AsRef<str>
{
let relpath = self.0 let relpath = self.0
.get_header() .get_header()
.read_string("ref.relpath")? .read_string("ref.relpath")?
.map(PathBuf::from) .map(PathBuf::from)
.ok_or_else(|| Error::from(EM::EntryHeaderFieldMissing("ref.relpath")))?; .ok_or_else(|| Error::from(EM::EntryHeaderFieldMissing("ref.relpath")))?;
let basepath_name = self.0 get_file_path(config, base.as_ref(), relpath)
.get_header()
.read_string("ref.basepath")?
.ok_or_else(|| Error::from(EM::EntryHeaderFieldMissing("ref.basepath")))?;
get_file_path(config, &basepath_name, relpath)
} }
/// Get the relative path, relative to the configured basepath /// Get the relative path, relative to the configured basepath