Skip to content
This repository has been archived by the owner on May 18, 2020. It is now read-only.

Commit

Permalink
v0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Nugine committed Apr 8, 2019
1 parent e3deb32 commit 82bed45
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tester"
version = "0.3.0"
version = "0.3.1"
authors = ["Nugine <nugine@foxmail.com>"]
edition = "2018"

Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@

## Usage

tester 0.3.0
tester 0.3.1
Nugine <nugine@foxmail.com>

USAGE:
tester [FLAGS] <target> [-- <args>...]
tester [FLAGS] [OPTIONS] <target> [-- <args>...]

FLAGS:
-h, --help Prints help information
-j, --json json output (stderr)
-j, --json Json output
-V, --version Prints version information

OPTIONS:
-o, --output <output> output file path (default stderr)

ARGS:
<target> command to run
<args>... arguments to be passed
Expand All @@ -63,6 +66,8 @@ declare type TesterOutput = ({
## Changelog
+ Add option `--output` on `0.3.1`
+ Break changes on `0.3.0`
delete `--arg`
Expand Down
33 changes: 27 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,42 @@ use crate::tester::{Tester, TraitTester};

use structopt::StructOpt;

#[inline(always)]
fn err_exit<E: std::fmt::Display>(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<dyn Write> = 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)
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OsString>,

#[structopt(last = true, parse(from_os_str), help = "arguments to be passed")]
pub args: Vec<OsString>,
}

0 comments on commit 82bed45

Please sign in to comment.