-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvStore.cs
executable file
·47 lines (38 loc) · 1.82 KB
/
EnvStore.cs
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
using System.Collections;
using System.Collections.ObjectModel;
namespace BlobHandler;
public class EnvStore : IReadOnlyDictionary<string, string>
{
private ReadOnlyDictionary<string, string> _environmentVariables;
public IReadOnlyDictionary<string, string> EnvironmentVariables => _environmentVariables;
public EnvStore(IEnumerable<string> requiredVariables)
{
var missingVariables = new List<string>();
var environmentVariables = new Dictionary<string, string>();
foreach (var variable in requiredVariables)
{
var value = Environment.GetEnvironmentVariable(variable);
if (string.IsNullOrEmpty(value))
{
missingVariables.Add(variable);
}
else
{
environmentVariables[variable] = value;
}
}
if (missingVariables.Count > 0)
{
throw new ArgumentException($"The following environment variables are not defined: {string.Join(", ", missingVariables)}");
}
_environmentVariables = new ReadOnlyDictionary<string, string>(environmentVariables);
}
public string this[string key] => _environmentVariables[key];
public IEnumerable<string> Keys => _environmentVariables.Keys;
public IEnumerable<string> Values => _environmentVariables.Values;
public int Count => _environmentVariables.Count;
public bool ContainsKey(string key) => _environmentVariables.ContainsKey(key);
public bool TryGetValue(string key, out string value) => _environmentVariables.TryGetValue(key, out value);
public IEnumerator<KeyValuePair<string, string>> GetEnumerator() => _environmentVariables.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _environmentVariables.GetEnumerator();
}