Implement list_command

This commit is contained in:
Matthias Beyer 2015-11-30 19:55:13 +01:00
parent 42181afde5
commit 6fcccc8297

View file

@ -7,9 +7,11 @@ use module::CommandResult;
use module::CommandEnv;
use module::bm::header::build_header;
use module::bm::header::get_tags_from_header;
use storage::json::parser::JsonHeaderParser;
use storage::parser::{Parser, FileHeaderParser};
use storage::file::File;
use ui::file::{FilePrinter, TablePrinter};
use clap::ArgMatches;
use regex::Regex;
@ -33,18 +35,29 @@ pub fn add_command(module: &Module, env: CommandEnv) -> CommandResult {
pub fn list_command(module: &Module, env: CommandEnv) -> CommandResult {
let tags = get_tags(env.rt, env.matches);
let matcher = get_matcher(env.rt, env.matches);
debug!("Tags: {:?}", tags);
let parser = Parser::new(JsonHeaderParser::new(None));
let printer = TablePrinter::new(env.rt.is_verbose(), env.rt.is_debugging());
let files = env.bk.iter_files(module, &parser).and_then(|files| {
let f = files.filter(|file| {
if tags.len() != 0 {
debug!("Checking tags of: {:?}", file.id());
get_tags_from_header(&file.header()).iter()
.any(|t| tags.contains(t))
} else {
true
}
}).filter(|file| {
debug!("Checking matches of: {:?}", file.id());
get_matcher(env.rt, env.matches)
.and_then(|r| Some(file.matches_with(&r)))
.unwrap_or(true)
}).collect::<Vec<File>>();
Some(f)
}).unwrap_or(Vec::<File>::new()).into_iter();
match matcher {
Some(reg) => {
info!("Listing urls with matcher '{}' and with tags {:?}",
reg.as_str(),
tags);
}
None => {
info!("Listing urls with tags {:?}", tags);
}
}
debug!("Printing files now");
printer.print_files(files);
Ok(())
}