-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dotenv configuration provider (#223)
* dotenv configuration provider * Documentation
- Loading branch information
Showing
8 changed files
with
447 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/Core/NBB.Core.Configuration/DotEnvConfigurationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.Extensions.FileProviders; | ||
using NBB.Core.Configuration.DotEnv; | ||
|
||
namespace Microsoft.Extensions.Configuration | ||
{ | ||
/// <summary> | ||
/// Extension methods for adding <see cref="DotEnvConfigurationProvider"/>. | ||
/// </summary> | ||
public static class DotEnvConfigurationExtensions | ||
{ | ||
/// <summary> | ||
/// Adds the DotEnv configuration provider at <paramref name="path"/> to <paramref name="builder"/>. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param> | ||
/// <param name="path">Path relative to the base path stored in | ||
/// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param> | ||
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns> | ||
public static IConfigurationBuilder AddDotEnvFile(this IConfigurationBuilder builder, string path) | ||
{ | ||
return AddDotEnvFile(builder, provider: null, path: path, optional: false, reloadOnChange: false); | ||
} | ||
|
||
/// <summary> | ||
/// Adds the DotEnv configuration provider at <paramref name="path"/> to <paramref name="builder"/>. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param> | ||
/// <param name="path">Path relative to the base path stored in | ||
/// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param> | ||
/// <param name="optional">Whether the file is optional.</param> | ||
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns> | ||
public static IConfigurationBuilder AddDotEnvFile(this IConfigurationBuilder builder, string path, bool optional) | ||
{ | ||
return AddDotEnvFile(builder, provider: null, path: path, optional: optional, reloadOnChange: false); | ||
} | ||
|
||
/// <summary> | ||
/// Adds the DotEnv configuration provider at <paramref name="path"/> to <paramref name="builder"/>. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param> | ||
/// <param name="path">Path relative to the base path stored in | ||
/// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param> | ||
/// <param name="optional">Whether the file is optional.</param> | ||
/// <param name="reloadOnChange">Whether the configuration should be reloaded if the file changes.</param> | ||
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns> | ||
public static IConfigurationBuilder AddDotEnvFile(this IConfigurationBuilder builder, string path, bool optional, bool reloadOnChange) | ||
{ | ||
return AddDotEnvFile(builder, provider: null, path: path, optional: optional, reloadOnChange: reloadOnChange); | ||
} | ||
|
||
/// <summary> | ||
/// Adds a DotEnv configuration source to <paramref name="builder"/>. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param> | ||
/// <param name="provider">The <see cref="IFileProvider"/> to use to access the file.</param> | ||
/// <param name="path">Path relative to the base path stored in | ||
/// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param> | ||
/// <param name="optional">Whether the file is optional.</param> | ||
/// <param name="reloadOnChange">Whether the configuration should be reloaded if the file changes.</param> | ||
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns> | ||
public static IConfigurationBuilder AddDotEnvFile(this IConfigurationBuilder builder, IFileProvider? provider, string path, bool optional, bool reloadOnChange) | ||
{ | ||
if (string.IsNullOrEmpty(path)) | ||
{ | ||
throw new ArgumentException("Invalid DotEnv file path", nameof(path)); | ||
} | ||
|
||
return builder.AddDotEnvFile(s => | ||
{ | ||
s.FileProvider = provider; | ||
s.Path = path; | ||
s.Optional = optional; | ||
s.ReloadOnChange = reloadOnChange; | ||
s.ResolveFileProvider(); | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// Adds a DotEnv configuration source to <paramref name="builder"/>. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param> | ||
/// <param name="configureSource">Configures the source.</param> | ||
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns> | ||
public static IConfigurationBuilder AddDotEnvFile(this IConfigurationBuilder builder, Action<DotEnvConfigurationSource>? configureSource) | ||
=> builder.Add(configureSource); | ||
|
||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/Core/NBB.Core.Configuration/DotEnvConfigurationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (c) TotalSoft. | ||
// This source code is licensed under the MIT license. | ||
|
||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace NBB.Core.Configuration.DotEnv | ||
{ | ||
/// <summary> | ||
/// An DotEnv file based <see cref="ConfigurationProvider"/>. | ||
/// Files are simple line structures with environment variable declarations as key-value pairs | ||
/// </summary> | ||
/// <examples> | ||
/// key1=value1 | ||
/// key2 = " value2 " | ||
/// # comment | ||
/// </examples> | ||
public class DotEnvConfigurationProvider : FileConfigurationProvider | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance with the specified source. | ||
/// </summary> | ||
/// <param name="source">The source settings.</param> | ||
public DotEnvConfigurationProvider(DotEnvConfigurationSource source) : base(source) { } | ||
|
||
/// <summary> | ||
/// Loads the DotEnv data from a stream. | ||
/// </summary> | ||
/// <param name="stream">The stream to read.</param> | ||
public override void Load(Stream stream) | ||
=> Data = Read(stream); | ||
|
||
/// <summary> | ||
/// Read a stream of DotEnv values into a key/value dictionary. | ||
/// </summary> | ||
/// <param name="stream">The stream of DotEnv data.</param> | ||
/// <returns>The <see cref="IDictionary{String, String}"/> which was read from the stream.</returns> | ||
private static IDictionary<string, string?> Read(Stream stream) | ||
{ | ||
var data = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase); | ||
using (var reader = new StreamReader(stream)) | ||
{ | ||
string sectionPrefix = string.Empty; | ||
|
||
while (reader.Peek() != -1) | ||
{ | ||
string rawLine = reader.ReadLine()!; | ||
string line = rawLine.Trim(); | ||
|
||
// Ignore blank lines | ||
if (string.IsNullOrWhiteSpace(line)) | ||
{ | ||
continue; | ||
} | ||
// Ignore comments | ||
if (line[0] is '#') | ||
{ | ||
continue; | ||
} | ||
|
||
// key = value OR "value" | ||
int separator = line.IndexOf('='); | ||
if (separator < 0) | ||
{ | ||
throw new FormatException($"Unrecognized line formmat: {rawLine}"); | ||
} | ||
|
||
string key = sectionPrefix + line.Substring(0, separator).Trim(); | ||
string value = line.Substring(separator + 1).Trim(); | ||
|
||
// Remove quotes | ||
if (value.Length > 1 && value[0] == '"' && value[value.Length - 1] == '"') | ||
{ | ||
value = value.Substring(1, value.Length - 2); | ||
} | ||
|
||
if (data.ContainsKey(key)) | ||
{ | ||
throw new FormatException($"Duplicated key: {key}"); | ||
} | ||
|
||
var normalizedKey = Normalize(key); | ||
data[normalizedKey] = value; | ||
} | ||
} | ||
return data; | ||
} | ||
|
||
private static string Normalize(string key) | ||
=> key.Replace("__", ConfigurationPath.KeyDelimiter); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Core/NBB.Core.Configuration/DotEnvConfigurationSource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) TotalSoft. | ||
// This source code is licensed under the MIT license. | ||
|
||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace NBB.Core.Configuration.DotEnv | ||
{ | ||
/// <summary> | ||
/// Represents a DotEnv file as an <see cref="IConfigurationSource"/>. | ||
/// Files are simple line structures with environment variable declarations as key-value pairs | ||
/// </summary> | ||
/// <examples> | ||
/// key1=value1 | ||
/// key2 = " value2 " | ||
/// # comment | ||
/// </examples> | ||
public class DotEnvConfigurationSource : FileConfigurationSource | ||
{ | ||
/// <summary> | ||
/// Builds the <see cref="IniConfigurationProvider"/> for this source. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param> | ||
/// <returns>An <see cref="IniConfigurationProvider"/></returns> | ||
public override IConfigurationProvider Build(IConfigurationBuilder builder) | ||
{ | ||
EnsureDefaults(builder); | ||
return new DotEnvConfigurationProvider(this); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Core/NBB.Core.Configuration/NBB.Core.Configuration.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="$(MicrosoftExtensionsPackagesVersion)" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="$(MicrosoftExtensionsPackagesVersion)" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="$(MicrosoftExtensionsPackagesVersion)" /> | ||
<PackageReference Include="Microsoft.Extensions.FileProviders.Abstractions" Version="$(MicrosoftExtensionsPackagesVersion)" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# NBB.Core.Configuration | ||
|
||
This package contains configuration extensions. | ||
|
||
## NuGet install | ||
``` | ||
dotnet add package NBB.Core.Configuration | ||
``` | ||
|
||
## DotEnv configuration provider | ||
Adds support for configuration files that contain environment variable declarations. | ||
|
||
### Registration | ||
```csharp | ||
var builder = new ConfigurationBuilder(); | ||
builder.AddDotEnvFile("config/env.txt", optional: false, reloadOnChange: true); | ||
``` | ||
|
||
Parameters: | ||
* `path` - The path of the configuration file relative to the base path. | ||
* `optional` - Whether the file is optional. | ||
* `reloadOnChange` - Whether the configuration should be reloaded if the file changes. | ||
|
||
### Config file example | ||
```ini | ||
# Serilog | ||
Serilog__MinimumLevel__Default = Information | ||
Serilog__MinimumLevel__Override__Microsoft = Warning | ||
|
||
# MultiTenancy | ||
MultiTenancy__Enabled = true | ||
MultiTenancy__Tenants__BCR__ConnectionStrings__App_Database__User_Name = TestUser | ||
MultiTenancy__Tenants__BCR__Description = "My description " | ||
``` | ||
|
||
Notes: | ||
* Each line should contain the key and the value separated by character `=` | ||
* For hierarchical settings, the "__" separator should be used. | ||
* Comments can be added in the file by starting the line with character `#`. | ||
* Empty lines and whitespace around keys/values are ignored. | ||
* Values that start or end with whitespace should be surounded by quotes. The quotes will not be part of the value. |
Oops, something went wrong.