Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support System env value #29704

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Absolute path URL provider.
Expand All @@ -55,10 +57,28 @@ public byte[] getContent(final String url, final String urlPrefix) {
String line;
while (null != (line = reader.readLine())) {
if (!line.startsWith("#")) {
line = replaceVariables(line);
builder.append(line).append('\n');
}
}
return builder.toString().getBytes(StandardCharsets.UTF_8);
}
}

private String replaceVariables(final String line) {
Pattern variablePattern = Pattern.compile("\\$\\{(.+?)\\}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I should know where the conflict is going to be, but using #{(.+?)} would be considered a comment, so my current approach may not work well.

Matcher matcher = variablePattern.matcher(line);
StringBuffer modifiedLine = new StringBuffer();
while (matcher.find()) {
String variable = matcher.group(1);
String env = variable.split(":")[0];
String value = System.getenv(env);
if (value == null || value.isEmpty()) {
value = variable.split(":")[1];
}
matcher.appendReplacement(modifiedLine, value);
return modifiedLine.toString();
}
return line;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Classpath URL provider.
Expand All @@ -53,6 +55,7 @@ public byte[] getContent(final String url, final String urlPrefix) {
String line;
while (null != (line = reader.readLine())) {
if (!line.startsWith("#")) {
line = replaceVariables(line);
builder.append(line).append('\n');
}
}
Expand All @@ -68,4 +71,21 @@ private InputStream getResourceAsStream(final String resource) {
}
throw new IllegalArgumentException(String.format("Can not find configuration file `%s`.", resource));
}

private String replaceVariables(final String line) {
Pattern variablePattern = Pattern.compile("\\$\\{(.+?)\\}");
Matcher matcher = variablePattern.matcher(line);
StringBuffer modifiedLine = new StringBuffer();
while (matcher.find()) {
String variable = matcher.group(1);
String env = variable.split(":")[0];
String value = System.getenv(env);
if (value == null || value.isEmpty()) {
value = variable.split(":")[1];
}
matcher.appendReplacement(modifiedLine, value);
return modifiedLine.toString();
}
return line;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dataSources:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: org.h2.Driver
jdbcUrl: jdbc:h2:mem:foo_ds_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL
username: sa
username: ${DATASOURCE_USERNAME:sa}
password:
ds_1:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
Expand Down
Loading