Merge pull request #1312 from matthiasbeyer/libimagentrylink/unlink
libimagentrylink: unlink
This commit is contained in:
commit
9c63c82063
3 changed files with 50 additions and 0 deletions
|
@ -52,6 +52,7 @@ extern crate libimagutil;
|
|||
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::process::exit;
|
||||
|
||||
use libimagentrylink::external::ExternalLinker;
|
||||
use libimagentrylink::internal::InternalLinker;
|
||||
|
@ -99,6 +100,7 @@ fn main() {
|
|||
.map(|name| {
|
||||
match name {
|
||||
"remove" => remove_linking(&rt),
|
||||
"unlink" => unlink(&rt),
|
||||
"list" => list_linkings(&rt),
|
||||
_ => panic!("BUG"),
|
||||
}
|
||||
|
@ -224,6 +226,28 @@ fn remove_linking(rt: &Runtime) {
|
|||
});
|
||||
}
|
||||
|
||||
fn unlink(rt: &Runtime) {
|
||||
use libimagerror::iter::TraceIterator;
|
||||
use libimagstore::iter::get::StoreIdGetIteratorExtension;
|
||||
|
||||
let _ = rt
|
||||
.cli()
|
||||
.subcommand_matches("unlink")
|
||||
.unwrap() // checked in main()
|
||||
.values_of("from")
|
||||
.unwrap() // checked by clap
|
||||
.map(PathBuf::from)
|
||||
.collect::<Vec<PathBuf>>().into_iter() // for lifetime inference
|
||||
.map(StoreId::new_baseless)
|
||||
.unwrap_with(|e| { trace_error(&e); exit(1) })
|
||||
.into_get_iter(rt.store())
|
||||
.unwrap_with(|e| { trace_error(&e); exit(1) })
|
||||
.filter_map(|e| e)
|
||||
.map(|mut entry| entry.unlink(rt.store()))
|
||||
.unwrap_with(|e| { trace_error(&e); exit(1) })
|
||||
.collect::<Vec<_>>();
|
||||
}
|
||||
|
||||
fn list_linkings(rt: &Runtime) {
|
||||
let cmd = rt.cli()
|
||||
.subcommand_matches("list")
|
||||
|
|
|
@ -39,6 +39,17 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
|
|||
.help("Remove links to these entries")
|
||||
.value_name("ENTRIES"))
|
||||
)
|
||||
.subcommand(SubCommand::with_name("unlink")
|
||||
.about("Remove all links from an entry")
|
||||
.version("0.1")
|
||||
.arg(Arg::with_name("from")
|
||||
.index(1)
|
||||
.takes_value(true)
|
||||
.required(true)
|
||||
.multiple(true)
|
||||
.help("Remove links from these entries")
|
||||
.value_name("ENTRY"))
|
||||
)
|
||||
|
||||
.subcommand(SubCommand::with_name("list")
|
||||
.about("List links to this entry")
|
||||
|
|
|
@ -24,6 +24,7 @@ use std::path::PathBuf;
|
|||
use libimagstore::storeid::StoreId;
|
||||
use libimagstore::storeid::IntoStoreId;
|
||||
use libimagstore::store::Entry;
|
||||
use libimagstore::store::Store;
|
||||
use libimagstore::store::Result as StoreResult;
|
||||
|
||||
use toml_query::read::TomlValueReadExt;
|
||||
|
@ -178,6 +179,9 @@ pub trait InternalLinker {
|
|||
/// Remove an internal link from the implementor object
|
||||
fn remove_internal_link(&mut self, link: &mut Entry) -> Result<()>;
|
||||
|
||||
/// Remove _all_ internal links
|
||||
fn unlink(&mut self, store: &Store) -> Result<()>;
|
||||
|
||||
/// Add internal annotated link
|
||||
fn add_internal_annotated_link(&mut self, link: &mut Entry, annotation: String) -> Result<()>;
|
||||
}
|
||||
|
@ -452,6 +456,17 @@ impl InternalLinker for Entry {
|
|||
})
|
||||
}
|
||||
|
||||
fn unlink(&mut self, store: &Store) -> Result<()> {
|
||||
for id in self.get_internal_links()?.map(|l| l.get_store_id().clone()) {
|
||||
match store.get(id).map_err(LE::from)? {
|
||||
Some(mut entry) => self.remove_internal_link(&mut entry)?,
|
||||
None => return Err(LEK::LinkTargetDoesNotExist.into()),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn add_internal_annotated_link(&mut self, link: &mut Entry, annotation: String) -> Result<()> {
|
||||
let new_link = Link::Annotated {
|
||||
link: link.get_location().clone(),
|
||||
|
|
Loading…
Reference in a new issue