dev-fmt: better error handling

Check if rustfmt is installed at the start and exit if it isn't.
This commit is contained in:
Michael Wright 2019-07-28 06:41:13 +02:00
parent dc69a5c0b6
commit cc779c8050

View file

@ -10,6 +10,7 @@ pub enum CliError {
CommandFailed(String),
IoError(io::Error),
ProjectRootNotFound,
RustfmtNotInstalled,
WalkDirError(walkdir::Error),
}
@ -36,6 +37,8 @@ pub fn run(check: bool, verbose: bool) {
let project_root = project_root()?;
rustfmt_test(context)?;
success &= cargo_fmt(context, project_root.as_path())?;
success &= cargo_fmt(context, &project_root.join("clippy_dev"))?;
success &= cargo_fmt(context, &project_root.join("rustc_tools_util"))?;
@ -69,6 +72,9 @@ pub fn run(check: bool, verbose: bool) {
CliError::ProjectRootNotFound => {
eprintln!("error: Can't determine root of project. Please run inside a Clippy working dir.");
},
CliError::RustfmtNotInstalled => {
eprintln!("error: rustfmt nightly is not installed.");
},
CliError::WalkDirError(err) => {
eprintln!("error: {}", err);
},
@ -139,6 +145,29 @@ fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
Ok(success)
}
fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
let program = "rustfmt";
let dir = std::env::current_dir()?;
let args = &["+nightly", "--version"];
if context.verbose {
println!("{}", format_command(&program, &dir, args));
}
let output = Command::new(&program).current_dir(&dir).args(args.iter()).output()?;
if output.status.success() {
Ok(())
} else if std::str::from_utf8(&output.stderr)
.unwrap_or("")
.starts_with("error: 'rustfmt' is not installed")
{
Err(CliError::RustfmtNotInstalled)
} else {
Err(CliError::CommandFailed(format_command(&program, &dir, args)))
}
}
fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
let mut args = vec!["+nightly".as_ref(), path.as_os_str()];
if context.check {