Support both BESU_ and PANTHEON_ env var prefixes for backwards compatibility (#32)

BESU_ is preferred if both are set.

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/35/head
Adrian Sutton 5 years ago committed by GitHub
parent e3cc395cea
commit e0e7e88384
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      besu/src/main/java/org/hyperledger/besu/cli/util/EnvironmentVariableDefaultProvider.java
  2. 22
      besu/src/test/java/org/hyperledger/besu/cli/EnvironmentVariableDefaultProviderTest.java

@ -24,6 +24,7 @@ import picocli.CommandLine.Model.OptionSpec;
public class EnvironmentVariableDefaultProvider implements IDefaultValueProvider {
private static final String ENV_VAR_PREFIX = "BESU_";
private static final String LEGACY_ENV_VAR_PREFIX = "PANTHEON_";
private final Map<String, String> environment;
@ -43,9 +44,13 @@ public class EnvironmentVariableDefaultProvider implements IDefaultValueProvider
private Stream<String> envVarNames(final OptionSpec spec) {
return Arrays.stream(spec.names())
.filter(name -> name.startsWith("--")) // Only long options are allowed
.map(
.flatMap(
name ->
ENV_VAR_PREFIX
+ name.substring("--".length()).replace('-', '_').toUpperCase(Locale.US));
Stream.of(ENV_VAR_PREFIX, LEGACY_ENV_VAR_PREFIX)
.map(prefix -> prefix + nameToEnvVarSuffix(name)));
}
private String nameToEnvVarSuffix(final String name) {
return name.substring("--".length()).replace('-', '_').toUpperCase(Locale.US);
}
}

@ -40,6 +40,12 @@ public class EnvironmentVariableDefaultProviderTest {
assertThat(provider.defaultValue(OptionSpec.builder("--env-var-set").build())).isEqualTo("abc");
}
@Test
public void shouldReturnValueWhenEnvironmentVariableIsSetWithLegacyPrefix() {
environment.put("PANTHEON_ENV_VAR_SET", "abc");
assertThat(provider.defaultValue(OptionSpec.builder("--env-var-set").build())).isEqualTo("abc");
}
@Test
public void shouldReturnValueWhenEnvironmentVariableIsSetForAlternateName() {
environment.put("BESU_ENV_VAR_SET", "abc");
@ -47,9 +53,25 @@ public class EnvironmentVariableDefaultProviderTest {
.isEqualTo("abc");
}
@Test
public void shouldReturnValueWhenEnvironmentVariableIsSetForAlternateNameAndLegacyPrefix() {
environment.put("PANTHEON_ENV_VAR_SET", "abc");
assertThat(provider.defaultValue(OptionSpec.builder("--env-var", "--env-var-set").build()))
.isEqualTo("abc");
}
@Test
public void shouldNotReturnValueForShortOptions() {
environment.put("BESU_H", "abc");
environment.put("PANTHEON_H", "abc");
assertThat(provider.defaultValue(OptionSpec.builder("-h").build())).isNull();
}
@Test
public void shouldPreferBesuPrefixOverLegacyPrefix() {
environment.put("BESU_ENV_VAR_SET", "abc");
environment.put("PANTHEON_ENV_VAR_SET", "def");
assertThat(provider.defaultValue(OptionSpec.builder("--env-var", "--env-var-set").build()))
.isEqualTo("abc");
}
}

Loading…
Cancel
Save