Skip to content

Commit

Permalink
Fix #1479: Expand environment variables in config
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Tercete committed Oct 18, 2019
1 parent ae43814 commit 042b9da
Showing 1 changed file with 20 additions and 0 deletions.
Expand Up @@ -31,6 +31,8 @@
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Configuration-related utilities.
Expand Down Expand Up @@ -311,13 +313,31 @@ public static Map<String, String> loadConfigurationFile(File configFile, String
try {
String contents = FileCopyUtils.copyToString(new InputStreamReader(new FileInputStream(configFile), encoding));
Properties properties = new Properties();
contents = expandEnvironmentVariables(contents, System.getenv());
properties.load(new StringReader(contents.replace("\\", "\\\\")));
return propertiesToMap(properties);
} catch (IOException e) {
throw new FlywayException(errorMessage, e);
}
}

static String expandEnvironmentVariables(String value, Map<String, String> environmentVariables) {
Pattern pattern = Pattern.compile("\\$\\{([A-Za-z0-9_]+)}");
Matcher matcher = pattern.matcher(value);
String expandedValue = value;

while (matcher.find()) {
String variableName = matcher.group(1);
String variableValue = environmentVariables.containsKey(variableName)
? environmentVariables.get(variableName)
: "";

expandedValue = expandedValue.replaceAll(Pattern.quote(matcher.group(0)), variableValue);
}

return expandedValue;
}

/**
* Converts this Properties object into a map.
*
Expand Down

0 comments on commit 042b9da

Please sign in to comment.