| | | 1 | | using Microsoft.Extensions.Configuration; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.IO.Abstractions; |
| | | 4 | | using System.Text; |
| | | 5 | | |
| | | 6 | | namespace Anichron.Infrastructure.Configuration; |
| | | 7 | | |
| | | 8 | | public sealed record IniEntry(string Section, string Key, Func<string> DefaultValue); |
| | | 9 | | |
| | 21 | 10 | | public sealed class AppIniInitializer(IFileSystem fileSystem) |
| | | 11 | | { |
| | | 12 | | public static string PathUnder(string contentRootPath) |
| | 2 | 13 | | => Path.Combine(contentRootPath, "configuration", "app.ini"); |
| | | 14 | | |
| | | 15 | | public void EnsureUpToDate(string iniPath, IReadOnlyList<IniEntry> expectedEntries) |
| | 21 | 16 | | { |
| | 21 | 17 | | fileSystem.Directory.CreateDirectory(fileSystem.Path.GetDirectoryName(iniPath)!); |
| | | 18 | | |
| | 21 | 19 | | if (!fileSystem.File.Exists(iniPath)) |
| | 12 | 20 | | { |
| | 12 | 21 | | WriteNew(iniPath, expectedEntries); |
| | 11 | 22 | | return; |
| | | 23 | | } |
| | | 24 | | |
| | 9 | 25 | | using var stream = new MemoryStream(Encoding.UTF8.GetBytes(fileSystem.File.ReadAllText(iniPath))); |
| | 9 | 26 | | var config = new ConfigurationBuilder().AddIniStream(stream).Build(); |
| | 9 | 27 | | var missing = expectedEntries |
| | 9 | 28 | | .Where(e => config[$"{e.Section}:{e.Key}"] is null) |
| | 9 | 29 | | .ToList(); |
| | | 30 | | |
| | 9 | 31 | | if (missing.Count == 0) |
| | 5 | 32 | | return; |
| | | 33 | | |
| | 4 | 34 | | AppendMissing(iniPath, missing); |
| | 24 | 35 | | } |
| | | 36 | | |
| | | 37 | | private void WriteNew(string iniPath, IReadOnlyList<IniEntry> entries) |
| | 12 | 38 | | { |
| | 12 | 39 | | var sb = new StringBuilder(); |
| | 76 | 40 | | foreach (var section in entries.GroupBy(e => e.Section)) |
| | 20 | 41 | | { |
| | 20 | 42 | | sb.AppendLine(CultureInfo.InvariantCulture, $"[{section.Key}]"); |
| | 118 | 43 | | foreach (var entry in section) |
| | 29 | 44 | | sb.AppendLine(CultureInfo.InvariantCulture, $"{entry.Key} = {entry.DefaultValue()}"); |
| | 20 | 45 | | sb.AppendLine(); |
| | 20 | 46 | | } |
| | | 47 | | |
| | | 48 | | try |
| | 12 | 49 | | { |
| | | 50 | | // FileMode.CreateNew is O_CREAT|O_EXCL — atomic exclusive creation; only one wins if two instances race. |
| | 12 | 51 | | using var stream = fileSystem.FileStream.New(iniPath, FileMode.CreateNew, FileAccess.Write, FileShare.None); |
| | 10 | 52 | | using var writer = new StreamWriter(stream); |
| | 10 | 53 | | writer.Write(sb.ToString()); |
| | 10 | 54 | | } |
| | 1 | 55 | | catch (IOException) |
| | 1 | 56 | | { |
| | 1 | 57 | | return; // Another instance created the file concurrently — proceed with that file. |
| | | 58 | | } |
| | | 59 | | |
| | 10 | 60 | | Console.Error.WriteLine( |
| | 10 | 61 | | $"[WARN] Configuration file created at {iniPath}. Review the values and keep this file secure."); |
| | 11 | 62 | | } |
| | | 63 | | |
| | | 64 | | private void AppendMissing(string iniPath, IReadOnlyList<IniEntry> missing) |
| | 4 | 65 | | { |
| | 4 | 66 | | var sb = new StringBuilder(); |
| | 26 | 67 | | foreach (var section in missing.GroupBy(e => e.Section)) |
| | 7 | 68 | | { |
| | 7 | 69 | | sb.AppendLine(); |
| | 7 | 70 | | sb.AppendLine(CultureInfo.InvariantCulture, $"[{section.Key}]"); |
| | 35 | 71 | | foreach (var entry in section) |
| | 7 | 72 | | sb.AppendLine(CultureInfo.InvariantCulture, $"{entry.Key} = {entry.DefaultValue()}"); |
| | 7 | 73 | | } |
| | | 74 | | |
| | | 75 | | // Not atomic: two processes racing here can both append the same entries. |
| | | 76 | | // The INI provider tolerates duplicate keys (last value wins), so behavior is correct. |
| | 4 | 77 | | fileSystem.File.AppendAllText(iniPath, sb.ToString()); |
| | 4 | 78 | | Console.Error.WriteLine($"[INFO] Added missing configuration entries to {iniPath}."); |
| | 4 | 79 | | } |
| | | 80 | | } |