Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch to color-eyre for error reporting #654

Merged
merged 3 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- #654 - Use color-eyre for error reporting
- #658 - Upgrade dependencies
- #647 - Add `mips64-unknown-linux-muslabi64` and `mips64el-unknown-linux-muslabi64` support
- #652 - Allow trying individual targets via bors.
Expand Down
130 changes: 121 additions & 9 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ edition = "2018"

[dependencies]
atty = "0.2"
error-chain = "0.12"
color-eyre = "0.6.1"
eyre = "0.6.7"
Emilgardis marked this conversation as resolved.
Show resolved Hide resolved
home = "0.5"
lazy_static = "1.0"
libc = "0.2.104"
Expand Down
2 changes: 1 addition & 1 deletion src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Root {

/// Cargo project root
pub fn root() -> Result<Option<Root>> {
let cd = env::current_dir().chain_err(|| "couldn't get current directory")?;
let cd = env::current_dir().wrap_err("couldn't get current directory")?;

let mut dir = &*cd;
loop {
Expand Down
39 changes: 19 additions & 20 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::{Result, Target, Toml};

use crate::errors::*;
use std::collections::HashMap;
use std::env;

#[derive(Debug)]
struct Environment(&'static str, Option<HashMap<&'static str, &'static str>>);

Expand Down Expand Up @@ -42,22 +44,20 @@ impl Environment {
self.get_build_var("XARGO"),
self.get_target_var(target, "XARGO"),
);
let build_env =
if let Some(value) = build_xargo {
Some(value.parse::<bool>().map_err(|_| {
format!("error parsing {} from XARGO environment variable", value)
})?)
} else {
None
};
let target_env =
if let Some(value) = target_xargo {
Some(value.parse::<bool>().map_err(|_| {
format!("error parsing {} from XARGO environment variable", value)
})?)
} else {
None
};
let build_env = if let Some(value) = build_xargo {
Some(value.parse::<bool>().wrap_err_with(|| {
format!("error parsing {} from XARGO environment variable", value)
})?)
} else {
None
};
let target_env = if let Some(value) = target_xargo {
Some(value.parse::<bool>().wrap_err_with(|| {
format!("error parsing {} from XARGO environment variable", value)
})?)
} else {
None
};

Ok((build_env, target_env))
}
Expand Down Expand Up @@ -279,10 +279,9 @@ mod tests {

fn toml(content: &str) -> Result<crate::Toml> {
Ok(crate::Toml {
table: if let Ok(toml::Value::Table(table)) = content.parse() {
table
} else {
return Err("couldn't parse toml as TOML table".into());
table: match content.parse().wrap_err("couldn't parse toml")? {
toml::Value::Table(table) => table,
_ => eyre::bail!("couldn't parse toml as TOML table"),
},
})
}
Expand Down
53 changes: 19 additions & 34 deletions src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,35 @@ use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus};
use std::{env, fs};

use atty::Stream;
use error_chain::bail;

use crate::cargo::Root;
use crate::errors::*;
use crate::extensions::{CommandExt, SafeCommand};
use crate::id;
use crate::{Config, Target};
use atty::Stream;
use eyre::bail;

const DOCKER_IMAGES: &[&str] = &include!(concat!(env!("OUT_DIR"), "/docker-images.rs"));
const CROSS_IMAGE: &str = "ghcr.io/cross-rs";
const DOCKER: &str = "docker";
const PODMAN: &str = "podman";

fn get_container_engine() -> Result<std::path::PathBuf> {
let container_engine = env::var("CROSS_CONTAINER_ENGINE").unwrap_or_default();

if container_engine.is_empty() {
which::which(DOCKER)
.or_else(|_| which::which(PODMAN))
.map_err(|e| e.into())
fn get_container_engine() -> Result<std::path::PathBuf, which::Error> {
if let Ok(ce) = env::var("CROSS_CONTAINER_ENGINE") {
which::which(ce)
} else {
which::which(container_engine).map_err(|e| e.into())
which::which(DOCKER).or_else(|_| which::which(PODMAN))
}
}

pub fn docker_command(subcommand: &str) -> Result<Command> {
if let Ok(ce) = get_container_engine() {
let mut command = Command::new(ce);
command.arg(subcommand);
command.args(&["--userns", "host"]);
Ok(command)
} else {
Err("no container engine found; install docker or podman".into())
}
let ce = get_container_engine()
.map_err(|_| eyre::eyre!("no container engine found"))
.with_suggestion(|| "is docker or podman installed?")?;
let mut command = Command::new(ce);
command.arg(subcommand);
command.args(&["--userns", "host"]);
Ok(command)
}

/// Register binfmt interpreters
Expand Down Expand Up @@ -77,7 +71,7 @@ pub fn run(
};

let root = root.path();
let home_dir = home::home_dir().ok_or("could not find home directory")?;
let home_dir = home::home_dir().ok_or_else(|| eyre::eyre!("could not find home directory"))?;
let cargo_dir = home::cargo_home()?;
let xargo_dir = env::var_os("XARGO_HOME")
.map(PathBuf::from)
Expand Down Expand Up @@ -233,11 +227,7 @@ pub fn image(config: &Config, target: &Target) -> Result<String> {
}

fn docker_read_mount_paths() -> Result<Vec<MountDetail>> {
let hostname = if let Ok(v) = env::var("HOSTNAME") {
Ok(v)
} else {
Err("HOSTNAME environment variable not found")
}?;
let hostname = env::var("HOSTNAME").wrap_err("HOSTNAME environment variable not found")?;

let docker_path = which::which(DOCKER)?;
let mut docker: Command = {
Expand All @@ -248,12 +238,7 @@ fn docker_read_mount_paths() -> Result<Vec<MountDetail>> {
};

let output = docker.run_and_get_stdout(false)?;
let info = if let Ok(val) = serde_json::from_str(&output) {
Ok(val)
} else {
Err("failed to parse docker inspect output")
}?;

let info = serde_json::from_str(&output).wrap_err("failed to parse docker inspect output")?;
dockerinfo_parse_mounts(&info)
}

Expand All @@ -268,20 +253,20 @@ fn dockerinfo_parse_root_mount_path(info: &serde_json::Value) -> Result<MountDet
let driver_name = info
.pointer("/0/GraphDriver/Name")
.and_then(|v| v.as_str())
.ok_or("No driver name found")?;
.ok_or_else(|| eyre::eyre!("no driver name found"))?;

if driver_name == "overlay2" {
let path = info
.pointer("/0/GraphDriver/Data/MergedDir")
.and_then(|v| v.as_str())
.ok_or("No merge directory found")?;
.ok_or_else(|| eyre::eyre!("No merge directory found"))?;

Ok(MountDetail {
source: PathBuf::from(&path),
destination: PathBuf::from("/"),
})
} else {
Err(format!("want driver overlay2, got {}", driver_name).into())
eyre::bail!("want driver overlay2, got {}", driver_name)
}
}

Expand Down
Loading