mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-18 06:02:41 +01:00
Add support for configuring the plugin using environment variables.
Examples: - Plugin.ServerName -> PLAN_PLUGIN_SERVERNAME - Database.MySQL.Password -> PLAN_DATABASE_MYSQL_PASSWORD Affects issues: - Close #1353 - #1991
This commit is contained in:
parent
132fa2f919
commit
24a8c75b67
@ -101,6 +101,15 @@ dependencies {
|
||||
testImplementation "org.testcontainers:nginx:$testContainersVersion"
|
||||
}
|
||||
|
||||
test {
|
||||
environment "PLAN_TEST_NODE_STRING", "String"
|
||||
environment "PLAN_TEST_NODE_BOOLEAN", "true"
|
||||
environment "PLAN_TEST_NODE_INTEGER", "5"
|
||||
environment "PLAN_TEST_NODE_DOUBLE", "0.5"
|
||||
environment "PLAN_TEST_NODE_LONG", "9223372036854775807"
|
||||
environment "PLAN_TEST_NODE_STRINGLIST", "- Test\n- Another"
|
||||
}
|
||||
|
||||
task updateVersion(type: Copy) {
|
||||
from('src/main/resources') {
|
||||
include 'plugin.yml'
|
||||
|
@ -200,6 +200,14 @@ public class ConfigNode {
|
||||
return key;
|
||||
}
|
||||
|
||||
private String getEnvironmentVariableKey() {
|
||||
String deepKey = parent != null ? parent.getKey(true) + "." + key : "";
|
||||
if (deepKey.startsWith(".")) {
|
||||
deepKey = deepKey.substring(1);
|
||||
}
|
||||
return "PLAN_" + StringUtils.replaceChars(StringUtils.upperCase(deepKey), '.', '_');
|
||||
}
|
||||
|
||||
public void sort() {
|
||||
Collections.sort(nodeOrder);
|
||||
}
|
||||
@ -255,32 +263,51 @@ public class ConfigNode {
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
private String getEnvironmentVariable() {
|
||||
String key = getEnvironmentVariableKey();
|
||||
String variable = System.getenv(key);
|
||||
Map<String, String> env = System.getenv();
|
||||
return variable;
|
||||
}
|
||||
|
||||
public List<String> getStringList() {
|
||||
String environmentVariable = getEnvironmentVariable();
|
||||
if (environmentVariable != null) return new ConfigValueParser.StringListParser().compose(environmentVariable);
|
||||
return value == null ? Collections.emptyList()
|
||||
: new ConfigValueParser.StringListParser().compose(value);
|
||||
}
|
||||
|
||||
public Integer getInteger() {
|
||||
String environmentVariable = getEnvironmentVariable();
|
||||
if (environmentVariable != null) return new ConfigValueParser.IntegerParser().compose(environmentVariable);
|
||||
return value == null ? null
|
||||
: new ConfigValueParser.IntegerParser().compose(value);
|
||||
}
|
||||
|
||||
public Long getLong() {
|
||||
String environmentVariable = getEnvironmentVariable();
|
||||
if (environmentVariable != null) return new ConfigValueParser.LongParser().compose(environmentVariable);
|
||||
return value == null ? null
|
||||
: new ConfigValueParser.LongParser().compose(value);
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
String environmentVariable = getEnvironmentVariable();
|
||||
if (environmentVariable != null) return new ConfigValueParser.StringParser().compose(environmentVariable);
|
||||
return value == null ? null
|
||||
: new ConfigValueParser.StringParser().compose(value);
|
||||
}
|
||||
|
||||
public Double getDouble() {
|
||||
String environmentVariable = getEnvironmentVariable();
|
||||
if (environmentVariable != null) return new ConfigValueParser.DoubleParser().compose(environmentVariable);
|
||||
return value == null ? null
|
||||
: new ConfigValueParser.DoubleParser().compose(value);
|
||||
}
|
||||
|
||||
public boolean getBoolean() {
|
||||
String environmentVariable = getEnvironmentVariable();
|
||||
if (environmentVariable != null) return new ConfigValueParser.BooleanParser().compose(environmentVariable);
|
||||
return new ConfigValueParser.BooleanParser().compose(value);
|
||||
}
|
||||
|
||||
|
@ -39,6 +39,8 @@ public interface ConfigValueParser<T> {
|
||||
return new BooleanParser();
|
||||
} else if (Long.class.isAssignableFrom(type)) {
|
||||
return new LongParser();
|
||||
} else if (Double.class.isAssignableFrom(type)) {
|
||||
return new DoubleParser();
|
||||
} else if (Integer.class.isAssignableFrom(type)) {
|
||||
return new IntegerParser();
|
||||
}
|
||||
|
@ -393,4 +393,52 @@ class ConfigNodeTest {
|
||||
});
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void environmentVariableString() {
|
||||
ConfigNode node = new ConfigNode("Test.Node.String", new ConfigNode("", null, null), null);
|
||||
String expected = "String";
|
||||
String result = node.getString();
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void environmentVariableBoolean() {
|
||||
ConfigNode node = new ConfigNode("Test.Node.Boolean", new ConfigNode("", null, null), null);
|
||||
Boolean expected = true;
|
||||
Boolean result = node.getBoolean();
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void environmentVariableInteger() {
|
||||
ConfigNode node = new ConfigNode("Test.Node.Integer", new ConfigNode("", null, null), null);
|
||||
Integer expected = 5;
|
||||
Integer result = node.getInteger();
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void environmentVariableDouble() {
|
||||
ConfigNode node = new ConfigNode("Test.Node.Double", new ConfigNode("", null, null), null);
|
||||
Double expected = 0.5;
|
||||
Double result = node.getDouble();
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void environmentVariableLong() {
|
||||
ConfigNode node = new ConfigNode("Test.Node.Long", new ConfigNode("", null, null), null);
|
||||
Long expected = Long.MAX_VALUE;
|
||||
Long result = node.getLong();
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void environmentVariableStringList() {
|
||||
ConfigNode node = new ConfigNode("Test.Node.StringList", new ConfigNode("", null, null), null);
|
||||
List<String> expected = List.of("Test", "Another");
|
||||
List<String> result = node.getStringList();
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user