| | | 1 | | namespace Envilder.Infrastructure.Configuration; |
| | | 2 | | |
| | | 3 | | using Envilder.Application; |
| | | 4 | | using Envilder.Domain.Ports; |
| | | 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 against the given <paramref name="secretProvider"/>, |
| | | 18 | | /// 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="secretProvider">The secret provider to resolve values from.</param> |
| | | 23 | | /// <returns>The same <see cref="IConfigurationBuilder"/> for chaining.</returns> |
| | | 24 | | /// <exception cref="ArgumentException"><paramref name="mapFilePath"/> is null or empty.</exception> |
| | | 25 | | /// <exception cref="FileNotFoundException">The map file does not exist on disk.</exception> |
| | | 26 | | public static IConfigurationBuilder AddEnvilder(this IConfigurationBuilder builder, |
| | | 27 | | string mapFilePath, |
| | | 28 | | ISecretProvider secretProvider) |
| | | 29 | | { |
| | 1 | 30 | | if (secretProvider is null) |
| | | 31 | | { |
| | 0 | 32 | | throw new ArgumentNullException(nameof(secretProvider)); |
| | | 33 | | } |
| | | 34 | | |
| | 1 | 35 | | if (string.IsNullOrWhiteSpace(mapFilePath)) |
| | | 36 | | { |
| | 1 | 37 | | throw new ArgumentException("Map file path cannot be null or empty.", nameof(mapFilePath)); |
| | | 38 | | } |
| | | 39 | | |
| | 1 | 40 | | if (!File.Exists(mapFilePath)) |
| | | 41 | | { |
| | 1 | 42 | | throw new FileNotFoundException("Map file not found.", mapFilePath); |
| | | 43 | | } |
| | | 44 | | |
| | 1 | 45 | | var json = File.ReadAllText(mapFilePath); |
| | 1 | 46 | | var mapFile = new MapFileParser().Parse(json); |
| | 1 | 47 | | var client = new EnvilderClient(secretProvider); |
| | | 48 | | |
| | 1 | 49 | | return builder.Add(new EnvilderConfigurationSource(client, mapFile)); |
| | | 50 | | } |
| | | 51 | | } |