Merge branch 'libimagcontact-force-override-ref-data' into master

This commit is contained in:
Matthias Beyer 2019-04-22 14:40:19 +02:00
commit c927eb49f9
3 changed files with 42 additions and 13 deletions

View file

@ -158,6 +158,7 @@ fn list(rt: &Runtime) {
fn import(rt: &Runtime) { fn import(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("import").unwrap(); // secured by main let scmd = rt.cli().subcommand_matches("import").unwrap(); // secured by main
let force_override = scmd.is_present("force-override");
let path = scmd.value_of("path").map(PathBuf::from).unwrap(); // secured by clap let path = scmd.value_of("path").map(PathBuf::from).unwrap(); // secured by clap
let collection_name = rt.cli().value_of("contact-ref-collection-name").unwrap(); // default by clap let collection_name = rt.cli().value_of("contact-ref-collection-name").unwrap(); // default by clap
@ -180,7 +181,7 @@ fn import(rt: &Runtime) {
if path.is_file() { if path.is_file() {
let entry = rt let entry = rt
.store() .store()
.retrieve_from_path(&path, &ref_config, &collection_name) .retrieve_from_path(&path, &ref_config, &collection_name, force_override)
.map_err_trace_exit_unwrap(); .map_err_trace_exit_unwrap();
let _ = rt.report_touched(entry.get_location()).unwrap_or_exit(); let _ = rt.report_touched(entry.get_location()).unwrap_or_exit();
@ -194,7 +195,7 @@ fn import(rt: &Runtime) {
let pb = PathBuf::from(entry.path()); let pb = PathBuf::from(entry.path());
let fle = rt let fle = rt
.store() .store()
.retrieve_from_path(&pb, &ref_config, &collection_name) .retrieve_from_path(&pb, &ref_config, &collection_name, force_override)
.map_err_trace_exit_unwrap(); .map_err_trace_exit_unwrap();
let _ = rt.report_touched(fle.get_location()).unwrap_or_exit(); let _ = rt.report_touched(fle.get_location()).unwrap_or_exit();

View file

@ -65,6 +65,12 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.value_name("PATH") .value_name("PATH")
.help("Import from this file/directory")) .help("Import from this file/directory"))
.arg(Arg::with_name("force-override")
.long("force")
.takes_value(false)
.required(false)
.multiple(false)
.help("Force to override existing entries"))
) )
.subcommand(SubCommand::with_name("show") .subcommand(SubCommand::with_name("show")

View file

@ -45,7 +45,11 @@ pub trait ContactStore<'a> {
-> Result<FileLockEntry<'a>> -> Result<FileLockEntry<'a>>
where CN: AsRef<str>; where CN: AsRef<str>;
fn retrieve_from_path<CN>(&'a self, p: &PathBuf, rc: &RefConfig, collection_name: CN) fn retrieve_from_path<CN>(&'a self,
p: &PathBuf,
rc: &RefConfig,
collection_name: CN,
force: bool)
-> Result<FileLockEntry<'a>> -> Result<FileLockEntry<'a>>
where CN: AsRef<str>; where CN: AsRef<str>;
@ -54,7 +58,12 @@ pub trait ContactStore<'a> {
where CN: AsRef<str>, where CN: AsRef<str>,
P: AsRef<Path>; P: AsRef<Path>;
fn retrieve_from_buf<CN, P>(&'a self, buf: &str, path: P, rc: &RefConfig, collection_name: CN) fn retrieve_from_buf<CN, P>(&'a self,
buf: &str,
path: P,
rc: &RefConfig,
collection_name: CN,
force: bool)
-> Result<FileLockEntry<'a>> -> Result<FileLockEntry<'a>>
where CN: AsRef<str>, where CN: AsRef<str>,
P: AsRef<Path>; P: AsRef<Path>;
@ -82,11 +91,12 @@ impl<'a> ContactStore<'a> for Store {
/// ///
/// Uses the collection with `collection_name` from RefConfig to store the reference to the /// Uses the collection with `collection_name` from RefConfig to store the reference to the
/// file. /// file.
fn retrieve_from_path<CN>(&'a self, p: &PathBuf, rc: &RefConfig, collection_name: CN) fn retrieve_from_path<CN>(&'a self, p: &PathBuf, rc: &RefConfig, collection_name: CN, force: bool)
-> Result<FileLockEntry<'a>> -> Result<FileLockEntry<'a>>
where CN: AsRef<str> where CN: AsRef<str>
{ {
util::read_to_string(p).and_then(|buf| self.retrieve_from_buf(&buf, p, rc, collection_name)) util::read_to_string(p)
.and_then(|buf| self.retrieve_from_buf(&buf, p, rc, collection_name, force))
} }
/// Create a contact from a buffer /// Create a contact from a buffer
@ -96,13 +106,19 @@ impl<'a> ContactStore<'a> for Store {
/// ///
/// Needs the `path` passed where the buffer was read from, because we want to create a /// Needs the `path` passed where the buffer was read from, because we want to create a
/// reference to it. /// reference to it.
///
/// # Note
///
/// This does _never_ force-override existing reference data, thus the `force` parameter of
/// `postprocess_fetched_entry()` is hardcoded to `false`.
///
fn create_from_buf<CN, P>(&'a self, buf: &str, path: P, rc: &RefConfig, collection_name: CN) fn create_from_buf<CN, P>(&'a self, buf: &str, path: P, rc: &RefConfig, collection_name: CN)
-> Result<FileLockEntry<'a>> -> Result<FileLockEntry<'a>>
where CN: AsRef<str>, where CN: AsRef<str>,
P: AsRef<Path> P: AsRef<Path>
{ {
let (sid, value) = prepare_fetching_from_store(buf)?; let (sid, value) = prepare_fetching_from_store(buf)?;
postprocess_fetched_entry(self.create(sid)?, value, path, rc, collection_name) postprocess_fetched_entry(self.create(sid)?, value, path, rc, collection_name, false)
} }
/// Retrieve a contact from a buffer /// Retrieve a contact from a buffer
@ -112,13 +128,18 @@ impl<'a> ContactStore<'a> for Store {
/// ///
/// Needs the `path` passed where the buffer was read from, because we want to create a /// Needs the `path` passed where the buffer was read from, because we want to create a
/// reference to it. /// reference to it.
fn retrieve_from_buf<CN, P>(&'a self, buf: &str, path: P, rc: &RefConfig, collection_name: CN) fn retrieve_from_buf<CN, P>(&'a self,
buf: &str,
path: P,
rc: &RefConfig,
collection_name: CN,
force: bool)
-> Result<FileLockEntry<'a>> -> Result<FileLockEntry<'a>>
where CN: AsRef<str>, where CN: AsRef<str>,
P: AsRef<Path> P: AsRef<Path>
{ {
let (sid, value) = prepare_fetching_from_store(buf)?; let (sid, value) = prepare_fetching_from_store(buf)?;
postprocess_fetched_entry(self.retrieve(sid)?, value, path, rc, collection_name) postprocess_fetched_entry(self.retrieve(sid)?, value, path, rc, collection_name, force)
} }
fn all_contacts(&'a self) -> Result<Entries<'a>> { fn all_contacts(&'a self) -> Result<Entries<'a>> {
@ -159,7 +180,8 @@ fn postprocess_fetched_entry<'a, CN, P>(mut entry: FileLockEntry<'a>,
value: Value, value: Value,
path: P, path: P,
rc: &RefConfig, rc: &RefConfig,
collection_name: CN) collection_name: CN,
force: bool)
-> Result<FileLockEntry<'a>> -> Result<FileLockEntry<'a>>
where CN: AsRef<str>, where CN: AsRef<str>,
P: AsRef<Path> P: AsRef<Path>
@ -169,7 +191,7 @@ fn postprocess_fetched_entry<'a, CN, P>(mut entry: FileLockEntry<'a>,
entry.set_isflag::<IsContact>()?; entry.set_isflag::<IsContact>()?;
entry.get_header_mut().insert("contact.data", value)?; entry.get_header_mut().insert("contact.data", value)?;
entry.as_ref_with_hasher_mut::<Sha1Hasher>().make_ref(path, collection_name, rc, false)?; entry.as_ref_with_hasher_mut::<Sha1Hasher>().make_ref(path, collection_name, rc, force)?;
Ok(entry) Ok(entry)
} }