From 75b6c893acf9df6c3cecc8528f3c17e110378336 Mon Sep 17 00:00:00 2001 From: Till Höppner Date: Thu, 4 Feb 2016 13:46:11 +0100 Subject: Re-add infer-data functionality --- src/app/mod.rs | 56 +++++++++++++++++++++++++++++++------------------------- src/main.rs | 7 ++++++- 2 files changed, 37 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/app/mod.rs b/src/app/mod.rs index 50117e7..b8006b7 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -7,10 +7,11 @@ use glob::glob; use std::process; use std::str::FromStr; -use std::path::PathBuf; +use std::path::{ Path, PathBuf }; use std::io::{ self, Write, BufWriter, BufRead, BufReader }; use std::fs::File; use std::error::Error; +use std::ffi::OsStr; use ilc::context::Context; use ilc::format::{ self, Encode, Decode }; @@ -57,43 +58,48 @@ pub fn force_encoder<'a>(s: Option<&str>) -> Box { } pub fn build_context(args: &ArgMatches) -> Context { - Context { + let mut context = Context { timezone: FixedOffset::west(args.value_of("timezone").and_then(|s| s.parse().ok()).unwrap_or(0)), override_date: args.value_of("date").and_then(|d| NaiveDate::from_str(&d).ok()), channel: args.value_of("channel").map(str::to_owned).clone() - } -} - -pub fn build_input(args: &ArgMatches) -> Box { - let input_files = args.values_of("input_files"); - if input_files.map(|files| files.count() > 0).unwrap_or(false) { - let input_files: Vec = if let Some(iter) = args.values_of("input_files") { - iter.flat_map(|p| { - match glob(p) { - Ok(paths) => paths, - Err(e) => die(&format!("{}", e.msg)) - } - }).filter_map(Result::ok).collect() - } else { Vec::new() }; - - /*if args.flag_infer_date { - if input_files.len() > 1 { die("Too many input files, can't infer date") } - if let Some(date) = input_files.iter().next() + }; + if args.is_present("infer_date") { + let input_files = gather_input(args); + match input_files.len() { + 0 => die("No input files given, can't infer date"), + 1 => if let Some(date) = input_files.get(0) .map(PathBuf::as_path) .and_then(Path::file_stem) .and_then(OsStr::to_str) .and_then(|s: &str| NaiveDate::from_str(s).ok()) { context.override_date = Some(date); + }, + _n => die("Too many input files, can't infer date") + } + } + context +} + +pub fn gather_input(args: &ArgMatches) -> Vec { + if let Some(iter) = args.values_of("input_files") { + iter.flat_map(|p| { + match glob(p) { + Ok(paths) => paths, + Err(e) => die(&format!("{}", e.msg)) } - }*/ + }).filter_map(Result::ok).collect() + } else { Vec::new() } +} - Box::new(BufReader::new(chain::Chain::new(input_files.iter().map(|p| File::open(p).unwrap()).collect()))) +pub fn open_files(files: Vec) -> Box { + if files.len() > 0 { + Box::new(BufReader::new(chain::Chain::new(files.iter().map(|p| File::open(p).unwrap()).collect()))) } else { Box::new(BufReader::new(io::stdin())) } } -pub fn build_output(args: &ArgMatches) -> Box { +pub fn open_output(args: &ArgMatches) -> Box { if let Some(out) = args.value_of("output_file") { match File::create(out) { Ok(f) => Box::new(BufWriter::new(f)), @@ -108,8 +114,8 @@ pub struct Environment<'a>(pub &'a ArgMatches<'a>); impl<'a> Environment<'a> { pub fn context(&self) -> Context { build_context(self.0) } - pub fn input(&self) -> Box { build_input(self.0) } - pub fn output(&self) -> Box { build_output(self.0) } + pub fn input(&self) -> Box { open_files(gather_input(self.0)) } + pub fn output(&self) -> Box { open_output(self.0) } pub fn decoder(&self) -> Box { force_decoder(self.0.value_of("input_format")) } pub fn encoder(&self) -> Box { force_encoder(self.0.value_of("output_format")) } } diff --git a/src/main.rs b/src/main.rs index 11f007f..df4d790 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,6 +50,10 @@ fn main() { .takes_value(true) .long("date") .short("d")) + .arg(Arg::with_name("infer_date") + .help("Try to use the filename as date for the current log") + .global(true) + .long("infer-date")) .arg(Arg::with_name("channel") .help("Set a channel for the current log") .global(true) @@ -107,6 +111,7 @@ fn main() { ("seen", Some(args)) => app::seen::seen(args), ("sort", Some(args)) => app::sort::sort(args), ("dedup", Some(args)) => app::dedup::dedup(args), - _ => panic!("Unimplemented subcommand, this is a bug") + (sc, _) if !sc.is_empty() => panic!("Unimplemented subcommand `{}`, this is a bug", sc), + _ => () } } -- cgit v1.2.3