Merge pull request #357 from Moonlight-Panel/v2_AddEnvironmentConfiguration

Implemented environment variable loading in config service
This commit is contained in:
Marcel Baumgartner 2024-01-16 14:39:14 +01:00 committed by GitHub
commit da53a0eef7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -21,6 +21,9 @@ public class ConfigService
var text = File.ReadAllText(Path);
Data = JsonConvert.DeserializeObject<ConfigV1>(text) ?? new();
ApplyEnvironmentVariables("Moonlight", Data);
Save();
}
@ -57,4 +60,43 @@ public class ConfigService
return JsonConvert.SerializeObject(data, Formatting.Indented);
}
private void ApplyEnvironmentVariables(string prefix, object objectToLookAt)
{
foreach (var property in objectToLookAt.GetType().GetProperties())
{
var envName = $"{prefix}_{property.Name}";
if (property.PropertyType.Assembly == GetType().Assembly)
{
ApplyEnvironmentVariables(envName, property.GetValue(objectToLookAt)!);
}
else
{
if(!Environment.GetEnvironmentVariables().Contains(envName))
continue;
var envValue = Environment.GetEnvironmentVariable(envName)!;
if (property.PropertyType == typeof(string))
{
property.SetValue(objectToLookAt, envValue);
}
else if (property.PropertyType == typeof(int))
{
if(!int.TryParse(envValue, out int envIntValue))
continue;
property.SetValue(objectToLookAt, envIntValue);
}
else if (property.PropertyType == typeof(bool))
{
if(!bool.TryParse(envValue, out bool envBoolValue))
continue;
property.SetValue(objectToLookAt, envBoolValue);
}
}
}
}
}