MireaBackend/Endpoint/Configuration/EnvironmentManager.cs

36 lines
946 B
C#
Raw Normal View History

2024-01-26 19:29:08 +03:00
using System;
using System.IO;
2024-01-28 06:28:14 +03:00
namespace Mirea.Api.Endpoint.Configuration;
2024-01-26 19:29:08 +03:00
internal static class EnvironmentManager
{
2024-05-28 06:51:40 +03:00
public static void LoadEnvironment(string envFile)
2024-01-26 19:29:08 +03:00
{
2024-05-28 06:51:40 +03:00
if (!File.Exists(envFile)) return;
2024-01-26 19:29:08 +03:00
2024-05-28 06:51:40 +03:00
foreach (var line in File.ReadAllLines(envFile))
2024-01-26 19:29:08 +03:00
{
2024-05-28 06:51:40 +03:00
if (string.IsNullOrEmpty(line)) continue;
var commentIndex = line.IndexOf('#', StringComparison.Ordinal);
string arg = line;
if (commentIndex != -1)
arg = arg.Remove(commentIndex, arg.Length - commentIndex);
var parts = arg.Split(
2024-01-26 19:29:08 +03:00
'=',
StringSplitOptions.RemoveEmptyEntries);
2024-05-28 06:51:40 +03:00
if (parts.Length > 2)
parts = [parts[0], string.Join("=", parts[1..])];
2024-01-26 19:29:08 +03:00
if (parts.Length != 2)
continue;
2024-05-28 06:51:40 +03:00
Environment.SetEnvironmentVariable(parts[0].Trim(), parts[1].Trim());
2024-01-26 19:29:08 +03:00
}
}
}