cargo-fmt: don't return zero on failure

This commit is contained in:
Aleksey Kladov 2016-03-25 16:11:33 +03:00
parent 8fd95df54a
commit ac7778cc51

View File

@ -12,19 +12,30 @@
#![cfg(not(test))] #![cfg(not(test))]
#![cfg(feature="cargo-fmt")] #![cfg(feature="cargo-fmt")]
#![deny(warnings)]
extern crate getopts; extern crate getopts;
extern crate rustc_serialize; extern crate rustc_serialize;
use std::path::PathBuf;
use std::process::Command;
use std::env; use std::env;
use std::io::Write;
use std::path::PathBuf;
use std::process::{Command, ExitStatus};
use std::str; use std::str;
use getopts::Options; use getopts::Options;
use rustc_serialize::json::Json; use rustc_serialize::json::Json;
fn main() { fn main() {
let exit_status = execute();
std::io::stdout().flush().unwrap();
std::process::exit(exit_status);
}
fn execute() -> i32 {
let success = 0;
let failure = 1;
let mut opts = getopts::Options::new(); let mut opts = getopts::Options::new();
opts.optflag("h", "help", "show this message"); opts.optflag("h", "help", "show this message");
opts.optflag("q", "quiet", "no output printed to stdout"); opts.optflag("q", "quiet", "no output printed to stdout");
@ -34,7 +45,7 @@ fn main() {
Ok(m) => m, Ok(m) => m,
Err(e) => { Err(e) => {
print_usage(&opts, &e.to_string()); print_usage(&opts, &e.to_string());
return; return failure;
} }
}; };
@ -42,13 +53,26 @@ fn main() {
if verbose && quiet { if verbose && quiet {
print_usage(&opts, "quiet mode and verbose mode are not compatible"); print_usage(&opts, "quiet mode and verbose mode are not compatible");
return; return failure;
} }
if matches.opt_present("h") { if matches.opt_present("h") {
print_usage(&opts, ""); print_usage(&opts, "");
} else { return success;
format_crate(&opts, verbose, quiet); }
match format_crate(verbose, quiet) {
Err(e) => {
print_usage(&opts, &e.to_string());
failure
}
Ok(status) => {
if status.success() {
success
} else {
status.code().unwrap_or(failure)
}
}
} }
} }
@ -59,14 +83,8 @@ fn print_usage(opts: &Options, reason: &str) {
opts.usage(&msg)); opts.usage(&msg));
} }
fn format_crate(opts: &Options, verbose: bool, quiet: bool) { fn format_crate(verbose: bool, quiet: bool) -> Result<ExitStatus, std::io::Error> {
let targets = match get_targets() { let targets = try!(get_targets());
Ok(t) => t,
Err(e) => {
print_usage(opts, &e.to_string());
return;
}
};
// Currently only bin and lib files get formatted // Currently only bin and lib files get formatted
let files: Vec<_> = targets.into_iter() let files: Vec<_> = targets.into_iter()
@ -80,7 +98,6 @@ fn format_crate(opts: &Options, verbose: bool, quiet: bool) {
.collect(); .collect();
format_files(&files, &get_fmt_args(), verbose, quiet) format_files(&files, &get_fmt_args(), verbose, quiet)
.unwrap_or_else(|e| print_usage(opts, &e.to_string()));
} }
fn get_fmt_args() -> Vec<String> { fn get_fmt_args() -> Vec<String> {
@ -158,7 +175,7 @@ fn format_files(files: &Vec<PathBuf>,
fmt_args: &Vec<String>, fmt_args: &Vec<String>,
verbose: bool, verbose: bool,
quiet: bool) quiet: bool)
-> Result<(), std::io::Error> { -> Result<ExitStatus, std::io::Error> {
let stdout = if quiet { let stdout = if quiet {
std::process::Stdio::null() std::process::Stdio::null()
} else { } else {
@ -179,7 +196,5 @@ fn format_files(files: &Vec<PathBuf>,
.args(files) .args(files)
.args(fmt_args) .args(fmt_args)
.spawn()); .spawn());
try!(command.wait()); command.wait()
Ok(())
} }