From fefd7aeb2c36adeab766dddbfb61cd4576b2f3d5 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 26 Aug 2024 22:39:06 +0200 Subject: [PATCH] make xtask use anyhow --- Cargo.lock | 1 + xtask/Cargo.toml | 1 + xtask/src/deny.rs | 10 ++++------ xtask/src/main.rs | 13 ++++--------- xtask/src/utils.rs | 10 ++++------ 5 files changed, 14 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fac885ea0e3f..b1cee8101941 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4997,6 +4997,7 @@ checksum = "ec7a2a501ed189703dba8b08142f057e887dfc4b2cc4db2d343ac6376ba3e0b9" name = "xtask" version = "0.28.1" dependencies = [ + "anyhow", "log", ] diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 4993979fecc2..fcbb2e1544f5 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -11,3 +11,4 @@ workspace = true [dependencies] log = { workspace = true } +anyhow = "1.0" diff --git a/xtask/src/deny.rs b/xtask/src/deny.rs index 5c24949bd439..9c93951f2e6d 100644 --- a/xtask/src/deny.rs +++ b/xtask/src/deny.rs @@ -4,11 +4,9 @@ use std::process::Command; -use super::DynError; - -pub fn deny(args: &[&str]) -> Result<(), DynError> { +pub fn deny(args: &[&str]) -> anyhow::Result<()> { if !args.is_empty() { - return Err(format!("Invalid arguments: {args:?}").into()); + anyhow::bail!(format!("Invalid arguments: {args:?}")); } install_cargo_deny()?; let targets = [ @@ -39,13 +37,13 @@ pub fn deny(args: &[&str]) -> Result<(), DynError> { super::utils::print_cmd(&cmd); let status = cmd.status()?; if !status.success() { - return Err(status.to_string().into()); + anyhow::bail!(status.to_string()); } } Ok(()) } -fn install_cargo_deny() -> Result<(), DynError> { +fn install_cargo_deny() -> anyhow::Result<()> { let already_installed = Command::new("cargo") .args(["deny", "--version"]) .output() diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 75c1c399ab9e..28d89aac61ab 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -7,23 +7,18 @@ mod deny; pub(crate) mod utils; -type DynError = Box; - -fn main() { - if let Err(e) = try_main() { - log::error!("{e}"); - std::process::exit(-1); - } +fn main() -> anyhow::Result<()> { + try_main() } -fn try_main() -> Result<(), DynError> { +fn try_main() -> anyhow::Result<()> { let arg_strings: Vec<_> = std::env::args().skip(1).collect(); let args: Vec<_> = arg_strings.iter().map(String::as_str).collect(); match args.as_slice() { &[] | &["-h"] | &["--help"] => print_help(), &["deny", ..] => deny::deny(&args[1..])?, - c => Err(format!("Invalid arguments {c:?}"))?, + c => anyhow::bail!(format!("Invalid arguments {c:?}")), } Ok(()) } diff --git a/xtask/src/utils.rs b/xtask/src/utils.rs index 6597f2fb8dba..1fc97ea46e15 100644 --- a/xtask/src/utils.rs +++ b/xtask/src/utils.rs @@ -4,8 +4,6 @@ use std::{ process::Command, }; -use super::DynError; - /// Print the command and its arguments as if the user had typed them pub fn print_cmd(cmd: &Command) { print!("{} ", cmd.get_program().to_string_lossy()); @@ -18,7 +16,7 @@ pub fn print_cmd(cmd: &Command) { /// Prompt user before running a command /// /// Adapted from [miri](https://github.com/rust-lang/miri/blob/dba35d2be72f4b78343d1a0f0b4737306f310672/cargo-miri/src/util.rs#L181-L204) -pub fn ask_to_run(mut cmd: Command, ask: bool, reason: &str) -> Result<(), DynError> { +pub fn ask_to_run(mut cmd: Command, ask: bool, reason: &str) -> anyhow::Result<()> { // Disable interactive prompts in CI (GitHub Actions, Travis, AppVeyor, etc). // Azure doesn't set `CI` though (nothing to see here, just Microsoft being Microsoft), // so we also check their `TF_BUILD`. @@ -30,8 +28,8 @@ pub fn ask_to_run(mut cmd: Command, ask: bool, reason: &str) -> Result<(), DynEr io::stdin().read_line(&mut buf).unwrap(); match buf.trim().to_lowercase().as_ref() { "" | "y" | "yes" => {} - "n" | "no" => return Err("Aborting as per your request".into()), - a => return Err(format!("Invalid answer `{a}`").into()), + "n" | "no" => anyhow::bail!("Aborting as per your request"), + a => anyhow::bail!(format!("Invalid answer `{a}`")), }; } else { log::info!("Running `{cmd:?}` to {reason}."); @@ -39,7 +37,7 @@ pub fn ask_to_run(mut cmd: Command, ask: bool, reason: &str) -> Result<(), DynEr let status = cmd.status()?; if !status.success() { - return Err(format!("failed to {reason}: {status}").into()); + anyhow::bail!(format!("failed to {reason}: {status}")); } Ok(()) }