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::{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}
17
18/// Result summary for a `treeboot env` invocation.
19#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
20#[serde(transparent)]
21pub struct EnvReport {
22    /// Environment variables passed to init scripts and configured commands.
23    pub environment: BTreeMap<String, String>,
24}
25
26/// Inspects the treeboot child environment.
27///
28/// This function does not discover init scripts, parse config, apply file
29/// operations, or execute commands.
30///
31/// # Errors
32///
33/// Returns an error if context discovery fails.
34pub fn inspect_env(options: EnvOptions) -> Result<EnvReport> {
35    let context = context::resolve(&WorktreeOptions {
36        cwd: options.cwd,
37        root: options.root,
38    })?;
39    let environment = context
40        .environment
41        .into_iter()
42        .map(|(name, value)| (name, value.to_string_lossy().into_owned()))
43        .collect();
44
45    Ok(EnvReport { environment })
46}