|
7 | 7 |
|
8 | 8 | use anyhow::bail;
|
9 | 9 | use anyhow::Context;
|
| 10 | +use std::collections::BTreeMap; |
| 11 | +use std::ffi::OsStr; |
| 12 | +use std::ffi::OsString; |
10 | 13 | use std::path::PathBuf;
|
11 | 14 |
|
12 | 15 | // We've made our own parser here instead of using something like clap in order
|
@@ -119,26 +122,41 @@ pub struct Options {
|
119 | 122 | }
|
120 | 123 |
|
121 | 124 | 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 | + }; |
132 | 136 | }
|
133 | 137 |
|
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 { |
135 | 153 | value
|
136 | 154 | .map(|v| v.to_ascii_lowercase() == "true" || v == "1")
|
137 | 155 | .unwrap_or_default()
|
138 | 156 | }
|
139 | 157 |
|
140 | 158 | 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())); |
142 | 160 |
|
143 | 161 | let parse_legacy_env_number = |name| {
|
144 | 162 | legacy_openhcl_env(name)
|
|
0 commit comments