deployment/Deployment/ConfigurationBootstrapper.cs
adam dd7efe9155 "hey, self. ever heard of YAGNI?"
"no, please explain each and every nuance of it in excruciating detail."
"eh. the gist is good enough."
2024-10-30 20:32:33 -04:00

35 lines
1.1 KiB
C#

using System;
using System.Dynamic;
using System.Reflection;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;
[assembly: InternalsVisibleTo("deployment.tests")]
namespace greyn.Deployment
{
public static class ConfigurationBootstrapper
{
private const string confpath = "appsettings.json";
public static T Load<T>() where T : new()
{
if (File.Exists("appsettings.json"))
{
/*
* if the configuration expects new values we write them in.
* if you left other junk for whatever reason, get rekt
*/
var toReturn = JsonConvert.DeserializeObject<T>(File.ReadAllText(confpath)) ?? new T();
File.WriteAllText(confpath, JsonConvert.SerializeObject(toReturn, Formatting.Indented));
return toReturn;
}
else
{
var toReturn = new T();
File.WriteAllText(confpath, JsonConvert.SerializeObject(toReturn));
return toReturn;
}
}
}
}