Skip to content

Commit fbd06e8

Browse files
authored
Merge pull request #1579 from cgwalters/srb-main
Deduplicate main error printing
2 parents 5be0261 + 4fb177c commit fbd06e8

File tree

7 files changed

+28
-24
lines changed

7 files changed

+28
-24
lines changed

Cargo.lock

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cli/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ bootc-utils = { package = "bootc-internal-utils", path = "../utils", version = "
2222
anstream = { workspace = true }
2323
anyhow = { workspace = true }
2424
log = { workspace = true }
25-
owo-colors = { workspace = true }
2625
tokio = { workspace = true, features = ["macros"] }
2726
tracing = { workspace = true }
2827

crates/cli/src/main.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//! The main entrypoint for bootc, which just performs global initialization, and then
22
//! calls out into the library.
33
//!
4-
use std::io::Write as _;
5-
64
use anyhow::Result;
75

86
/// The code called after we've done process global init and created
@@ -35,15 +33,5 @@ fn run() -> Result<()> {
3533
}
3634

3735
fn main() {
38-
use owo_colors::OwoColorize;
39-
40-
// In order to print the error in a custom format (with :#) our
41-
// main simply invokes a run() where all the work is done.
42-
// This code just captures any errors.
43-
if let Err(e) = run() {
44-
let mut stderr = anstream::stderr();
45-
// Don't panic if writing fails
46-
let _ = writeln!(stderr, "{}{:#}", "error: ".red(), e);
47-
std::process::exit(1);
48-
}
36+
bootc_utils::run_main(run)
4937
}

crates/system-reinstall-bootc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ bootc-mount = { path = "../mount" }
1919
bootc-utils = { package = "bootc-internal-utils", path = "../utils", version = "0.0.0" }
2020

2121
# Workspace dependencies
22+
anstream = { workspace = true }
2223
anyhow = { workspace = true }
2324
clap = { workspace = true, features = ["derive"] }
2425
indoc = { workspace = true }

crates/system-reinstall-bootc/src/main.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,5 @@ fn run() -> Result<()> {
6666
}
6767

6868
fn main() {
69-
// In order to print the error in a custom format (with :#) our
70-
// main simply invokes a run() where all the work is done.
71-
// This code just captures any errors.
72-
if let Err(e) = run() {
73-
tracing::error!("{:#}", e);
74-
std::process::exit(1);
75-
}
69+
bootc_utils::run_main(run)
7670
}

crates/utils/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ repository = "https://github.com/bootc-dev/bootc"
88

99
[dependencies]
1010
# Workspace dependencies
11+
anstream = { workspace = true }
1112
anyhow = { workspace = true }
1213
chrono = { workspace = true, features = ["std"] }
14+
owo-colors = { workspace = true }
1315
rustix = { workspace = true }
1416
serde = { workspace = true, features = ["derive"] }
1517
serde_json = { workspace = true }

crates/utils/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,21 @@ pub use tracing_util::*;
1616
pub mod reexec;
1717
mod result_ext;
1818
pub use result_ext::*;
19+
20+
/// Intended for use in `main`, calls an inner function and
21+
/// handles errors by printing them.
22+
pub fn run_main<F>(f: F)
23+
where
24+
F: FnOnce() -> anyhow::Result<()>,
25+
{
26+
use std::io::Write as _;
27+
28+
use owo_colors::OwoColorize;
29+
30+
if let Err(e) = f() {
31+
let mut stderr = anstream::stderr();
32+
// Don't panic if writing fails.
33+
let _ = writeln!(stderr, "{}{:#}", "error: ".red(), e);
34+
std::process::exit(1);
35+
}
36+
}

0 commit comments

Comments
 (0)