-
Notifications
You must be signed in to change notification settings - Fork 880
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into reuse-bonsai-historical-block-limit
Signed-off-by: Simon Dudley <[email protected]>
- Loading branch information
Showing
32 changed files
with
947 additions
and
360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
besu/src/main/java/org/hyperledger/besu/cli/config/ProfileName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.cli.config; | ||
|
||
/** Enum for profile names. Each profile corresponds to a configuration file. */ | ||
public enum ProfileName { | ||
/** The 'DEV' profile. Corresponds to the 'profiles/dev.toml' configuration file. */ | ||
DEV("profiles/dev.toml"); | ||
|
||
private final String configFile; | ||
|
||
/** | ||
* Constructs a new ProfileName. | ||
* | ||
* @param configFile the configuration file corresponding to the profile | ||
*/ | ||
ProfileName(final String configFile) { | ||
this.configFile = configFile; | ||
} | ||
|
||
/** | ||
* Gets the configuration file corresponding to the profile. | ||
* | ||
* @return the configuration file | ||
*/ | ||
public String getConfigFile() { | ||
return configFile; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
besu/src/main/java/org/hyperledger/besu/cli/util/AbstractConfigurationFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.cli.util; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import picocli.CommandLine; | ||
|
||
/** | ||
* Abstract class for finding configuration resources. This class provides a common structure for | ||
* classes that need to find configuration resources based on command line options and environment | ||
* variables. | ||
* | ||
* @param <T> the type of configuration resource this finder will return | ||
*/ | ||
public abstract class AbstractConfigurationFinder<T> { | ||
|
||
/** | ||
* Returns the name of the configuration option. | ||
* | ||
* @return the name of the configuration option | ||
*/ | ||
protected abstract String getConfigOptionName(); | ||
|
||
/** | ||
* Returns the name of the environment variable for the configuration. | ||
* | ||
* @return the name of the environment variable for the configuration | ||
*/ | ||
protected abstract String getConfigEnvName(); | ||
|
||
/** | ||
* Finds the configuration resource based on command line options and environment variables. | ||
* | ||
* @param environment the environment variables | ||
* @param parseResult the command line parse result | ||
* @return an Optional containing the configuration resource, or an empty Optional if no | ||
* configuration resource was found | ||
*/ | ||
public Optional<T> findConfiguration( | ||
final Map<String, String> environment, final CommandLine.ParseResult parseResult) { | ||
final CommandLine commandLine = parseResult.commandSpec().commandLine(); | ||
if (isConfigSpecifiedInBothSources(environment, parseResult)) { | ||
throwExceptionForBothSourcesSpecified(environment, parseResult, commandLine); | ||
} | ||
if (parseResult.hasMatchedOption(getConfigOptionName())) { | ||
return getFromOption(parseResult, commandLine); | ||
} | ||
if (environment.containsKey(getConfigEnvName())) { | ||
return getFromEnvironment(environment, commandLine); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
/** | ||
* Gets the configuration resource from the command line option. | ||
* | ||
* @param parseResult the command line parse result | ||
* @param commandLine the command line | ||
* @return an Optional containing the configuration resource, or an empty Optional if the | ||
* configuration resource was not specified in the command line option | ||
*/ | ||
protected abstract Optional<T> getFromOption( | ||
final CommandLine.ParseResult parseResult, final CommandLine commandLine); | ||
|
||
/** | ||
* Gets the configuration resource from the environment variable. | ||
* | ||
* @param environment the environment variables | ||
* @param commandLine the command line | ||
* @return an Optional containing the configuration resource, or an empty Optional if the | ||
* configuration resource was not specified in the environment variable | ||
*/ | ||
protected abstract Optional<T> getFromEnvironment( | ||
final Map<String, String> environment, final CommandLine commandLine); | ||
|
||
/** | ||
* Checks if the configuration resource is specified in both command line options and environment | ||
* variables. | ||
* | ||
* @param environment the environment variables | ||
* @param parseResult the command line parse result | ||
* @return true if the configuration resource is specified in both places, false otherwise | ||
*/ | ||
public boolean isConfigSpecifiedInBothSources( | ||
final Map<String, String> environment, final CommandLine.ParseResult parseResult) { | ||
return parseResult.hasMatchedOption(getConfigOptionName()) | ||
&& environment.containsKey(getConfigEnvName()); | ||
} | ||
|
||
/** | ||
* Throws an exception if the configuration resource is specified in both command line options and | ||
* environment variables. | ||
* | ||
* @param environment the environment variables | ||
* @param parseResult the command line parse result | ||
* @param commandLine the command line | ||
*/ | ||
public void throwExceptionForBothSourcesSpecified( | ||
final Map<String, String> environment, | ||
final CommandLine.ParseResult parseResult, | ||
final CommandLine commandLine) { | ||
throw new CommandLine.ParameterException( | ||
commandLine, | ||
String.format( | ||
"Both %s=%s and %s %s specified. Please specify only one.", | ||
getConfigEnvName(), | ||
getConfigOptionName(), | ||
environment.get(getConfigEnvName()), | ||
parseResult.matchedOption(getConfigOptionName()).stringValues())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
besu/src/main/java/org/hyperledger/besu/cli/util/ConfigFileFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.cli.util; | ||
|
||
import static org.hyperledger.besu.cli.DefaultCommandValues.CONFIG_FILE_OPTION_NAME; | ||
|
||
import java.io.File; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import picocli.CommandLine; | ||
|
||
/** | ||
* Class for finding configuration files. This class extends the AbstractConfigurationFinder and | ||
* provides methods for finding configuration files based on command line options and environment | ||
* variables. | ||
*/ | ||
public class ConfigFileFinder extends AbstractConfigurationFinder<File> { | ||
private static final String CONFIG_FILE_ENV_NAME = "BESU_CONFIG_FILE"; | ||
|
||
/** | ||
* Returns the name of the configuration option. | ||
* | ||
* @return the name of the configuration option | ||
*/ | ||
@Override | ||
protected String getConfigOptionName() { | ||
return CONFIG_FILE_OPTION_NAME; | ||
} | ||
|
||
/** | ||
* Returns the name of the environment variable for the configuration. | ||
* | ||
* @return the name of the environment variable for the configuration | ||
*/ | ||
@Override | ||
protected String getConfigEnvName() { | ||
return CONFIG_FILE_ENV_NAME; | ||
} | ||
|
||
/** | ||
* Gets the configuration file from the command line option. | ||
* | ||
* @param parseResult the command line parse result | ||
* @param commandLine the command line | ||
* @return an Optional containing the configuration file, or an empty Optional if the | ||
* configuration file was not specified in the command line option | ||
*/ | ||
@Override | ||
public Optional<File> getFromOption( | ||
final CommandLine.ParseResult parseResult, final CommandLine commandLine) { | ||
final CommandLine.Model.OptionSpec configFileOption = | ||
parseResult.matchedOption(CONFIG_FILE_OPTION_NAME); | ||
try { | ||
File file = configFileOption.getter().get(); | ||
if (!file.exists()) { | ||
throw new CommandLine.ParameterException( | ||
commandLine, | ||
String.format("Unable to read TOML configuration, file not found: %s", file)); | ||
} | ||
return Optional.of(file); | ||
} catch (final Exception e) { | ||
throw new CommandLine.ParameterException(commandLine, e.getMessage(), e); | ||
} | ||
} | ||
|
||
/** | ||
* Gets the configuration file from the environment variable. | ||
* | ||
* @param environment the environment variables | ||
* @param commandLine the command line | ||
* @return an Optional containing the configuration file, or an empty Optional if the | ||
* configuration file was not specified in the environment variable | ||
*/ | ||
@Override | ||
public Optional<File> getFromEnvironment( | ||
final Map<String, String> environment, final CommandLine commandLine) { | ||
final File toml = new File(environment.get(CONFIG_FILE_ENV_NAME)); | ||
if (!toml.exists()) { | ||
throw new CommandLine.ParameterException( | ||
commandLine, | ||
String.format( | ||
"TOML file %s specified in environment variable %s not found", | ||
CONFIG_FILE_ENV_NAME, environment.get(CONFIG_FILE_ENV_NAME))); | ||
} | ||
return Optional.of(toml); | ||
} | ||
} |
Oops, something went wrong.