From f41b6e5ec431c88bc1d94e4832d8ba49ddc42004 Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Tue, 5 Mar 2024 11:19:52 +0100 Subject: HomeserverEmulator work --- .../Services/HSEConfiguration.cs | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Tests/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs (limited to 'Tests/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs') diff --git a/Tests/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs b/Tests/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs new file mode 100644 index 0000000..73b0d23 --- /dev/null +++ b/Tests/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs @@ -0,0 +1,57 @@ +using System.Collections; +using System.Diagnostics.CodeAnalysis; +using ArcaneLibs.Extensions; + +namespace LibMatrix.HomeserverEmulator.Services; + +public class HSEConfiguration { + private static ILogger _logger; + public static HSEConfiguration Current { get; set; } + + [RequiresUnreferencedCode("Uses reflection binding")] + public HSEConfiguration(ILogger logger, IConfiguration config, HostBuilderContext host) { + Current = this; + _logger = logger; + logger.LogInformation("Loading configuration for environment: {}...", host.HostingEnvironment.EnvironmentName); + config.GetSection("HomeserverEmulator").Bind(this); + if (StoreData) { + DataStoragePath = ExpandPath(DataStoragePath ?? throw new NullReferenceException("DataStoragePath is not set")); + CacheStoragePath = ExpandPath(CacheStoragePath ?? throw new NullReferenceException("CacheStoragePath is not set")); + } + + _logger.LogInformation("Configuration loaded: {}", this.ToJson()); + } + + public string CacheStoragePath { get; set; } + + public string DataStoragePath { get; set; } + + public bool StoreData { get; set; } = true; + + public bool UnknownSyncTokenIsInitialSync { get; set; } = true; + + private static string ExpandPath(string path, bool retry = true) { + _logger.LogInformation("Expanding path `{}`", path); + + if (path.StartsWith('~')) { + path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), path[1..]); + } + + Environment.GetEnvironmentVariables().Cast().OrderByDescending(x => x.Key.ToString()!.Length).ToList().ForEach(x => { + path = path.Replace($"${x.Key}", x.Value.ToString()); + }); + + _logger.LogInformation("Expanded path to `{}`", path); + var tries = 0; + while (retry && path.ContainsAnyOf("~$".Split())) { + if (tries++ > 100) + throw new Exception($"Path `{path}` contains unrecognised environment variables"); + path = ExpandPath(path, false); + } + + if(path.StartsWith("./")) + path = Path.Join(Directory.GetCurrentDirectory(), path[2..].Replace("/", Path.DirectorySeparatorChar.ToString())); + + return path; + } +} \ No newline at end of file -- cgit 1.4.1