Skip to content

Commit

Permalink
make xtask use anyhow
Browse files Browse the repository at this point in the history
  • Loading branch information
bircni committed Aug 26, 2024
1 parent e4160e3 commit fefd7ae
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 21 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4997,6 +4997,7 @@ checksum = "ec7a2a501ed189703dba8b08142f057e887dfc4b2cc4db2d343ac6376ba3e0b9"
name = "xtask"
version = "0.28.1"
dependencies = [
"anyhow",
"log",
]

Expand Down
1 change: 1 addition & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ workspace = true

[dependencies]
log = { workspace = true }
anyhow = "1.0"
10 changes: 4 additions & 6 deletions xtask/src/deny.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 4 additions & 9 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@
mod deny;
pub(crate) mod utils;

type DynError = Box<dyn std::error::Error>;

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(())
}
Expand Down
10 changes: 4 additions & 6 deletions xtask/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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`.
Expand All @@ -30,16 +28,16 @@ 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}.");
}

let status = cmd.status()?;
if !status.success() {
return Err(format!("failed to {reason}: {status}").into());
anyhow::bail!(format!("failed to {reason}: {status}"));
}
Ok(())
}

0 comments on commit fefd7ae

Please sign in to comment.