Extract shared configuration helpers into Infrastructure.Configuration library (#248)

This commit is contained in:
2026-08-25 14:16:57 -04:00
committed by GitHub
parent 8a047e8d9f
commit 2b82d1918b
22 changed files with 161 additions and 64 deletions

View File

@@ -0,0 +1,32 @@
using Microsoft.Extensions.Configuration;
namespace Infrastructure.Configuration;
public static class ConfigurationHelpers
{
public static string GetKeyOrThrow(IConfiguration configuration, string key) =>
configuration[key]
?? throw new InvalidOperationException($"The {key} environment variable is not set.");
public static bool GetBooleanKeyOrThrow(IConfiguration configuration, string key)
{
string value = GetKeyOrThrow(configuration, key);
return bool.TryParse(value, out bool result)
? result
: throw new InvalidOperationException(
$"The {key} environment variable is set to '{value}', which is not a valid boolean."
);
}
public static int GetIntKeyOrThrow(IConfiguration configuration, string key)
{
string value = GetKeyOrThrow(configuration, key);
return int.TryParse(value, out int result)
? result
: throw new InvalidOperationException(
$"The {key} environment variable is set to '{value}', which is not a valid integer."
);
}
}