-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoadEnv.cs
111 lines (95 loc) · 3.52 KB
/
LoadEnv.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System.Collections;
namespace BlogWebApiDotNet.Utils
{
/// <summary>
/// Class <c>LoadDotEnv</c> - Utiliy class to load and validate environment variables.
/// </summary>
public class LoadDotEnv
{
/// Public flag to store verification state.
public bool isValidated = false;
/// keys which were verified and are GURANTED to be the environment.
Dictionary<string, string> validatedKeys = [];
/// <summary>
/// Constructor <c>LoadDotEnv</c> creates new instance.
/// </summary>
public LoadDotEnv() { }
/// <summary>
/// Constructor <c>LoadDotEnv</c> loads env from a file.
/// </summary>
/// <param name="m_filePath">filepath of env file.</param>
public LoadDotEnv(string m_filePath)
{
if (!File.Exists(m_filePath))
{
Console.WriteLine(".env file doesn't exist.");
return;
}
// for each line in the file
foreach (string line in File.ReadAllLines(m_filePath))
{
// split it
string[] lineParts = line.Split("=");
if (lineParts.Length != 2)
{
throw new Exception("The .env file provided is in incorrect format.");
}
// set env var.
Environment.SetEnvironmentVariable(lineParts[0], lineParts[1]);
}
}
/// <summary>
/// Method <c>ValidateEnv</c> validates current set environment variables.
/// </summary>
/// <param name="m_KeysToValidate">Array of string keys to verify.</param>
public void ValidateEnv(ref string[] m_KeysToValidate)
{
IDictionary environmentVariables = Environment.GetEnvironmentVariables();
foreach (string key in m_KeysToValidate)
{
var value = environmentVariables[key] as string;
if (value == null)
{
Console.WriteLine($"Failed to validate environment variable {key} is not set.");
continue;
}
validatedKeys.Add(key, value);
}
isValidated = true;
}
/// <summary>
/// Method <c>GetKeyOrThrow</c> returns a key, throws error if it doesnt exist.
/// </summary>
/// <param name="key">key to find</param>
public string GetKeyOrThrow(string key)
{
if (!isValidated)
{
throw new Exception(
"Env vars has not been validated yet. try running EnvLoader.ValidateEnv() before retrieving it."
);
}
if (!validatedKeys.ContainsKey(key))
{
throw new Exception($"Env var {key} is not present in LoadDotEnv.");
}
return validatedKeys[key];
}
/// <summary>
/// Method <c>UnsafeGet</c> returns a key, doesn't throw error if it doesnt exist.
/// </summary>
/// <param name="key">key to find</param>
public string UnsafeGetKey(string key)
{
if (!isValidated)
{
Console.WriteLine("Env vars has not been validated yet.");
}
if (!validatedKeys.ContainsKey(key))
{
Console.WriteLine($"Env var {key} is not present in LoadDotEnv, Returning null");
}
return validatedKeys[key];
}
}
}