From 82bed456bdf562bca7cb7e1178ba68ab3aaee9db Mon Sep 17 00:00:00 2001 From: Nugine Date: Mon, 8 Apr 2019 19:55:00 +0800 Subject: [PATCH] v0.3.1 --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 11 ++++++++--- src/main.rs | 33 +++++++++++++++++++++++++++------ src/opt.rs | 10 +++++++++- 5 files changed, 46 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 49de9db..0d6daf0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -184,7 +184,7 @@ dependencies = [ [[package]] name = "tester" -version = "0.3.0" +version = "0.3.1" dependencies = [ "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "nix 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 0c8b5ce..16c799a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tester" -version = "0.3.0" +version = "0.3.1" authors = ["Nugine "] edition = "2018" diff --git a/README.md b/README.md index e74cdc4..1710da1 100644 --- a/README.md +++ b/README.md @@ -27,17 +27,20 @@ ## Usage - tester 0.3.0 + tester 0.3.1 Nugine USAGE: - tester [FLAGS] [-- ...] + tester [FLAGS] [OPTIONS] [-- ...] FLAGS: -h, --help Prints help information - -j, --json json output (stderr) + -j, --json Json output -V, --version Prints version information + OPTIONS: + -o, --output output file path (default stderr) + ARGS: command to run ... arguments to be passed @@ -63,6 +66,8 @@ declare type TesterOutput = ({ ## Changelog ++ Add option `--output` on `0.3.1` + + Break changes on `0.3.0` delete `--arg` diff --git a/src/main.rs b/src/main.rs index 589e0d1..4c3df7e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,21 +11,42 @@ use crate::tester::{Tester, TraitTester}; use structopt::StructOpt; +#[inline(always)] +fn err_exit(e: E) -> ! { + eprintln!("tester: {}", e); + std::process::exit(1) +} + fn main() { let opt = Opt::from_args(); - let tester = Tester::new(opt.target, opt.args); + let json = opt.json; + + use std::io::Write; + let mut output_file: Box = match opt.output { + None => Box::new(std::io::stderr()), + Some(ref path) => match std::fs::File::create(path) { + Err(e) => err_exit(e), + Ok(f) => Box::new(f), + }, + }; + let tester = Tester::new(opt.target, opt.args); match tester.run() { Err(err) => { - eprintln!("tester: {}", err.msg()); - std::process::exit(1); + if let Err(e) = writeln!(output_file, "tester: {}", err.msg()) { + err_exit(e) + } } Ok(out) => { - if opt.json { - eprintln!("{}", serde_json::to_string(&out).unwrap()); + let out_string = if json { + serde_json::to_string(&out).unwrap() } else { - eprintln!("{}", out); + format!("{}", out) + }; + + if let Err(e) = writeln!(output_file, "{}", out_string) { + err_exit(e) } } } diff --git a/src/opt.rs b/src/opt.rs index bcf0ae7..de1d521 100644 --- a/src/opt.rs +++ b/src/opt.rs @@ -8,9 +8,17 @@ pub struct Opt { #[structopt(parse(from_os_str), help = "command to run")] pub target: OsString, - #[structopt(short = "j", long = "json", help = "json output (stderr)")] + #[structopt(short = "j", long = "json", help = "Json output")] pub json: bool, + #[structopt( + short = "o", + long = "output", + parse(from_os_str), + help = "output file path (default stderr)" + )] + pub output: Option, + #[structopt(last = true, parse(from_os_str), help = "arguments to be passed")] pub args: Vec, }