Skip to content

Commit

Permalink
Change config file extensions to .json
Browse files Browse the repository at this point in the history
  • Loading branch information
rubo committed Oct 24, 2024
1 parent 7660ed2 commit 2f13e8e
Show file tree
Hide file tree
Showing 41 changed files with 39 additions and 34 deletions.
73 changes: 39 additions & 34 deletions src/Nethermind/Nethermind.Runner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,60 +374,65 @@ IConfigProvider CreateConfigProvider(ParseResult parseResult)
configProvider.AddSource(argsSource);
configProvider.AddSource(new EnvConfigSource());

string configDir = parseResult.GetResult(BasicOptions.ConfigurationDirectory)?.GetValueOrDefault<string>();
string configFilePath = parseResult.GetResult(BasicOptions.Configuration)?.GetValueOrDefault<string>();
string? configPathVariable = Environment.GetEnvironmentVariable("NETHERMIND_CONFIG");
string configsDir = parseResult.GetResult(BasicOptions.ConfigurationDirectory)?.GetValueOrDefault<string>();
string configFile = parseResult.GetResult(BasicOptions.Configuration)?.GetValueOrDefault<string>();
string? configFileEnvVar = Environment.GetEnvironmentVariable("NETHERMIND_CONFIG");

if (!string.IsNullOrWhiteSpace(configPathVariable))
if (!string.IsNullOrWhiteSpace(configFileEnvVar))
{
configFilePath = configPathVariable;
configFile = configFileEnvVar;
}

if (!PathUtils.IsExplicitlyRelative(configFilePath))
// If the configuration is a rooted path, don't handle it
if (!Path.IsPathRooted(configFile))
{
configFilePath = configDir == "configs"
? configFilePath.GetApplicationResourcePath()
: Path.Combine(configDir, string.Concat(configFilePath));
}
// If the configuration doesn't have any directory info or file extension,
// append a supported file extension
var shouldAppendExtension = string.IsNullOrEmpty(Path.GetDirectoryName(configFile)) &&
!Path.HasExtension(configFile);

if (!Path.HasExtension(configFilePath) && !configFilePath.Contains(Path.DirectorySeparatorChar))
{
string redirectedConfigPath = Path.Combine(configDir, string.Concat(configFilePath, ".cfg"));
configFilePath = redirectedConfigPath;
if (!File.Exists(configFilePath))
configFile = Path.Combine(configsDir, configFile);

// If the resulting path is still not rooted, combine with the current directory
if (!Path.IsPathRooted(configFile))
{
throw new InvalidOperationException($"Configuration: {configFilePath} was not found.");
configFile = Path.Combine(AppContext.BaseDirectory, configFile);
}
}

if (!Path.HasExtension(configFilePath))
{
configFilePath = string.Concat(configFilePath, ".cfg");
}

// Fallback to "{executingDirectory}/configs/{configFile}" if "configs" catalog was not specified.
if (!File.Exists(configFilePath))
{
string configName = Path.GetFileName(configFilePath);
string? configDirectory = Path.GetDirectoryName(configFilePath);
string redirectedConfigPath = Path.Combine(configDirectory ?? string.Empty, configDir, configName);
configFilePath = redirectedConfigPath;
if (!File.Exists(configFilePath))
if (shouldAppendExtension)
{
throw new InvalidOperationException($"Configuration: {configFilePath} was not found.");
string? fallback = null;

foreach (var ext in new[] { ".json", ".cfg" })
{
fallback = $"{configFile}{ext}";

if (File.Exists(fallback))
{
configFile = fallback;
break;
}
}
}
}

logger.Info($"Reading config file from {configFilePath}");
configProvider.AddSource(new JsonConfigSource(configFilePath));
if (!File.Exists(configFile))
throw new FileNotFoundException("Configuration not found.", configFile);

logger.Info($"Loading configuration from {configFile}");

configProvider.AddSource(new JsonConfigSource(configFile));
configProvider.Initialize();

var incorrectSettings = configProvider.FindIncorrectSettings();
if (incorrectSettings.Errors.Count > 0)

if (incorrectSettings.Errors.Any())
{
logger.Warn($"Incorrect config settings found:{Environment.NewLine}{incorrectSettings.ErrorMsg}");
}

logger.Info("Configuration initialized.");

return configProvider;
}

Expand Down

0 comments on commit 2f13e8e

Please sign in to comment.