Skip to content

Commit

Permalink
Try #791:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored Jun 14, 2022
2 parents 5fe2145 + 81ac368 commit 413be06
Show file tree
Hide file tree
Showing 14 changed files with 249 additions and 110 deletions.
29 changes: 25 additions & 4 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ members = ["xtask"]

[dependencies]
atty = "0.2"
# Pinned until further action to migrate to newer clap, see https://github.com/clap-rs/clap/issues/3822#issuecomment-1154069623
clap = { version = "~3.1", features = ["derive"] }
clap = { version = "3.2.2", features = ["derive", "unstable-v4"] }
color-eyre = "0.6"
eyre = "0.6"
thiserror = "1"
home = "0.5"
rustc_version = "0.4"
toml = "0.5"
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cross-util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ fn remove_images(
}
command.args(images);
if execute {
command.run(verbose)
command.run(verbose).map_err(Into::into)
} else {
println!("{:?}", command);
Ok(())
Expand Down
7 changes: 5 additions & 2 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,14 @@ pub fn cargo_metadata_with_args(
}

/// Pass-through mode
pub fn run(args: &[String], verbose: bool) -> Result<ExitStatus> {
pub fn run(args: &[String], verbose: bool) -> Result<ExitStatus, CommandError> {
Command::new("cargo").args(args).run_and_get_status(verbose)
}

/// run cargo and get the output, does not check the exit status
pub fn run_and_get_output(args: &[String], verbose: bool) -> Result<std::process::Output> {
pub fn run_and_get_output(
args: &[String],
verbose: bool,
) -> Result<std::process::Output, CommandError> {
Command::new("cargo").args(args).run_and_get_output(verbose)
}
2 changes: 2 additions & 0 deletions src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub fn register(target: &Target, verbose: bool) -> Result<()> {
.arg("ubuntu:16.04")
.args(&["sh", "-c", cmd])
.run(verbose)
.map_err(Into::into)
}

fn validate_env_var(var: &str) -> Result<(&str, Option<&str>)> {
Expand Down Expand Up @@ -363,6 +364,7 @@ pub fn run(
.arg(&image(config, target)?)
.args(&["sh", "-c", &format!("PATH=$PATH:/rust/bin {:?}", cmd)])
.run_and_get_status(verbose)
.map_err(Into::into)
}

pub fn image(config: &Config, target: &Target) -> Result<String> {
Expand Down
10 changes: 10 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ pub fn install_panic_hook() -> Result<()> {
.display_env_section(false)
.install()
}

#[derive(Debug, thiserror::Error)]
pub enum CommandError {
#[error("`{1}` failed with exit code: {0}")]
NonZeroExitCode(std::process::ExitStatus, String),
#[error("could not execute `{0}`")]
CouldNotExecute(#[source] Box<dyn std::error::Error + Send + Sync>, String),
#[error("`{0:?}` output was not UTF-8")]
Utf8Error(#[source] std::string::FromUtf8Error, std::process::Output),
}
34 changes: 16 additions & 18 deletions src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use crate::errors::*;

pub trait CommandExt {
fn print_verbose(&self, verbose: bool);
fn status_result(&self, status: ExitStatus) -> Result<()>;
fn run(&mut self, verbose: bool) -> Result<()>;
fn run_and_get_status(&mut self, verbose: bool) -> Result<ExitStatus>;
fn run_and_get_stdout(&mut self, verbose: bool) -> Result<String>;
fn run_and_get_output(&mut self, verbose: bool) -> Result<std::process::Output>;
fn status_result(&self, status: ExitStatus) -> Result<(), CommandError>;
fn run(&mut self, verbose: bool) -> Result<(), CommandError>;
fn run_and_get_status(&mut self, verbose: bool) -> Result<ExitStatus, CommandError>;
fn run_and_get_stdout(&mut self, verbose: bool) -> Result<String, CommandError>;
fn run_and_get_output(&mut self, verbose: bool) -> Result<std::process::Output, CommandError>;
}

impl CommandExt for Command {
Expand All @@ -20,29 +20,29 @@ impl CommandExt for Command {
}
}

fn status_result(&self, status: ExitStatus) -> Result<()> {
fn status_result(&self, status: ExitStatus) -> Result<(), CommandError> {
if status.success() {
Ok(())
} else {
eyre::bail!("`{:?}` failed with exit code: {:?}", self, status.code())
Err(CommandError::NonZeroExitCode(status, format!("{self:?}")))
}
}

/// Runs the command to completion
fn run(&mut self, verbose: bool) -> Result<()> {
fn run(&mut self, verbose: bool) -> Result<(), CommandError> {
let status = self.run_and_get_status(verbose)?;
self.status_result(status)
}

/// Runs the command to completion
fn run_and_get_status(&mut self, verbose: bool) -> Result<ExitStatus> {
fn run_and_get_status(&mut self, verbose: bool) -> Result<ExitStatus, CommandError> {
self.print_verbose(verbose);
self.status()
.wrap_err_with(|| format!("couldn't execute `{:?}`", self))
.map_err(|e| CommandError::CouldNotExecute(Box::new(e), format!("{self:?}")))
}

/// Runs the command to completion and returns its stdout
fn run_and_get_stdout(&mut self, verbose: bool) -> Result<String> {
fn run_and_get_stdout(&mut self, verbose: bool) -> Result<String, CommandError> {
let out = self.run_and_get_output(verbose)?;
self.status_result(out.status)?;
out.stdout()
Expand All @@ -53,22 +53,20 @@ impl CommandExt for Command {
/// # Notes
///
/// This command does not check the status.
fn run_and_get_output(&mut self, verbose: bool) -> Result<std::process::Output> {
fn run_and_get_output(&mut self, verbose: bool) -> Result<std::process::Output, CommandError> {
self.print_verbose(verbose);
self.output()
.wrap_err_with(|| format!("couldn't execute `{:?}`", self))
.map_err(Into::into)
.map_err(|e| CommandError::CouldNotExecute(Box::new(e), format!("{self:?}")))
}
}

pub trait OutputExt {
fn stdout(&self) -> Result<String>;
fn stdout(&self) -> Result<String, CommandError>;
}

impl OutputExt for std::process::Output {
fn stdout(&self) -> Result<String> {
String::from_utf8(self.stdout.clone())
.wrap_err_with(|| format!("`{:?}` output was not UTF-8", self))
fn stdout(&self) -> Result<String, CommandError> {
String::from_utf8(self.stdout.clone()).map_err(|e| CommandError::Utf8Error(e, self.clone()))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod cli;
mod config;
mod cross_toml;
mod docker;
mod errors;
pub mod errors;
mod extensions;
mod file;
mod id;
Expand Down Expand Up @@ -464,7 +464,7 @@ pub fn run() -> Result<ExitStatus> {
}
Ok(out.status)
}
_ => cargo::run(&argv, verbose),
_ => cargo::run(&argv, verbose).map_err(Into::into),
}
}

Expand Down
1 change: 1 addition & 0 deletions src/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub fn target_list(verbose: bool) -> Result<TargetList> {
.map(|s| TargetList {
triples: s.lines().map(|l| l.to_owned()).collect(),
})
.map_err(Into::into)
}

pub fn sysroot(host: &Host, target: &Target, verbose: bool) -> Result<PathBuf> {
Expand Down
2 changes: 1 addition & 1 deletion xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cross = { path = "..", features = ["dev"] }
walkdir = { version = "2" }
color-eyre = "0.6"
eyre = "0.6"
clap = { version = "~3.1", features = ["derive", "env"] }
clap = { version = "3.1", features = ["derive", "env", "unstable-v4"] }
which = { version = "4", default_features = false }
serde = { version = "1", features = ["derive"] }
serde_yaml = "0.8"
Loading

0 comments on commit 413be06

Please sign in to comment.