rippy_cli/environment.rs
1use std::path::PathBuf;
2
3use crate::resolve::{EnvLookup, VarLookup};
4
5/// External environment dependencies for the analysis pipeline.
6///
7/// Groups all values that come from the OS environment so they can be
8/// overridden in tests without manipulating env vars. Production code
9/// creates this via [`Environment::from_system`]; tests use the builder
10/// methods to inject specific values.
11pub struct Environment {
12 /// Home directory (`$HOME`). Used for `~/.rippy/config`, `~/.claude/settings`.
13 /// `None` skips all home-based lookups.
14 pub home: Option<PathBuf>,
15
16 /// Working directory for the analysis (usually `std::env::current_dir()`).
17 pub working_directory: PathBuf,
18
19 /// Variable lookup for static expansion resolution.
20 /// Defaults to `EnvLookup` (real `std::env::var`).
21 pub var_lookup: Box<dyn VarLookup>,
22
23 /// Whether the command originates from a remote context (e.g. `docker exec`).
24 pub remote: bool,
25
26 /// Emit tracing to stderr.
27 pub verbose: bool,
28}
29
30impl Environment {
31 /// Build from the real system environment.
32 #[must_use]
33 pub fn from_system(working_directory: PathBuf, remote: bool, verbose: bool) -> Self {
34 Self {
35 home: std::env::var_os("HOME").map(PathBuf::from),
36 working_directory,
37 var_lookup: Box::new(EnvLookup),
38 remote,
39 verbose,
40 }
41 }
42
43 /// Build an isolated environment for tests: no home directory, no remote,
44 /// no verbose, real env-var lookup for variable resolution.
45 #[must_use]
46 pub fn for_test(working_directory: PathBuf) -> Self {
47 Self {
48 home: None,
49 working_directory,
50 var_lookup: Box::new(EnvLookup),
51 remote: false,
52 verbose: false,
53 }
54 }
55
56 /// Override the home directory (builder pattern).
57 #[must_use]
58 pub fn with_home(mut self, home: Option<PathBuf>) -> Self {
59 self.home = home;
60 self
61 }
62
63 /// Override the variable lookup (builder pattern).
64 #[must_use]
65 pub fn with_var_lookup(mut self, var_lookup: Box<dyn VarLookup>) -> Self {
66 self.var_lookup = var_lookup;
67 self
68 }
69}