| | 1 | | namespace Common.Hosting; |
| | 2 | |
|
| | 3 | | public class GetEnvironmentCommand |
| | 4 | | { |
| | 5 | | private static string _environmentName; |
| | 6 | |
|
| | 7 | | public virtual string Execute() |
| 1 | 8 | | { |
| 1 | 9 | | _environmentName ??= GetEnvironmentName(); |
| 1 | 10 | | return _environmentName; |
| 1 | 11 | | } |
| | 12 | |
|
| | 13 | | protected virtual string GetEnvironmentKey() |
| 1 | 14 | | { |
| 1 | 15 | | string key = Environment.GetEnvironmentVariables().Keys.Cast<string>().FirstOrDefault(IsEnvironmentKey); |
| 1 | 16 | | return key; |
| 1 | 17 | | } |
| | 18 | |
|
| | 19 | | protected virtual List<string> GetEnvironmentKeys() |
| 59 | 20 | | { |
| 59 | 21 | | return new List<string> { "DOTNET_ENVIRONMENT", "ASPNETCORE_ENVIRONMENT" }; |
| 59 | 22 | | } |
| | 23 | |
|
| | 24 | | protected virtual string GetEnvironmentName() |
| 2 | 25 | | { |
| 2 | 26 | | string key = GetEnvironmentKey(); |
| 2 | 27 | | return key == null ? string.Empty : Environment.GetEnvironmentVariable(key); |
| 2 | 28 | | } |
| | 29 | |
|
| | 30 | | protected virtual bool IsEnvironmentKey(string key) |
| 59 | 31 | | { |
| 59 | 32 | | List<string> keys = GetEnvironmentKeys(); |
| 176 | 33 | | return keys.Exists(x => string.Equals(x, key, StringComparison.OrdinalIgnoreCase)); |
| 59 | 34 | | } |
| | 35 | | } |