openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
OpenAI/src/Utility/AppContextSwitchHelper.cs
33lines · modecode
| 1 | using System; |
| 2 | |
| 3 | namespace OpenAI; |
| 4 | |
| 5 | internal static class AppContextSwitchHelper |
| 6 | { |
| 7 | /// <summary> |
| 8 | /// Determines if either an AppContext switch or its corresponding Environment Variable is set |
| 9 | /// </summary> |
| 10 | /// <param name="appContextSwitchName">Name of the AppContext switch.</param> |
| 11 | /// <param name="environmentVariableName">Name of the Environment variable.</param> |
| 12 | /// <returns>If the AppContext switch has been set, returns the value of the switch. |
| 13 | /// If the AppContext switch has not been set, returns the value of the environment variable. |
| 14 | /// False if neither is set. |
| 15 | /// </returns> |
| 16 | public static bool GetConfigValue(string appContextSwitchName, string environmentVariableName) |
| 17 | { |
| 18 | // First check for the AppContext switch, giving it priority over the environment variable. |
| 19 | if (AppContext.TryGetSwitch(appContextSwitchName, out bool value)) |
| 20 | { |
| 21 | return value; |
| 22 | } |
| 23 | // AppContext switch wasn't used. Check the environment variable. |
| 24 | string envVar = Environment.GetEnvironmentVariable(environmentVariableName); |
| 25 | if (envVar != null && (envVar.Equals("true", StringComparison.OrdinalIgnoreCase) || envVar.Equals("1"))) |
| 26 | { |
| 27 | return true; |
| 28 | } |
| 29 | |
| 30 | // Default to false. |
| 31 | return false; |
| 32 | } |
| 33 | } |
| 34 | |