diff options
author | Till Höppner | 2016-02-04 13:46:11 +0100 |
---|---|---|
committer | Till Höppner | 2016-02-04 13:46:11 +0100 |
commit | 75b6c893acf9df6c3cecc8528f3c17e110378336 (patch) | |
tree | 53eac9cdf67b97c5a170f1105df95bc84cdea611 | |
parent | c0246e67ac04a35f7b03811946e1a3db22315ad9 (diff) | |
download | ilc-75b6c893acf9df6c3cecc8528f3c17e110378336.tar.gz ilc-75b6c893acf9df6c3cecc8528f3c17e110378336.tar.xz ilc-75b6c893acf9df6c3cecc8528f3c17e110378336.zip |
Re-add infer-data functionality
-rw-r--r-- | src/app/mod.rs | 56 | ||||
-rw-r--r-- | src/main.rs | 7 |
2 files changed, 37 insertions, 26 deletions
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<Encode> { } 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<BufRead> { - let input_files = args.values_of("input_files"); - if input_files.map(|files| files.count() > 0).unwrap_or(false) { - let input_files: Vec<PathBuf> = 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<PathBuf> { + 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<PathBuf>) -> Box<BufRead> { + 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<Write> { +pub fn open_output(args: &ArgMatches) -> Box<Write> { 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<BufRead> { build_input(self.0) } - pub fn output(&self) -> Box<Write> { build_output(self.0) } + pub fn input(&self) -> Box<BufRead> { open_files(gather_input(self.0)) } + pub fn output(&self) -> Box<Write> { open_output(self.0) } pub fn decoder(&self) -> Box<Decode> { force_decoder(self.0.value_of("input_format")) } pub fn encoder(&self) -> Box<Encode> { 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), + _ => () } } |