| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for license information. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using Microsoft.Extensions.Configuration; |
| | 6 | | using System.IO; |
| | 7 | |
|
| | 8 | | namespace KeyVault.TestFramework |
| | 9 | | { |
| | 10 | | public class TestConfigurationManager |
| | 11 | | { |
| | 12 | | public static string GetEnvironmentOrAppSetting(string settingName) |
| | 13 | | { |
| 0 | 14 | | var value = TryGetEnvironmentOrAppSetting(settingName, null); |
| | 15 | |
|
| 0 | 16 | | if (value == null) |
| 0 | 17 | | throw new Exception(string.Format("Missing configuration setting for {0}", settingName)); |
| | 18 | |
|
| 0 | 19 | | return value; |
| | 20 | | } |
| | 21 | |
|
| | 22 | | public static string TryGetEnvironmentOrAppSetting(string settingName, string defaultValue = null) |
| | 23 | | { |
| 32 | 24 | | var value = Environment.GetEnvironmentVariable(settingName); |
| | 25 | |
|
| | 26 | | // We don't use IsNullOrEmpty because an empty setting overrides what's on AppSettings. |
| 32 | 27 | | if (value == null) |
| | 28 | | { |
| 32 | 29 | | var config = new ConfigurationBuilder() |
| 32 | 30 | | .SetBasePath(Directory.GetCurrentDirectory()) |
| 32 | 31 | | .AddJsonFile("appsettings.json").Build(); |
| 32 | 32 | | value = config.GetSection("AppSettings:" + settingName).Value; |
| | 33 | | } |
| | 34 | |
|
| 32 | 35 | | return value ?? defaultValue; |
| | 36 | | } |
| | 37 | |
|
| | 38 | | public static string GetEnvironmentSetting(string settingName) |
| | 39 | | { |
| 0 | 40 | | var result = TryGetEnvironmentSetting(settingName, null); |
| | 41 | |
|
| 0 | 42 | | if (string.IsNullOrEmpty(result)) |
| 0 | 43 | | throw new Exception(string.Format("Missing environment variable for {0}", settingName)); |
| | 44 | |
|
| 0 | 45 | | return result; |
| | 46 | | } |
| | 47 | |
|
| | 48 | | public static string TryGetEnvironmentSetting(string settingName, string defaultValue = null) |
| | 49 | | { |
| 0 | 50 | | var result = Environment.GetEnvironmentVariable(settingName); |
| | 51 | |
|
| 0 | 52 | | if (string.IsNullOrWhiteSpace(result)) |
| 0 | 53 | | result = defaultValue; |
| | 54 | |
|
| 0 | 55 | | return result; |
| | 56 | | } |
| | 57 | | } |
| | 58 | | } |