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::*;
21
22/// The name of our binary
23pub const NAME: &str = "bootc";
24
25/// Return the podman binary path, honouring the `BOOTC_EXP_EXTERNAL_CONTAINER_TOOL`
26/// environment variable so callers can substitute an alternative tool (e.g. `dtool`)
27/// without hard-linking it as `/usr/bin/podman`. The _EXP prefix indicates this
28/// interface is experimental and subject to change.
29pub fn podman_bin() -> &'static str {
30    static BIN: std::sync::OnceLock<String> = std::sync::OnceLock::new();
31    BIN.get_or_init(|| {
32        std::env::var("BOOTC_EXP_EXTERNAL_CONTAINER_TOOL").unwrap_or_else(|_| "podman".to_string())
33    })
34}
35
36/// Return the skopeo binary path, honouring the `BOOTC_EXP_EXTERNAL_CONTAINER_TOOL`
37/// environment variable so callers can substitute an alternative tool (e.g. `dtool`)
38/// without hard-linking it as `/usr/bin/skopeo`. The _EXP prefix indicates this
39/// interface is experimental and subject to change.
40pub fn skopeo_bin() -> &'static str {
41    static BIN: std::sync::OnceLock<String> = std::sync::OnceLock::new();
42    BIN.get_or_init(|| {
43        std::env::var("BOOTC_EXP_EXTERNAL_CONTAINER_TOOL").unwrap_or_else(|_| "skopeo".to_string())
44    })
45}
46
47/// Intended for use in `main`, calls an inner function and
48/// handles errors by printing them.
49pub fn run_main<F>(f: F)
50where
51    F: FnOnce() -> anyhow::Result<()>,
52{
53    use std::io::Write as _;
54
55    use owo_colors::OwoColorize;
56
57    if let Err(e) = f() {
58        let mut stderr = anstream::stderr();
59        // Don't panic if writing fails.
60        let _ = writeln!(stderr, "{}{:#}", "error: ".red(), e);
61        std::process::exit(1);
62    }
63}