Implement list-dead subcommand

This patch implements a helper in imag-ref to list all dead references
either by store id or by path which is referenced.

Suggested-by: Leon Schürmann <leon@is.currently.online>
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-04-22 14:57:10 +02:00
parent dc60942111
commit 485779a176

View file

@ -74,9 +74,10 @@ fn main() {
.map(|name| {
debug!("Call: {}", name);
match name {
"deref" => deref(&rt),
"create" => create(&rt),
"remove" => remove(&rt),
"deref" => deref(&rt),
"create" => create(&rt),
"remove" => remove(&rt),
"list-dead" => list_dead(&rt),
other => {
debug!("Unknown command");
let _ = rt.handle_unknown_subcommand("imag-ref", other, rt.cli())
@ -161,6 +162,47 @@ fn remove(rt: &Runtime) {
});
}
fn list_dead(rt: &Runtime) {
let cfg = get_ref_config(&rt, "imag-ref").map_err_trace_exit_unwrap();
let cmd = rt.cli().subcommand_matches("list-dead").unwrap(); // safe by main()
let list_path = cmd.is_present("list-dead-pathes");
let list_id = cmd.is_present("list-dead-ids");
let mut output = rt.stdout();
rt.ids::<::ui::PathProvider>()
.map_err_trace_exit_unwrap()
.into_iter()
.for_each(|id| {
match rt.store().get(id.clone()).map_err_trace_exit_unwrap() {
Some(entry) => {
let entry_path = entry
.as_ref_with_hasher::<DefaultHasher>()
.get_path(&cfg)
.map_err_trace_exit_unwrap();
if !entry_path.exists() {
if list_id {
writeln!(output, "{}", entry.get_location().local().display())
} else if list_path {
writeln!(output, "{}", entry_path.display())
} else {
unimplemented!()
}
.map_err(Error::from)
.map_err_trace_exit_unwrap();
let _ = rt.report_touched(entry.get_location()).unwrap_or_exit();
}
}
None => {
error!("Does not exist: {}", id.local().display());
exit(1)
}
}
});
}
fn create(rt: &Runtime) {
unimplemented!()
}