Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for DAPRSIDEKICK_XXX variables #14

Merged
merged 1 commit into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ public static IDaprSidekickBuilder AddDaprSidekick(this IServiceCollection servi

var builder = AddCoreServices(services);

services.Configure<DaprOptions>(configuration.GetSection(name));
// Create a new configuration based on the initial configuration but with the additional
// support for setting/overriding top-level properties using environment variables.
var daprConfig = new ConfigurationBuilder()
.AddConfiguration(configuration.GetSection(name))
.AddEnvironmentVariables(DaprOptions.EnvironmentVariablePrefix)
.Build();
services.Configure<DaprOptions>(daprConfig);

if (postConfigureAction != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup Condition="'$(TargetFramework)' == 'net462' OR '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.AspNetCore.Hosting.Server.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="2.2.0" />
Expand Down
1 change: 1 addition & 0 deletions src/Man.Dapr.Sidekick/Options/DaprOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class DaprOptions : Options.DaprProcessOptions
{
public const string SectionName = "Dapr";
public const string EnvironmentVariablePrefix = "DAPRSIDEKICK_";

public DaprOptions()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,32 @@ public void Should_add_core_services_with_post_configure_action()
Assert.That(provider.GetRequiredService<IDaprProcessFactory>(), Is.Not.Null);
configuration.Received(1).GetSection("Dapr");
}

[Test]
public void Should_extend_config_with_environmentvars()
{
var configuration = new ConfigurationBuilder()
.AddCommandLine(new[]
{
DaprOptions.SectionName + ":BinDirectory=FROM_ARGS"
})
.Build();

// First check command
var services = new ServiceCollection();
services.AddDaprSidekick(configuration);
var provider = services.BuildServiceProvider();
var options = provider.GetRequiredService<IOptions<DaprOptions>>().Value;
Assert.That(options.BinDirectory, Is.EqualTo("FROM_ARGS"));

// Now add environment variables
Environment.SetEnvironmentVariable(DaprOptions.EnvironmentVariablePrefix + "BINDIRECTORY", "FROM_ENV");
services = new ServiceCollection();
services.AddDaprSidekick(configuration);
provider = services.BuildServiceProvider();
options = provider.GetRequiredService<IOptions<DaprOptions>>().Value;
Assert.That(options.BinDirectory, Is.EqualTo("FROM_ENV"));
}
}
}
}