| | | 1 | | namespace Envilder.Infrastructure.Configuration; |
| | | 2 | | |
| | | 3 | | using Envilder.Application; |
| | | 4 | | using Envilder.Domain; |
| | | 5 | | using Microsoft.Extensions.Configuration; |
| | | 6 | | using System; |
| | | 7 | | using System.IO; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Extension methods for integrating Envilder into the |
| | | 11 | | /// <see cref="IConfigurationBuilder"/> pipeline. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class ConfigurationBuilderExtensions |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Adds an Envilder configuration source that reads a JSON map file from disk, |
| | | 17 | | /// resolves every mapping via the provider specified in the file's <c>$config</c> |
| | | 18 | | /// section, and exposes the results as <see cref="IConfiguration"/> keys. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="builder">The configuration builder.</param> |
| | | 21 | | /// <param name="mapFilePath">Absolute or relative path to the JSON map file.</param> |
| | | 22 | | /// <param name="options">Optional runtime overrides (provider, profile, vault URL).</param> |
| | | 23 | | /// <returns>The same <see cref="IConfigurationBuilder"/> for chaining.</returns> |
| | | 24 | | public static IConfigurationBuilder AddEnvilder(this IConfigurationBuilder builder, |
| | | 25 | | string mapFilePath, |
| | | 26 | | EnvilderOptions? options = null) |
| | | 27 | | { |
| | 1 | 28 | | if (string.IsNullOrWhiteSpace(mapFilePath)) |
| | | 29 | | { |
| | 1 | 30 | | throw new ArgumentException("Map file path cannot be null or empty.", nameof(mapFilePath)); |
| | | 31 | | } |
| | | 32 | | |
| | 1 | 33 | | if (!File.Exists(mapFilePath)) |
| | | 34 | | { |
| | 1 | 35 | | throw new FileNotFoundException("Map file not found.", mapFilePath); |
| | | 36 | | } |
| | | 37 | | |
| | 1 | 38 | | var json = File.ReadAllText(mapFilePath); |
| | 1 | 39 | | var mapFile = new MapFileParser().Parse(json); |
| | 1 | 40 | | var provider = SecretProviderFactory.Create(mapFile.Config, options); |
| | 1 | 41 | | var client = new EnvilderClient(provider); |
| | | 42 | | |
| | 1 | 43 | | return builder.Add(new EnvilderConfigurationSource(client, mapFile)); |
| | | 44 | | } |
| | | 45 | | } |