|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is dual-licensed under either the MIT license found in the |
| 5 | + * LICENSE-MIT file in the root directory of this source tree or the Apache |
| 6 | + * License, Version 2.0 found in the LICENSE-APACHE file in the root directory |
| 7 | + * of this source tree. You may select, at your option, one of the |
| 8 | + * above-listed licenses. |
| 9 | + */ |
| 10 | + |
| 11 | +use std::env::VarError; |
| 12 | + |
| 13 | +use once_cell::sync::Lazy; |
| 14 | +use regex::Regex; |
| 15 | + |
| 16 | +/// Replace occurrences of `$FOO` in a string with the value of the env var `$FOO`. |
| 17 | +/// Returns an error if any referenced variable is not set. |
| 18 | +pub fn substitute_env_vars(s: &str) -> buck2_error::Result<String> { |
| 19 | + substitute_env_vars_impl(s, |v| std::env::var(v)) |
| 20 | +} |
| 21 | + |
| 22 | +pub fn substitute_env_vars_impl( |
| 23 | + s: &str, |
| 24 | + getter: impl Fn(&str) -> Result<String, VarError>, |
| 25 | +) -> buck2_error::Result<String> { |
| 26 | + static ENV_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new("\\$[a-zA-Z_][a-zA-Z_0-9]*").unwrap()); |
| 27 | + |
| 28 | + let mut out = String::with_capacity(s.len()); |
| 29 | + let mut last_idx = 0; |
| 30 | + |
| 31 | + for mat in ENV_REGEX.find_iter(s) { |
| 32 | + out.push_str(&s[last_idx..mat.start()]); |
| 33 | + let var = &mat.as_str()[1..]; |
| 34 | + let val = getter(var).map_err(|e| { |
| 35 | + buck2_error::buck2_error!( |
| 36 | + buck2_error::ErrorTag::Environment, |
| 37 | + "Error substituting `{}`: {}", |
| 38 | + mat.as_str(), |
| 39 | + e |
| 40 | + ) |
| 41 | + })?; |
| 42 | + out.push_str(&val); |
| 43 | + last_idx = mat.end(); |
| 44 | + } |
| 45 | + |
| 46 | + if last_idx < s.len() { |
| 47 | + out.push_str(&s[last_idx..s.len()]); |
| 48 | + } |
| 49 | + |
| 50 | + Ok(out) |
| 51 | +} |
| 52 | + |
| 53 | +#[cfg(test)] |
| 54 | +mod tests { |
| 55 | + use std::env::VarError; |
| 56 | + |
| 57 | + use super::*; |
| 58 | + |
| 59 | + #[test] |
| 60 | + fn test_substitute_env_vars() { |
| 61 | + let getter = |s: &str| match s { |
| 62 | + "FOO" => Ok("foo_value".to_owned()), |
| 63 | + "BAR" => Ok("bar_value".to_owned()), |
| 64 | + "BAZ" => Err(VarError::NotPresent), |
| 65 | + _ => panic!("Unexpected"), |
| 66 | + }; |
| 67 | + |
| 68 | + assert_eq!( |
| 69 | + substitute_env_vars_impl("$FOO", getter).unwrap(), |
| 70 | + "foo_value" |
| 71 | + ); |
| 72 | + assert_eq!( |
| 73 | + substitute_env_vars_impl("$FOO$BAR", getter).unwrap(), |
| 74 | + "foo_valuebar_value" |
| 75 | + ); |
| 76 | + assert_eq!( |
| 77 | + substitute_env_vars_impl("some$FOO.bar", getter).unwrap(), |
| 78 | + "somefoo_value.bar" |
| 79 | + ); |
| 80 | + assert_eq!(substitute_env_vars_impl("foo", getter).unwrap(), "foo"); |
| 81 | + assert_eq!(substitute_env_vars_impl("FOO", getter).unwrap(), "FOO"); |
| 82 | + assert!(substitute_env_vars_impl("$FOO$BAZ", getter).is_err()); |
| 83 | + } |
| 84 | +} |
0 commit comments