about summary refs log tree commit diff
path: root/Tests/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-09-17 04:26:25 +0200
committerRory& <root@rory.gay>2024-09-17 04:26:25 +0200
commit37a8ac420278fd53ab8218956f1ba13692feb48e (patch)
tree83afec51130a254f6ed0ba11c45975f3f2ba8b9f /Tests/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs
parentDrop example bots (diff)
parentMove around some projects, further cleanup pending (diff)
downloadLibMatrix-bak-37a8ac420278fd53ab8218956f1ba13692feb48e.tar.xz
Merge remote-tracking branch 'origin/dev/project-cleanup'
Diffstat (limited to 'Tests/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs')
-rw-r--r--Tests/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs57
1 files changed, 0 insertions, 57 deletions
diff --git a/Tests/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs b/Tests/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs
deleted file mode 100644

index 73b0d23..0000000 --- a/Tests/LibMatrix.HomeserverEmulator/Services/HSEConfiguration.cs +++ /dev/null
@@ -1,57 +0,0 @@ -using System.Collections; -using System.Diagnostics.CodeAnalysis; -using ArcaneLibs.Extensions; - -namespace LibMatrix.HomeserverEmulator.Services; - -public class HSEConfiguration { - private static ILogger<HSEConfiguration> _logger; - public static HSEConfiguration Current { get; set; } - - [RequiresUnreferencedCode("Uses reflection binding")] - public HSEConfiguration(ILogger<HSEConfiguration> 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<DictionaryEntry>().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