Transform the main function to get the subcommand object

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-11-13 19:38:39 +01:00
parent 5a89a29b6d
commit 9e532f79dd

View file

@ -86,51 +86,40 @@ pub enum ImagTag {}
impl ImagApplication for ImagTag { impl ImagApplication for ImagTag {
fn run(rt: Runtime) -> Result<()> { fn run(rt: Runtime) -> Result<()> {
let process = |iter: &mut dyn Iterator<Item = Result<StoreId>>| -> Result<()> { let process = |iter: &mut dyn Iterator<Item = Result<StoreId>>| -> Result<()> {
if let Some(name) = rt.cli().subcommand_name() { match rt.cli().subcommand() {
match name { ("list", _) => iter
"list" => iter .map_ok(|id| list(id, &rt))
.map_ok(|id| list(id, &rt)) .collect::<Result<Vec<_>>>()
.collect::<Result<Vec<_>>>() .map(|_| ()),
.map(|_| ()),
"remove" => iter.and_then_ok(|id| { ("remove", _) => iter.and_then_ok(|id| {
let add = None; let add = None;
let rem = get_remove_tags(rt.cli())?; let rem = get_remove_tags(rt.cli())?;
debug!("id = {:?}, add = {:?}, rem = {:?}", id, add, rem); debug!("id = {:?}, add = {:?}, rem = {:?}", id, add, rem);
alter(&rt, id, add, rem) alter(&rt, id, add, rem)
}).collect(), }).collect(),
"add" => iter.and_then_ok(|id| { ("add", _) => iter.and_then_ok(|id| {
let add = get_add_tags(rt.cli())?; let add = get_add_tags(rt.cli())?;
let rem = None; let rem = None;
debug!("id = {:?}, add = {:?}, rem = {:?}", id, add, rem); debug!("id = {:?}, add = {:?}, rem = {:?}", id, add, rem);
alter(&rt, id, add, rem) alter(&rt, id, add, rem)
}).collect(), }).collect(),
other => { (other, _) => {
debug!("Unknown command"); debug!("Unknown command");
if rt.handle_unknown_subcommand("imag-tag", other, rt.cli())?.success() { if rt.handle_unknown_subcommand("imag-tag", other, rt.cli())?.success() {
Ok(()) Ok(())
} else { } else {
Err(format_err!("Subcommand failed")) Err(format_err!("Subcommand failed"))
} }
}, },
}
} else {
Ok(())
} }
}; };
if rt.ids_from_stdin() { match rt.ids::<crate::ui::PathProvider>()? {
debug!("Fetching IDs from stdin..."); Some(ids) => process(&mut ids.into_iter().map(Ok)),
let mut iter = rt.ids::<crate::ui::PathProvider>()? None => process(&mut rt.store().entries()?),
.ok_or_else(|| err_msg("No ids supplied"))?
.into_iter()
.map(Ok);
process(&mut iter)
} else {
process(&mut rt.store().entries()?)
} }
} }