Skip to content

Commit

Permalink
Optimize configuration loading and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
rubo committed Oct 30, 2024
1 parent d533291 commit 6e4527b
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Nethermind/Nethermind.Runner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,22 +381,23 @@ IConfigProvider CreateConfigProvider(ParseResult parseResult)
IConfigSource argsSource = new ArgsConfigSource(configArgs);
configProvider.AddSource(argsSource);
configProvider.AddSource(new EnvConfigSource());

string configsDir = parseResult.GetValue(BasicOptions.ConfigurationDirectory) ?? "configs";

string configFile = parseResult.GetValue(BasicOptions.Configuration)
?? Environment.GetEnvironmentVariable("NETHERMIND_CONFIG")
?? "mainnet";

// If configFile is not a path, handle it
if (string.IsNullOrEmpty(Path.GetDirectoryName(configFile)))
{
string configsDir = parseResult.GetValue(BasicOptions.ConfigurationDirectory) ?? "configs";

// If configsDir is rooted, AppContext.BaseDirectory is ignored
configFile = Path.Combine(AppContext.BaseDirectory, configsDir, configFile);

// If the configFile doesn't an extension, append a supported file extension
// If the configFile doesn't have an extension, try with supported file extensions
if (!Path.HasExtension(configFile))
{
string? fallback = null;
string? fallback;

foreach (var ext in new[] { ".json", ".cfg" })
{
Expand All @@ -411,8 +412,11 @@ IConfigProvider CreateConfigProvider(ParseResult parseResult)
}
}

// Resolve the full path for logging purposes
configFile = Path.GetFullPath(configFile);

if (!File.Exists(configFile))
throw new FileNotFoundException("Configuration not found.", configFile);
throw new FileNotFoundException("Configuration file not found.", configFile);

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

Expand All @@ -423,7 +427,7 @@ IConfigProvider CreateConfigProvider(ParseResult parseResult)

if (incorrectSettings.Errors.Any())
{
logger.Warn($"Incorrect config settings found:\n{incorrectSettings.ErrorMsg}");
logger.Warn($"Invalid configuration settings:\n{incorrectSettings.ErrorMsg}");
}

return configProvider;
Expand Down

0 comments on commit 6e4527b

Please sign in to comment.