Skip to content

Commit 3528c1d

Browse files
committed
uh_core: Don't modify the environment for start requests
1 parent b682370 commit 3528c1d

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

openhcl/underhill_core/src/lib.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ fn install_task_name_panic_hook() {
192192
}
193193

194194
async fn do_main(driver: DefaultDriver, mut tracing: TracingBackend) -> anyhow::Result<()> {
195-
let opt = Options::parse(Vec::new())?;
195+
let opt = Options::parse(Vec::new(), Vec::new())?;
196196

197197
let crate_name = build_info::get().crate_name();
198198
let crate_revision = build_info::get().scm_revision();
@@ -496,14 +496,7 @@ async fn run_control(
496496
if workers.is_some() {
497497
Err(anyhow::anyhow!("workers have already been started"))?;
498498
}
499-
for (key, value) in params.env {
500-
if let Some(value) = value {
501-
std::env::set_var(key, value);
502-
} else {
503-
std::env::remove_var(key);
504-
}
505-
}
506-
let new_opt = Options::parse(params.args)
499+
let new_opt = Options::parse(params.args, params.env)
507500
.context("failed to parse new options")?;
508501

509502
workers = Some(

openhcl/underhill_core/src/options.rs

+30-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
use anyhow::bail;
99
use anyhow::Context;
10+
use std::collections::BTreeMap;
11+
use std::ffi::OsStr;
12+
use std::ffi::OsString;
1013
use std::path::PathBuf;
1114

1215
// We've made our own parser here instead of using something like clap in order
@@ -119,26 +122,41 @@ pub struct Options {
119122
}
120123

121124
impl Options {
122-
pub(crate) fn parse(extra_args: Vec<String>) -> anyhow::Result<Self> {
123-
/// Reads an environment variable, falling back to a legacy variable (replacing
124-
/// "OPENHCL_" with "UNDERHILL_") if the original is not set.
125-
fn legacy_openhcl_env(name: &str) -> Option<std::ffi::OsString> {
126-
std::env::var_os(name).or_else(|| {
127-
std::env::var_os(format!(
128-
"UNDERHILL_{}",
129-
name.strip_prefix("OPENHCL_").unwrap_or(name)
130-
))
131-
})
125+
pub(crate) fn parse(
126+
extra_args: Vec<String>,
127+
extra_env: Vec<(String, Option<String>)>,
128+
) -> anyhow::Result<Self> {
129+
// Pull the entire environment into a BTreeMap for manipulation through extra_env.
130+
let mut env: BTreeMap<OsString, OsString> = std::env::vars_os().collect();
131+
for (key, value) in extra_env {
132+
match value {
133+
Some(value) => env.insert(key.into(), value.into()),
134+
None => env.remove::<OsStr>(key.as_ref()),
135+
};
132136
}
133137

134-
fn parse_bool(value: Option<std::ffi::OsString>) -> bool {
138+
// Reads an environment variable, falling back to a legacy variable (replacing
139+
// "OPENHCL_" with "UNDERHILL_") if the original is not set.
140+
let legacy_openhcl_env = |name: &str| -> Option<&OsString> {
141+
env.get::<OsStr>(name.as_ref()).or_else(|| {
142+
env.get::<OsStr>(
143+
format!(
144+
"UNDERHILL_{}",
145+
name.strip_prefix("OPENHCL_").unwrap_or(name)
146+
)
147+
.as_ref(),
148+
)
149+
})
150+
};
151+
152+
fn parse_bool(value: Option<&OsString>) -> bool {
135153
value
136154
.map(|v| v.to_ascii_lowercase() == "true" || v == "1")
137155
.unwrap_or_default()
138156
}
139157

140158
let parse_legacy_env_bool = |name| parse_bool(legacy_openhcl_env(name));
141-
let parse_env_bool = |name| parse_bool(std::env::var_os(name));
159+
let parse_env_bool = |name: &str| parse_bool(env.get::<OsStr>(name.as_ref()));
142160

143161
let parse_legacy_env_number = |name| {
144162
legacy_openhcl_env(name)

0 commit comments

Comments
 (0)