Skip to content

uh_core: Don't modify the environment for start requests #518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions openhcl/underhill_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ fn install_task_name_panic_hook() {
}

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

let crate_name = build_info::get().crate_name();
let crate_revision = build_info::get().scm_revision();
Expand Down Expand Up @@ -496,14 +496,7 @@ async fn run_control(
if workers.is_some() {
Err(anyhow::anyhow!("workers have already been started"))?;
}
for (key, value) in params.env {
if let Some(value) = value {
std::env::set_var(key, value);
} else {
std::env::remove_var(key);
}
}
let new_opt = Options::parse(params.args)
let new_opt = Options::parse(params.args, params.env)
.context("failed to parse new options")?;

workers = Some(
Expand Down
42 changes: 30 additions & 12 deletions openhcl/underhill_core/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

use anyhow::bail;
use anyhow::Context;
use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::ffi::OsString;
use std::path::PathBuf;

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

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

fn parse_bool(value: Option<std::ffi::OsString>) -> bool {
// Reads an environment variable, falling back to a legacy variable (replacing
// "OPENHCL_" with "UNDERHILL_") if the original is not set.
let legacy_openhcl_env = |name: &str| -> Option<&OsString> {
env.get::<OsStr>(name.as_ref()).or_else(|| {
env.get::<OsStr>(
format!(
"UNDERHILL_{}",
name.strip_prefix("OPENHCL_").unwrap_or(name)
)
.as_ref(),
)
})
};

fn parse_bool(value: Option<&OsString>) -> bool {
value
.map(|v| v.to_ascii_lowercase() == "true" || v == "1")
.unwrap_or_default()
}

let parse_legacy_env_bool = |name| parse_bool(legacy_openhcl_env(name));
let parse_env_bool = |name| parse_bool(std::env::var_os(name));
let parse_env_bool = |name: &str| parse_bool(env.get::<OsStr>(name.as_ref()));

let parse_legacy_env_number = |name| {
legacy_openhcl_env(name)
Expand Down
Loading