mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2024-11-22 00:38:05 -06:00
Atfile environment variables with default values
This commit is contained in:
parent
ee271124f5
commit
2352521dfa
@ -217,15 +217,20 @@ public class NBEnvironment {
|
||||
* @return The interpolated value, after substitutions, or null if any lookup failed
|
||||
*/
|
||||
public Optional<String> interpolate(String word, Map<String,String> supplemental) {
|
||||
Pattern envpattern = Pattern.compile("(\\$(?<env1>[a-zA-Z_][A-Za-z0-9_.]+)|\\$\\{(?<env2>[^}]+)\\})");
|
||||
Pattern envpattern = Pattern.compile("\\$(?<env1>[a-zA-Z_][A-Za-z0-9_.]+)|\\$\\{(?<env2>[a-zA-Z_][A-Za-z0-9_.]*)(:(?<default>[a-zA-Z0-9_.]+))?\\}");
|
||||
Matcher matcher = envpattern.matcher(word);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while (matcher.find()) {
|
||||
String envvar = matcher.group("env1");
|
||||
String defaultValue = null;
|
||||
if (envvar == null) {
|
||||
envvar = matcher.group("env2");
|
||||
defaultValue = matcher.group("default");
|
||||
}
|
||||
String value = peek(envvar,supplemental);
|
||||
if (value == null) {
|
||||
value = defaultValue;
|
||||
}
|
||||
if (value == null) {
|
||||
if (logger != null) {
|
||||
logger.debug("no value found for '" + envvar + "', returning Optional.empty() for '" + word + "'");
|
||||
|
@ -97,6 +97,12 @@ class NBAtFileTest {
|
||||
assertThat(strings).containsExactly("My value environment");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAtfileEnvironmentVariableDefault() {
|
||||
LinkedList<String> strings = NBAtFile.includeAt("@src/test/resources/atfiles/environment_variable_default.yaml");
|
||||
assertThat(strings).containsExactly("My default value environment");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAtfileMissingEnvironmentVariable() {
|
||||
assertThrows(RuntimeException.class, () -> NBAtFile.includeAt("@src/test/resources/atfiles/environment_variable_missing.yaml:>:"));
|
||||
|
@ -0,0 +1 @@
|
||||
- My ${MISSING_ENV_VAR:default value} environment
|
Loading…
Reference in New Issue
Block a user