deployment/Deployment/ConfigurationBootstrapper.cs

35 lines
1.1 KiB
C#
Raw Normal View History

2024-10-24 16:05:50 -04:00
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
2024-10-24 16:05:50 -04:00
*/
var toReturn = JsonConvert.DeserializeObject<T>(File.ReadAllText(confpath)) ?? new T();
File.WriteAllText(confpath, JsonConvert.SerializeObject(toReturn, Formatting.Indented));
2024-10-24 16:05:50 -04:00
return toReturn;
}
else
{
var toReturn = new T();
File.WriteAllText(confpath, JsonConvert.SerializeObject(toReturn));
return toReturn;
}
}
}
}