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