-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationBuilderExtensions.cs
More file actions
58 lines (51 loc) · 2.47 KB
/
ConfigurationBuilderExtensions.cs
File metadata and controls
58 lines (51 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
namespace Neolution.Extensions.Configuration.GoogleSecrets
{
using System;
using Microsoft.Extensions.Configuration;
/// <summary>
/// The Google Secrets Extensions
/// </summary>
public static class ConfigurationBuilderExtensions
{
/// <summary>
/// Adds the Google secrets to the <see cref="ConfigurationBuilder"/>.
/// If the GOOGLE_SECRETS_PROJECT environment variable is set, it will be used as the project name.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <returns>The IConfigurationBuilder</returns>
/// <exception cref="System.ArgumentNullException">options</exception>
public static IConfigurationBuilder AddGoogleSecrets(this IConfigurationBuilder configuration)
{
// Configure app configuration to add Google Secrets if environment variable is set
var googleSecretProject = Environment.GetEnvironmentVariable(EnvironmentVariableNames.GoogleSecretsProject);
if (!string.IsNullOrWhiteSpace(googleSecretProject))
{
return AddGoogleSecrets(configuration, options =>
{
options.ProjectName = googleSecretProject;
});
}
return AddGoogleSecrets(configuration, _ => { });
}
/// <summary>
/// Adds the Google secrets to the <see cref="ConfigurationBuilder"/>.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="options">The options.</param>
/// <returns>The IConfigurationBuilder</returns>
/// <exception cref="System.ArgumentNullException">options</exception>
public static IConfigurationBuilder AddGoogleSecrets(this IConfigurationBuilder configuration, Action<GoogleSecretsOptions> options)
{
_ = configuration ?? throw new ArgumentNullException(nameof(configuration));
_ = options ?? throw new ArgumentNullException(nameof(options));
var googleSecretsOptions = new GoogleSecretsOptions();
options(googleSecretsOptions);
if (string.IsNullOrWhiteSpace(googleSecretsOptions.ProjectName))
{
throw new ArgumentNullException(nameof(googleSecretsOptions.ProjectName));
}
configuration.Add(new GoogleSecretsSource(googleSecretsOptions, configuration.Build()));
return configuration;
}
}
}