Skip to content

Commit b7f87dc

Browse files
committed
Enable setting config via cmd line args
1 parent f602f90 commit b7f87dc

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
6+
namespace Microsoft.Extensions.Configuration
7+
{
8+
public static class ConfigurationExtensions
9+
{
10+
public static IConfigurationBuilder AddConfiguration(this IConfigurationBuilder builder, IConfiguration source)
11+
{
12+
return builder.Add(new ConfigurationConfigurationProvider(source));
13+
}
14+
15+
public static IConfigurationBuilder AddConfiguration(this IConfigurationBuilder builder, string prefix, IConfiguration source)
16+
{
17+
return builder.Add(new ConfigurationConfigurationProvider(prefix, source));
18+
}
19+
}
20+
21+
public class ConfigurationConfigurationProvider : ConfigurationProvider
22+
{
23+
public ConfigurationConfigurationProvider(IConfiguration source)
24+
: this(null, source)
25+
{
26+
27+
}
28+
29+
public ConfigurationConfigurationProvider(string prefix, IConfiguration source)
30+
{
31+
if (source == null)
32+
{
33+
throw new ArgumentNullException(nameof(source));
34+
}
35+
36+
if (prefix == null)
37+
{
38+
foreach (var pair in source.GetChildren())
39+
{
40+
Data.Add(pair.Key, pair.Value);
41+
}
42+
}
43+
else
44+
{
45+
var pairs = source.GetChildren();
46+
47+
foreach (var pair in pairs)
48+
{
49+
if (pair.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
50+
{
51+
Data.Add(pair.Key.Substring(prefix.Length), pair.Value);
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}

src/Benchmarks/Startup.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public Startup(IHostingEnvironment env)
2424
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
2525
.AddEnvironmentVariables();
2626

27+
if (env.Configuration != null)
28+
{
29+
// This allows passing of config values via the cmd line, e.g.: dnx web --app.EnableDbTests=true
30+
builder.AddConfiguration("app.", env.Configuration);
31+
}
32+
2733
Configuration = builder.Build();
2834

2935
Configuration.Bind(StartupOptions);

0 commit comments

Comments
 (0)