Skip to content

Commit 7436d6f

Browse files
committed
Deduplicate main error printing
As part of the tracing-subscriber CVE I did a quick audit for usages of `tracing::error!` and I noticed when we updated the `main()` function in the primary crate we missed also doing the same for system-reinstall-bootc. Move the handling of that to utils. xref: https://bugzilla.redhat.com/show_bug.cgi?id=2392017 Signed-off-by: Colin Walters <[email protected]>
1 parent df2da1a commit 7436d6f

File tree

7 files changed

+30
-22
lines changed

7 files changed

+30
-22
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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,5 @@ fn run() -> Result<()> {
3535
}
3636

3737
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-
}
38+
bootc_utils::run_main(run)
4939
}

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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,23 @@ 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+
// NOTE: The main logic in crates/cli/main.rs is the canonical
31+
// copy of this.
32+
if let Err(e) = f() {
33+
let mut stderr = anstream::stderr();
34+
// Don't panic if writing fails
35+
let _ = writeln!(stderr, "{}{:#}", "error: ".red(), e);
36+
std::process::exit(1);
37+
}
38+
}

0 commit comments

Comments
 (0)