34 lines
1.2 KiB
C#
34 lines
1.2 KiB
C#
using VirtualDDNSRouter.Client.Context;
|
|
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 YamlStaticContextClient())
|
|
.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;
|
|
}
|
|
} |