Skip to content

Commit 2edcb99

Browse files
committed
Cleanup EnvironmentalHelper
1 parent 015b4af commit 2edcb99

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

src/EnvironmentHelper.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,36 @@ public static class EnvironmentHelper
1010
/// <summary>
1111
/// Gets a OxideMod environmental variable
1212
/// </summary>
13-
/// <param name="key">The environmental variable</param>
14-
/// <returns></returns>
13+
/// <param name="key">The environmental variable key</param>
14+
/// <returns>The environmental value</returns>
1515
public static string GetVariable(string key) => Environment.GetEnvironmentVariable(NormalizeKey(key));
1616

1717
/// <summary>
1818
/// Sets a OxideMod environmental variable for the process
1919
/// </summary>
20-
/// <param name="key"></param>
21-
/// <param name="value"></param>
22-
/// <param name="force"></param>
20+
/// <remarks>
21+
/// Setting forced to true will bypass throwOnExisting
22+
/// </remarks>
23+
/// <param name="key">The environmental variable key</param>
24+
/// <param name="value">The environmental variable value</param>
25+
/// <param name="throwOnExisting">The exception if the variable already exists</param>
26+
/// <param name="force">Overwrite exist variable</param>
2327
/// <exception cref="InvalidOperationException"></exception>
24-
public static void SetVariable(string key, string value, bool force = false)
28+
public static void SetVariable(string key, string value, bool throwOnExisting = false, bool force = false)
2529
{
2630
key = NormalizeKey(key);
2731

28-
if (force)
29-
{
30-
Environment.SetEnvironmentVariable(key, value);
31-
return;
32-
}
33-
34-
string existingValue = Environment.GetEnvironmentVariable(key);
32+
string existingValue = !force ? Environment.GetEnvironmentVariable(key) : null;
3533

3634
if (existingValue != null)
3735
{
38-
throw new InvalidOperationException(
39-
$"'{key}' has existing value of '{existingValue}' to override set 'force' to 'true'");
36+
if (throwOnExisting)
37+
{
38+
throw new InvalidOperationException(
39+
$"'{key}' has existing value of '{existingValue}' to override set 'force' to 'true'");
40+
}
41+
42+
return;
4043
}
4144

4245
Environment.SetEnvironmentVariable(key, value);

0 commit comments

Comments
 (0)