Skip to content

Commit e815988

Browse files
authored
Merge pull request #1115 from pkgxdev/env-overrides
parent env can override pantry runtime env
2 parents df7249b + 94bc612 commit e815988

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

crates/cli/src/main.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use libpkgx::{
1212
config::Config, env, hydrate::hydrate, install_multi, pantry_db, resolve::resolve, sync,
1313
types::PackageReq, utils,
1414
};
15+
use regex::Regex;
1516
use rusqlite::Connection;
1617
use serde_json::json;
1718

@@ -207,6 +208,21 @@ async fn main() -> Result<(), Box<dyn Error>> {
207208
let env = env::mix(env);
208209
let mut env = env::mix_runtime(&env, &installations, &conn)?;
209210

211+
let re = Regex::new(r"^\$\{\w+:-([^}]+)\}$").unwrap();
212+
213+
for (key, value) in env.clone() {
214+
if let Some(caps) = re.captures(&value) {
215+
env.insert(key, caps.get(1).unwrap().as_str().to_string());
216+
} else {
217+
let cleaned_value = value
218+
.replace(&format!(":${}", key), "")
219+
.replace(&format!("${}:", key), "")
220+
.replace(&format!("; ${}", key), "") // one pantry instance of this
221+
.replace(&format!("${}", key), "");
222+
env.insert(key, cleaned_value);
223+
}
224+
}
225+
210226
// fork bomb protection
211227
env.insert("PKGX_LVL".to_string(), pkgx_lvl.to_string());
212228

@@ -220,7 +236,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
220236
let env = env.iter().map(|(k, v)| (k.clone(), v.join(":"))).collect();
221237
let env = env::mix_runtime(&env, &installations, &conn)?;
222238
for (key, value) in env {
223-
println!("{}=\"{}${{{}:+:${}}}\"", key, value, key, key);
239+
println!(
240+
"{}=\"{}\"",
241+
key,
242+
value.replace(&format!(":${}", key), &format!("${{{}:+:${}}}", key, key))
243+
);
224244
}
225245
} else {
226246
let mut runtime_env = HashMap::new();

crates/lib/src/env.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,22 +138,28 @@ pub fn mix_runtime(
138138
installations: &Vec<Installation>,
139139
conn: &Connection,
140140
) -> Result<HashMap<String, String>, Box<dyn Error>> {
141-
let mut output = input.clone();
141+
let mut output: HashMap<String, String> = input
142+
.iter()
143+
.map(|(k, v)| (k.clone(), format!("{}:${}", v, k)))
144+
.collect();
142145

143146
for installation in installations.clone() {
144147
let runtime_env =
145148
crate::pantry_db::runtime_env_for_project(&installation.pkg.project, conn)?;
146149
for (key, runtime_value) in runtime_env {
147150
let runtime_value = expand_moustaches(&runtime_value, &installation, installations);
148-
let new_value = match output.get(&key) {
149-
Some(curr_value) => runtime_value.replace(&format!("${}", key), curr_value),
150-
None => {
151-
//TODO need to remove any $FOO, aware of `:` delimiters
152-
runtime_value
153-
.replace(&format!(":${}", key), "")
154-
.replace(&format!("${}:", key), "")
155-
.replace(&format!("${}", key), "")
151+
let new_value = if let Some(curr_value) = output.get(&key) {
152+
if runtime_value.contains(&format!("${}", key)) {
153+
runtime_value.replace(&format!("${}", key), curr_value)
154+
} else {
155+
// parent env overrides runtime env if the runtime env
156+
// has no capacity to include the parent env
157+
curr_value.clone()
156158
}
159+
} else if runtime_value.contains(&format!("${}", key)) {
160+
runtime_value
161+
} else {
162+
format!("${{{}:-{}}}", key, runtime_value)
157163
};
158164
output.insert(key, new_value);
159165
}

0 commit comments

Comments
 (0)