Skip to main content

treeboot_core/
env.rs

1use std::collections::BTreeMap;
2use std::path::PathBuf;
3
4use serde::Serialize;
5
6use crate::context;
7use crate::{EnvironmentInput, Result, WorktreeOptions};
8
9/// Options for inspecting the treeboot child environment.
10#[derive(Debug, Clone, Default, PartialEq, Eq)]
11pub struct EnvOptions {
12    /// Directory from which environment discovery starts.
13    pub cwd: Option<PathBuf>,
14    /// Overrides the root checkout used for discovery.
15    pub root: Option<PathBuf>,
16    /// Explicit environment input used for compatibility discovery.
17    pub environment: EnvironmentInput,
18}
19
20/// Result summary for a `treeboot env` invocation.
21#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
22#[serde(transparent)]
23pub struct EnvReport {
24    /// Environment variables passed to init scripts and configured commands.
25    pub environment: BTreeMap<String, String>,
26}
27
28/// Inspects the treeboot child environment.
29///
30/// This function does not discover init scripts, parse config, apply file
31/// operations, or execute commands.
32///
33/// # Errors
34///
35/// Returns an error if context discovery fails.
36pub fn inspect_env(options: EnvOptions) -> Result<EnvReport> {
37    let context = context::resolve(&WorktreeOptions {
38        cwd: options.cwd,
39        root: options.root,
40        environment: options.environment,
41    })?;
42    let environment = context
43        .environment
44        .into_iter()
45        .map(|(name, value)| (name, value.to_string_lossy().into_owned()))
46        .collect();
47
48    Ok(EnvReport { environment })
49}