Skip to content

Commit

Permalink
Fixes an issue with returning value when using parent-env
Browse files Browse the repository at this point in the history
  • Loading branch information
haroon-sheikh committed Sep 16, 2022
1 parent 7003c1b commit b594080
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 39 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project are documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 1.2.1

### Fixed

- Fixes an issue with returning value when using parent-env

## 1.2.0

### Updated
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.github.sitture</groupId>
<artifactId>env-config</artifactId>
<packaging>jar</packaging>
<version>1.2.0</version>
<version>1.2.1</version>

<name>env-config</name>
<description>A simple utility to manage environment configs in Java-based projects by merging *.properties files with environment variables overrides.</description>
Expand Down
65 changes: 32 additions & 33 deletions src/main/java/com/github/sitture/envconfig/EnvConfigLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class EnvConfigLoader {

private static final Logger LOG = LoggerFactory.getLogger(EnvConfigLoader.class);
private static final int MIN_ENVIRONMENTS = 1;
private static final int MIN_ENVIRONMENTS = 2;
protected final CompositeConfiguration configuration = new CompositeConfiguration();
protected final EnvConfigProperties configProperties = new EnvConfigProperties();

Expand Down Expand Up @@ -48,51 +48,50 @@ private void loadEnvConfigurations(final List<String> envs) {
LOG.debug("Loading properties from system.properties");
this.configuration.addConfiguration(envVars.getSystemConfiguration());
final Configuration envOverrides = envVars.getEnvironmentConfiguration();

if (envs.size() > MIN_ENVIRONMENTS) {
envs.stream().filter(env -> !env.equals(EnvConfigUtils.CONFIG_ENV_DEFAULT)).forEach(env -> processEnvOverrides(envOverrides, env));
try {
for (final File file : new EnvConfigFileList(configProperties.getConfigPath(EnvConfigUtils.CONFIG_ENV_DEFAULT)).listFiles()) {
final Configuration defaultConfig = new Configurations().properties(file);
defaultConfig.getKeys().forEachRemaining(property -> {
if (envOverrides.containsKey(property)
&& envOverrides.getProperty(property).equals(defaultConfig.getProperty(property))) {
envOverrides.clearProperty(property);
}
});
}
} catch (ConfigurationException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Could not load configuration files. \n {}", e.getMessage());
}
}
}
LOG.debug("Loading properties from system.env");
this.configuration.addConfiguration(envOverrides);
}

private void processEnvOverrides(final Configuration envOverrides, final String env) {
try {
for (final File file : new EnvConfigFileList(configProperties.getConfigPath(env)).listFiles()) {
final Configuration properties = new Configurations().properties(file);
properties.getKeys().forEachRemaining(property -> {
if (envOverrides.containsKey(property) && envOverrides.getProperty(property).equals(properties.getProperty(property))) {
envOverrides.clearProperty(property);
}
});
}
} catch (ConfigurationException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Could not load configuration files. \n {}", e.getMessage());
}
}
}

private void loadFileConfigurations(final EnvConfigFileList fileList) {
if (fileList.listFiles().isEmpty()) {
LOG.debug("No property files found under {}", fileList.configPath);
}
fileList.listFiles().forEach(file ->
this.configuration.addConfiguration(new MapConfiguration(getFileConfigurationMap(file))));
}

private Map<String, Object> getFileConfigurationMap(final File file) {
final Map<String, Object> configurationMap = new HashMap<>();
try {
for (final File file : fileList.listFiles()) {
final Configuration config = new Configurations().properties(file);
final Map<String, Object> propertiesMap = new HashMap<>();
config.getKeys().forEachRemaining(key -> {
final Object value = config.getProperty(key);
propertiesMap.put(key, value);
propertiesMap.put(EnvConfigUtils.getProcessedEnvKey(key), value);
});
LOG.debug("Loading properties from {}", file);
this.configuration.addConfiguration(new MapConfiguration(propertiesMap));
}
LOG.debug("Loading properties from {}", file);
final Configuration config = new Configurations().properties(file);
config.getKeys().forEachRemaining(key -> {
final Object value = config.getProperty(key);
configurationMap.put(key, value);
configurationMap.put(EnvConfigUtils.getProcessedEnvKey(key), value);
});
} catch (ConfigurationException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Could not load configuration files. \n {}", e.getMessage());
}
throw new RuntimeException(e);
}
return configurationMap;
}

}
10 changes: 5 additions & 5 deletions src/test/java/com/github/sitture/envconfig/EnvConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ void testCanGetEntryWhenEnvVarAndDefaultValueSameWithParentEnv() {
final String testEnv = "test-env";
System.setProperty(EnvConfigUtils.CONFIG_ENV_KEY, "test," + testEnv);
// when property.five is set as env variable
// and does not exist in test-env
// and exists in default env with same value as env var
// and exists in test env with different value
// then value from env variable takes priority
Assertions.assertEquals("default", EnvConfig.get("property.five"));
// and does not exist in test-env
// then value in test env takes priority
Assertions.assertEquals("test", EnvConfig.get("property.five"));
}

@Test
Expand Down Expand Up @@ -197,8 +197,8 @@ void testCanGetEntryWhenEnvVarAndDefaultValueSameWithMultipleEnv() {
environmentVariables.set("PROPERTY_ONE", "default");
// and property.one is also set in test-env and test env properties
System.setProperty(EnvConfigUtils.CONFIG_ENV_KEY, "test,test-env");
// then value from env variable takes priority
Assertions.assertEquals("default", EnvConfig.get(key));
// then value in env test-env takes priority
Assertions.assertEquals("test-env", EnvConfig.get(key));
}

@Test
Expand Down

0 comments on commit b594080

Please sign in to comment.