Skip to main content

bootc_internal_utils/
lib.rs

1//! The inevitable catchall "utils" crate. Generally only add
2//! things here that only depend on the standard library and
3//! "core" crates.
4//!
5mod chroot;
6pub use chroot::*;
7mod command;
8pub use command::*;
9mod iterators;
10pub use iterators::*;
11mod path;
12pub use path::*;
13/// Re-execute the current process
14pub mod reexec;
15mod result_ext;
16pub use result_ext::*;
17mod timestamp;
18pub use timestamp::*;
19mod tracing_util;
20pub use tracing_util::*;
21mod uki;
22pub use uki::*;
23
24/// The name of our binary
25pub const NAME: &str = "bootc";
26
27/// Return the podman binary path, honouring the `BOOTC_EXP_EXTERNAL_CONTAINER_TOOL`
28/// environment variable so callers can substitute an alternative tool (e.g. `dtool`)
29/// without hard-linking it as `/usr/bin/podman`. The _EXP prefix indicates this
30/// interface is experimental and subject to change.
31pub fn podman_bin() -> &'static str {
32    static BIN: std::sync::OnceLock<String> = std::sync::OnceLock::new();
33    BIN.get_or_init(|| {
34        std::env::var("BOOTC_EXP_EXTERNAL_CONTAINER_TOOL").unwrap_or_else(|_| "podman".to_string())
35    })
36}
37
38/// Return the skopeo binary path, honouring the `BOOTC_EXP_EXTERNAL_CONTAINER_TOOL`
39/// environment variable so callers can substitute an alternative tool (e.g. `dtool`)
40/// without hard-linking it as `/usr/bin/skopeo`. The _EXP prefix indicates this
41/// interface is experimental and subject to change.
42pub fn skopeo_bin() -> &'static str {
43    static BIN: std::sync::OnceLock<String> = std::sync::OnceLock::new();
44    BIN.get_or_init(|| {
45        std::env::var("BOOTC_EXP_EXTERNAL_CONTAINER_TOOL").unwrap_or_else(|_| "skopeo".to_string())
46    })
47}
48
49/// Intended for use in `main`, calls an inner function and
50/// handles errors by printing them.
51pub fn run_main<F>(f: F)
52where
53    F: FnOnce() -> anyhow::Result<()>,
54{
55    use std::io::Write as _;
56
57    use owo_colors::OwoColorize;
58
59    if let Err(e) = f() {
60        let mut stderr = anstream::stderr();
61        // Don't panic if writing fails.
62        let _ = writeln!(stderr, "{}{:#}", "error: ".red(), e);
63        std::process::exit(1);
64    }
65}