Remove calls to exit() and replace them with error propagation up to main()
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
parent
ab39aa9353
commit
e740b2faaa
2 changed files with 33 additions and 61 deletions
|
@ -20,7 +20,7 @@ is-it-maintained-open-issues = { repository = "matthiasbeyer/imag" }
|
||||||
maintenance = { status = "actively-developed" }
|
maintenance = { status = "actively-developed" }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.4.6"
|
log = "0.4.6"
|
||||||
failure = "0.1.5"
|
failure = "0.1.5"
|
||||||
|
|
||||||
libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" }
|
libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" }
|
||||||
|
|
|
@ -35,33 +35,29 @@
|
||||||
)]
|
)]
|
||||||
|
|
||||||
#[macro_use] extern crate log;
|
#[macro_use] extern crate log;
|
||||||
|
#[macro_use] extern crate failure;
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
extern crate failure;
|
|
||||||
|
|
||||||
extern crate libimagrt;
|
extern crate libimagrt;
|
||||||
extern crate libimagstore;
|
extern crate libimagstore;
|
||||||
extern crate libimagerror;
|
extern crate libimagerror;
|
||||||
extern crate libimagentrylink;
|
extern crate libimagentrylink;
|
||||||
|
|
||||||
use std::process::exit;
|
|
||||||
|
|
||||||
mod ui;
|
mod ui;
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::result::Result as RResult;
|
|
||||||
|
|
||||||
use libimagrt::runtime::Runtime;
|
use libimagrt::runtime::Runtime;
|
||||||
use libimagrt::application::ImagApplication;
|
use libimagrt::application::ImagApplication;
|
||||||
use libimagerror::trace::MapErrTrace;
|
|
||||||
use libimagerror::iter::TraceIterator;
|
|
||||||
use libimagerror::exit::ExitUnwrap;
|
|
||||||
use libimagstore::storeid::StoreId;
|
use libimagstore::storeid::StoreId;
|
||||||
use libimagstore::store::Store;
|
use libimagstore::store::Store;
|
||||||
use libimagstore::store::FileLockEntry;
|
use libimagstore::store::FileLockEntry;
|
||||||
use libimagentrylink::linkable::Linkable;
|
use libimagentrylink::linkable::Linkable;
|
||||||
use libimagstore::iter::get::StoreIdGetIteratorExtension;
|
use libimagstore::iter::get::StoreIdGetIteratorExtension;
|
||||||
|
use libimagerror::iter::IterInnerOkOrElse;
|
||||||
|
|
||||||
use failure::Fallible as Result;
|
use failure::Fallible as Result;
|
||||||
|
use failure::err_msg;
|
||||||
use clap::App;
|
use clap::App;
|
||||||
|
|
||||||
|
|
||||||
|
@ -77,72 +73,50 @@ impl ImagApplication for ImagMv {
|
||||||
.value_of("source")
|
.value_of("source")
|
||||||
.map(PathBuf::from)
|
.map(PathBuf::from)
|
||||||
.map(StoreId::new)
|
.map(StoreId::new)
|
||||||
.unwrap() // unwrap safe by clap
|
.unwrap()?; // unwrap safe by clap
|
||||||
.map_err_trace_exit_unwrap();
|
|
||||||
|
|
||||||
let destname = rt
|
let destname = rt
|
||||||
.cli()
|
.cli()
|
||||||
.value_of("dest")
|
.value_of("dest")
|
||||||
.map(PathBuf::from)
|
.map(PathBuf::from)
|
||||||
.map(StoreId::new)
|
.map(StoreId::new)
|
||||||
.unwrap() // unwrap safe by clap
|
.unwrap()?; // unwrap safe by clap
|
||||||
.map_err_trace_exit_unwrap();
|
|
||||||
|
|
||||||
// remove links to entry, and re-add them later
|
// remove links to entry, and re-add them later
|
||||||
let mut linked_entries = {
|
let mut linked_entries = rt.store()
|
||||||
rt.store()
|
.get(sourcename.clone())?
|
||||||
.get(sourcename.clone())
|
.ok_or_else(|| format_err!("Entry does not exist: {}", sourcename))?
|
||||||
.map_err_trace_exit_unwrap()
|
.links()?
|
||||||
.unwrap_or_else(|| {
|
.map(|link| link.get_store_id().clone())
|
||||||
error!("Funny things happened: Entry moved to destination did not fail, but entry does not exist");
|
.map(Ok)
|
||||||
exit(1)
|
.into_get_iter(rt.store())
|
||||||
})
|
.map_inner_ok_or_else(|| err_msg("Linked entry does not exist"))
|
||||||
.links()
|
.collect::<Result<Vec<_>>>()?;
|
||||||
.map_err_trace_exit_unwrap()
|
|
||||||
.map(|link| Ok(link.get_store_id().clone()) as RResult<_, _>)
|
|
||||||
.into_get_iter(rt.store())
|
|
||||||
.trace_unwrap_exit()
|
|
||||||
.map(|e| {
|
|
||||||
e.unwrap_or_else(|| {
|
|
||||||
error!("Linked entry does not exist");
|
|
||||||
exit(1)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
};
|
|
||||||
|
|
||||||
{ // remove links to linked entries from source
|
{ // remove links to linked entries from source
|
||||||
let mut entry = rt
|
let mut entry = rt
|
||||||
.store()
|
.store()
|
||||||
.get(sourcename.clone())
|
.get(sourcename.clone())?
|
||||||
.map_err_trace_exit_unwrap()
|
.ok_or_else(|| err_msg("Source Entry does not exist"))?;
|
||||||
.unwrap_or_else(|| {
|
|
||||||
error!("Source Entry does not exist");
|
|
||||||
exit(1)
|
|
||||||
});
|
|
||||||
|
|
||||||
for link in linked_entries.iter_mut() {
|
for link in linked_entries.iter_mut() {
|
||||||
let _ = entry.remove_link(link).map_err_trace_exit_unwrap();
|
entry.remove_link(link)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let _ = rt
|
if let Err(e) = rt.store().move_by_id(sourcename.clone(), destname.clone()) {
|
||||||
.store()
|
debug!("Re-adding links to source entry because moving failed");
|
||||||
.move_by_id(sourcename.clone(), destname.clone())
|
relink(rt.store(), sourcename.clone(), &mut linked_entries)?;
|
||||||
.map_err(|e| { // on error, re-add links
|
|
||||||
debug!("Re-adding links to source entry because moving failed");
|
|
||||||
relink(rt.store(), sourcename.clone(), &mut linked_entries);
|
|
||||||
e
|
|
||||||
})
|
|
||||||
.map_err_trace_exit_unwrap();
|
|
||||||
|
|
||||||
let _ = rt.report_touched(&destname).unwrap_or_exit();
|
return Err(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
rt.report_touched(&destname)?;
|
||||||
|
|
||||||
// re-add links to moved entry
|
// re-add links to moved entry
|
||||||
relink(rt.store(), destname, &mut linked_entries);
|
relink(rt.store(), destname, &mut linked_entries)?;
|
||||||
|
|
||||||
info!("Ok.");
|
info!("Ok.");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,17 +139,15 @@ impl ImagApplication for ImagMv {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fn relink<'a>(store: &'a Store, target: StoreId, linked_entries: &mut Vec<FileLockEntry<'a>>) {
|
fn relink<'a>(store: &'a Store, target: StoreId, linked_entries: &mut Vec<FileLockEntry<'a>>) -> Result<()> {
|
||||||
let mut entry = store
|
let mut entry = store
|
||||||
.get(target)
|
.get(target)?
|
||||||
.map_err_trace_exit_unwrap()
|
.ok_or_else(|| err_msg("Funny things happened: Entry moved to destination did not fail, but entry does not exist"))?;
|
||||||
.unwrap_or_else(|| {
|
|
||||||
error!("Funny things happened: Entry moved to destination did not fail, but entry does not exist");
|
|
||||||
exit(1)
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
for mut link in linked_entries {
|
for mut link in linked_entries {
|
||||||
let _ = entry.add_link(&mut link).map_err_trace_exit_unwrap();
|
let _ = entry.add_link(&mut link)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue