Skip to content

Commit

Permalink
Store overridden environment variables with the correct type
Browse files Browse the repository at this point in the history
This should resolve the issue in #53 where the `AVA_DATABASE_VERIFYSERVERCERTIFICATE` environment variable isn't set correctly.
  • Loading branch information
Senither committed Nov 16, 2018
1 parent 52933d6 commit 2361d10
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -7,7 +7,7 @@ plugins {
apply plugin: 'java'
apply plugin: 'idea'

version = '0.8.84'
version = '0.8.85'
group = 'com.avairebot'
description = 'AvaIre Discord Bot'
mainClassName = 'com.avairebot.Main'
Expand Down
33 changes: 32 additions & 1 deletion src/main/java/com/avairebot/config/EnvironmentOverride.java
Expand Up @@ -21,12 +21,17 @@

package com.avairebot.config;

import com.avairebot.utilities.ComparatorUtil;
import com.avairebot.utilities.NumberUtil;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.Collections.EMPTY_LIST;

Expand Down Expand Up @@ -100,7 +105,7 @@ public static void overrideWithPrefix(String prefix, @Nonnull Configuration conf
continue;
}

configuration.set(key, env);
setConfigurationValue(configuration, key, env);

log.debug(
"\"{}\" has been set to \"{}\" using the \"{}\" environment string in the \"{}\" config.",
Expand All @@ -109,6 +114,32 @@ public static void overrideWithPrefix(String prefix, @Nonnull Configuration conf
}
}

private static void setConfigurationValue(Configuration configuration, String key, String value) {
if (configuration.isBoolean(key)) {
configuration.set(key, ComparatorUtil.getFuzzyType(value).getValue());
} else if (configuration.isInt(key)) {
configuration.set(key, NumberUtil.parseInt(value, configuration.getInt(key)));
} else if (configuration.isLong(key)) {
try {
configuration.set(key, Long.parseLong(value));
} catch (NumberFormatException ignored) {

}
} else if (configuration.isList(key)) {
configuration.set(key, convertStringToList(value));
} else if (configuration.isSet(key)) {
configuration.set(key, Sets.newHashSet(convertStringToList(value)));
} else {
configuration.set(key, value);
}
}

private static List<String> convertStringToList(String value) {
return Arrays.stream(value.split(";"))
.map(String::trim)
.collect(Collectors.toList());
}

private static String buildEnvironmentString(@Nullable String prefix, String key) {
String env = String.join("_", key.split("\\."))
.replaceAll("-", "_")
Expand Down

0 comments on commit 2361d10

Please sign in to comment.