mirror of
https://github.com/eyre-rs/eyre.git
synced 2025-09-28 05:21:34 +00:00

* port color-eyre to new eyre with global hook * add a singular hook configuration * add panic handler to globally installed hook * getting near the end * remove unused module * temp: working on capture consistency * walk back changes that make porting to new release harder * merge main into hooked * switch to github color-spantrace and copy from htmlgit status * move spantrace capture default mode into hook * rename and reorg section stuff * switch to released deps * copy changes from lib.rs to readme * update snippets and revert to images for readme * remove extremely fragile format tests
69 lines
1.8 KiB
Rust
69 lines
1.8 KiB
Rust
use color_eyre::{
|
|
eyre::Report,
|
|
eyre::{eyre, WrapErr},
|
|
Section, SectionExt,
|
|
};
|
|
use std::process::Command;
|
|
use tracing::instrument;
|
|
|
|
trait Output {
|
|
fn output2(&mut self) -> Result<String, Report>;
|
|
}
|
|
|
|
impl Output for Command {
|
|
#[instrument]
|
|
fn output2(&mut self) -> Result<String, Report> {
|
|
let output = self.output()?;
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
|
|
|
if !output.status.success() {
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
Err(eyre!("cmd exited with non-zero status code"))
|
|
.with_section(move || stdout.trim().to_string().header("Stdout:"))
|
|
.with_section(move || stderr.trim().to_string().header("Stderr:"))
|
|
} else {
|
|
Ok(stdout.into())
|
|
}
|
|
}
|
|
}
|
|
|
|
#[instrument]
|
|
fn main() -> Result<(), Report> {
|
|
#[cfg(feature = "capture-spantrace")]
|
|
install_tracing();
|
|
color_eyre::install()?;
|
|
|
|
Ok(read_config().map(drop)?)
|
|
}
|
|
|
|
#[cfg(feature = "capture-spantrace")]
|
|
fn install_tracing() {
|
|
use tracing_error::ErrorLayer;
|
|
use tracing_subscriber::prelude::*;
|
|
use tracing_subscriber::{fmt, EnvFilter};
|
|
|
|
let fmt_layer = fmt::layer().with_target(false);
|
|
let filter_layer = EnvFilter::try_from_default_env()
|
|
.or_else(|_| EnvFilter::try_new("info"))
|
|
.unwrap();
|
|
|
|
tracing_subscriber::registry()
|
|
.with(filter_layer)
|
|
.with(fmt_layer)
|
|
.with(ErrorLayer::default())
|
|
.init();
|
|
}
|
|
|
|
#[instrument]
|
|
fn read_file(path: &str) -> Result<String, Report> {
|
|
Command::new("cat").arg(path).output2()
|
|
}
|
|
|
|
#[instrument]
|
|
fn read_config() -> Result<String, Report> {
|
|
read_file("fake_file")
|
|
.wrap_err("Unable to read config")
|
|
.suggestion("try using a file that exists next time")
|
|
}
|