Add lister helper function
This commit is contained in:
parent
ecfdbab947
commit
a41d037bcf
2 changed files with 36 additions and 2 deletions
|
@ -1,4 +1,12 @@
|
||||||
use clap::{Arg, App, SubCommand};
|
use clap::{Arg, ArgMatches, App, SubCommand};
|
||||||
|
|
||||||
|
use libimagstore::store::FileLockEntry;
|
||||||
|
|
||||||
|
use result::Result;
|
||||||
|
use listers::line::LineLister;
|
||||||
|
use listers::path::PathLister;
|
||||||
|
use lister::Lister;
|
||||||
|
use error::{ListError, ListErrorKind};
|
||||||
|
|
||||||
pub fn build_list_cli_component<'a, 'b>() -> App<'a, 'b> {
|
pub fn build_list_cli_component<'a, 'b>() -> App<'a, 'b> {
|
||||||
SubCommand::with_name(list_subcommand_name())
|
SubCommand::with_name(list_subcommand_name())
|
||||||
|
@ -48,3 +56,27 @@ pub fn list_backend_path_absolute() -> &'static str {
|
||||||
"path-absolute"
|
"path-absolute"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Add Registry for listers where a HashMap name->lister is in and where we can fetch the
|
||||||
|
// lister from.
|
||||||
|
pub fn list_entries_with_lister<'a, I>(m: &ArgMatches, entries: I) -> Result<()>
|
||||||
|
where I: Iterator<Item = FileLockEntry<'a>>
|
||||||
|
{
|
||||||
|
if let Some(matches) = m.subcommand_matches(list_subcommand_name()) {
|
||||||
|
if matches.is_present(list_backend_line()) {
|
||||||
|
return LineLister::new("<unknown>").list(entries)
|
||||||
|
};
|
||||||
|
|
||||||
|
if matches.is_present(list_backend_path()) {
|
||||||
|
return PathLister::new(false).list(entries)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if matches.is_present(list_backend_path_absolute()) {
|
||||||
|
return PathLister::new(true).list(entries)
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(ListError::new(ListErrorKind::CLIError, None))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -10,7 +10,8 @@ use std::fmt::{Display, Formatter};
|
||||||
pub enum ListErrorKind {
|
pub enum ListErrorKind {
|
||||||
FormatError,
|
FormatError,
|
||||||
EntryError,
|
EntryError,
|
||||||
IterationError
|
IterationError,
|
||||||
|
CLIError,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn counter_error_type_as_str(err: &ListErrorKind) -> &'static str{
|
fn counter_error_type_as_str(err: &ListErrorKind) -> &'static str{
|
||||||
|
@ -18,6 +19,7 @@ fn counter_error_type_as_str(err: &ListErrorKind) -> &'static str{
|
||||||
&ListErrorKind::FormatError => "FormatError",
|
&ListErrorKind::FormatError => "FormatError",
|
||||||
&ListErrorKind::EntryError => "EntryError",
|
&ListErrorKind::EntryError => "EntryError",
|
||||||
&ListErrorKind::IterationError => "IterationError",
|
&ListErrorKind::IterationError => "IterationError",
|
||||||
|
&ListErrorKind::CLIError => "No CLI subcommand for listing entries",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue