Merge pull request #310 from matthiasbeyer/libimagentrylist/cli
Libimagentrylist/cli
This commit is contained in:
commit
1fc7b64171
9 changed files with 141 additions and 9 deletions
|
@ -4,6 +4,7 @@ version = "0.1.0"
|
|||
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
|
||||
|
||||
[dependencies]
|
||||
clap = "2.1.1"
|
||||
log = "0.3.5"
|
||||
toml = "0.1.25"
|
||||
|
||||
|
|
82
libimagentrylist/src/cli.rs
Normal file
82
libimagentrylist/src/cli.rs
Normal file
|
@ -0,0 +1,82 @@
|
|||
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> {
|
||||
SubCommand::with_name(list_subcommand_name())
|
||||
.author("Matthias Beyer <mail@beyermatthias.de>")
|
||||
.version("0.1")
|
||||
.about("List entries")
|
||||
|
||||
.arg(Arg::with_name(list_backend_line())
|
||||
.short("l")
|
||||
.long("line")
|
||||
.takes_value(false)
|
||||
.required(false)
|
||||
.multiple(false)
|
||||
.help("Use backend: Line"))
|
||||
|
||||
.arg(Arg::with_name(list_backend_path())
|
||||
.short("p")
|
||||
.long("path")
|
||||
.takes_value(false)
|
||||
.required(false)
|
||||
.multiple(false)
|
||||
.help("Use backend: Path"))
|
||||
|
||||
.arg(Arg::with_name(list_backend_path_absolute())
|
||||
.short("P")
|
||||
.long("path-absolute")
|
||||
.takes_value(false)
|
||||
.required(false)
|
||||
.multiple(false)
|
||||
.help("Use backend: Path (absolute)"))
|
||||
|
||||
}
|
||||
|
||||
pub fn list_subcommand_name() -> &'static str {
|
||||
"list"
|
||||
}
|
||||
|
||||
pub fn list_backend_line() -> &'static str {
|
||||
"line"
|
||||
}
|
||||
|
||||
pub fn list_backend_path() -> &'static str {
|
||||
"path"
|
||||
}
|
||||
|
||||
pub fn list_backend_path_absolute() -> &'static str {
|
||||
"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 {
|
||||
FormatError,
|
||||
EntryError,
|
||||
IterationError
|
||||
IterationError,
|
||||
CLIError,
|
||||
}
|
||||
|
||||
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::EntryError => "EntryError",
|
||||
&ListErrorKind::IterationError => "IterationError",
|
||||
&ListErrorKind::CLIError => "No CLI subcommand for listing entries",
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
extern crate clap;
|
||||
#[macro_use] extern crate log;
|
||||
extern crate toml;
|
||||
|
||||
extern crate libimagstore;
|
||||
|
||||
pub mod cli;
|
||||
pub mod error;
|
||||
pub mod lister;
|
||||
pub mod listers;
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
use clap::ArgMatches;
|
||||
|
||||
use libimagstore::store::FileLockEntry;
|
||||
|
||||
use result::Result;
|
||||
|
||||
pub trait Lister {
|
||||
pub trait Lister : Sized {
|
||||
|
||||
fn list<'a, I: Iterator<Item = FileLockEntry<'a>>>(&self, entries: I) -> Result<()>;
|
||||
|
||||
|
|
40
libimagentrylist/src/listers/core.rs
Normal file
40
libimagentrylist/src/listers/core.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
use std::io::stdout;
|
||||
use std::io::Write;
|
||||
use std::ops::Deref;
|
||||
|
||||
use lister::Lister;
|
||||
use result::Result;
|
||||
|
||||
use libimagstore::store::FileLockEntry;
|
||||
use libimagstore::store::Entry;
|
||||
|
||||
pub struct CoreLister<'a> {
|
||||
lister: &'a Fn(&Entry) -> String,
|
||||
}
|
||||
|
||||
impl<'a> CoreLister<'a> {
|
||||
|
||||
pub fn new(lister: &'a Fn(&Entry) -> String) -> CoreLister<'a> {
|
||||
CoreLister {
|
||||
lister: lister,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl<'a> Lister for CoreLister<'a> {
|
||||
|
||||
fn list<'b, I: Iterator<Item = FileLockEntry<'b>>>(&self, entries: I) -> Result<()> {
|
||||
use error::ListError as LE;
|
||||
use error::ListErrorKind as LEK;
|
||||
|
||||
entries.fold(Ok(()), |accu, entry| {
|
||||
accu.and_then(|_| {
|
||||
write!(stdout(), "{:?}\n", (self.lister)(entry.deref()))
|
||||
.map_err(|e| LE::new(LEK::FormatError, Some(Box::new(e))))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,22 +1,22 @@
|
|||
use std::io::stdout;
|
||||
use std::io::Write;
|
||||
use std::ops::Deref;
|
||||
|
||||
use cli::list_subcommand_name;
|
||||
use lister::Lister;
|
||||
use result::Result;
|
||||
|
||||
use clap::ArgMatches;
|
||||
use libimagstore::store::FileLockEntry;
|
||||
use libimagstore::store::Entry;
|
||||
|
||||
pub struct LineLister<'a> {
|
||||
lister: &'a Fn(&Entry) -> String,
|
||||
unknown_output: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> LineLister<'a> {
|
||||
|
||||
pub fn new(lister: &'a Fn(&Entry) -> String) -> LineLister<'a> {
|
||||
pub fn new(unknown_output: &'a str) -> LineLister<'a> {
|
||||
LineLister {
|
||||
lister: lister,
|
||||
unknown_output: unknown_output,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -30,11 +30,11 @@ impl<'a> Lister for LineLister<'a> {
|
|||
|
||||
entries.fold(Ok(()), |accu, entry| {
|
||||
accu.and_then(|_| {
|
||||
write!(stdout(), "{:?}\n", (self.lister)(entry.deref()))
|
||||
write!(stdout(), "{:?}\n",
|
||||
entry.get_location().to_str().unwrap_or(self.unknown_output))
|
||||
.map_err(|e| LE::new(LEK::FormatError, Some(Box::new(e))))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
pub mod core;
|
||||
pub mod line;
|
||||
pub mod path;
|
||||
|
|
|
@ -2,9 +2,11 @@ use std::io::stdout;
|
|||
use std::io::Write;
|
||||
use std::ops::Deref;
|
||||
|
||||
use cli::list_subcommand_name;
|
||||
use lister::Lister;
|
||||
use result::Result;
|
||||
|
||||
use clap::ArgMatches;
|
||||
use libimagstore::store::FileLockEntry;
|
||||
|
||||
pub struct PathLister {
|
||||
|
|
Loading…
Reference in a new issue