diff --git a/besu/src/main/java/org/hyperledger/besu/cli/util/EnvironmentVariableDefaultProvider.java b/besu/src/main/java/org/hyperledger/besu/cli/util/EnvironmentVariableDefaultProvider.java index 35d429d4f8c..046df2f1f1a 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/util/EnvironmentVariableDefaultProvider.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/util/EnvironmentVariableDefaultProvider.java @@ -24,6 +24,7 @@ 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 environment; @@ -43,9 +44,13 @@ public String defaultValue(final ArgSpec argSpec) { private Stream 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); } } diff --git a/besu/src/test/java/org/hyperledger/besu/cli/EnvironmentVariableDefaultProviderTest.java b/besu/src/test/java/org/hyperledger/besu/cli/EnvironmentVariableDefaultProviderTest.java index 7c4d0119360..b36a3c0e3ea 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/EnvironmentVariableDefaultProviderTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/EnvironmentVariableDefaultProviderTest.java @@ -40,6 +40,12 @@ public void shouldReturnValueWhenEnvironmentVariableIsSet() { 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 void shouldReturnValueWhenEnvironmentVariableIsSetForAlternateName() { .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"); + } }