39 lines
1.3 KiB
C#
39 lines
1.3 KiB
C#
using YamlDotNet.Serialization;
|
|
using YamlDotNet.Serialization.NamingConventions;
|
|
using Settings = VirtualDDNSRouter.Client.Models.Settings;
|
|
|
|
namespace VirtualDDNSRouter.Client.Helpers;
|
|
|
|
public class Helpers
|
|
{
|
|
private static readonly string YamlFilePath = "settings.yaml";
|
|
|
|
public static async Task<Settings> GetSettings()
|
|
{
|
|
if (!File.Exists(YamlFilePath))
|
|
throw new FileNotFoundException($"Settings file not found: {YamlFilePath}");
|
|
|
|
var yamlContent = await File.ReadAllTextAsync(YamlFilePath).ConfigureAwait(false);
|
|
|
|
// Build the deserializer with explicit naming convention
|
|
var deserializer = new StaticDeserializerBuilder(new YamlStaticContext())
|
|
.WithNamingConvention(UnderscoredNamingConvention.Instance)
|
|
.IgnoreUnmatchedProperties()
|
|
.Build();
|
|
|
|
// Deserialize into Settings
|
|
var settings = deserializer.Deserialize<Settings>(yamlContent);
|
|
|
|
if (settings is null || string.IsNullOrWhiteSpace(settings.Host) || string.IsNullOrWhiteSpace(settings.Path) ||
|
|
string.IsNullOrWhiteSpace(settings.ApiKey))
|
|
throw new Exception("Invalid settings file");
|
|
|
|
return settings;
|
|
}
|
|
|
|
[YamlStaticContext]
|
|
[YamlSerializable(typeof(Settings))]
|
|
private partial class YamlStaticContext : StaticContext
|
|
{
|
|
}
|
|
} |