Skip to content

Commit

Permalink
Setup auto-creating of config folder if missing
Browse files Browse the repository at this point in the history
- Setup check for config folder, if missing, create it so the program does not crash.
- Causes stop if the config folder is not created at all or config settings
  • Loading branch information
thomst08 committed Jan 7, 2024
1 parent 37aaecf commit b3dacd3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Requestrr.WebApi/ClientApp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 32 additions & 4 deletions Requestrr.WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Requestrr.WebApi.RequestrrBot;
using Requestrr.WebApi.RequestrrBot.Locale;
Expand All @@ -28,15 +30,41 @@ public static void Main(string[] args)

private static void UpdateSettingsFile()
{
if (!File.Exists(SettingsFile.FilePath))
try
{
File.WriteAllText(SettingsFile.FilePath, File.ReadAllText("SettingsTemplate.json").Replace("[PRIVATEKEY]", Guid.NewGuid().ToString()));
DirectoryInfo dirInfo = new DirectoryInfo(Directory.GetCurrentDirectory());
string configDirectory = dirInfo.EnumerateDirectories().Where(x => x.Name == "config").Single().FullName;
if (configDirectory == string.Empty || configDirectory == null)
{
throw new Exception("config folder cannot be found");
}
}
else
catch
{
SettingsFileUpgrader.Upgrade(SettingsFile.FilePath);
Console.WriteLine("No config folder found, creating one...");
Directory.CreateDirectory("config");
DirectoryInfo dirInfo = new DirectoryInfo(Directory.GetCurrentDirectory());
string configDirectory = dirInfo.EnumerateDirectories().Where(x => x.Name == "config").Single().FullName;
}

try
{
if (!File.Exists(SettingsFile.FilePath))
{
File.WriteAllText(SettingsFile.FilePath, File.ReadAllText("SettingsTemplate.json").Replace("[PRIVATEKEY]", Guid.NewGuid().ToString()));
}
else
{
SettingsFileUpgrader.Upgrade(SettingsFile.FilePath);
}
}
catch(Exception ex)
{
Console.WriteLine($"Failed to write to config folder: {ex.Message}");
throw new Exception("No config file to load and cannot create one. Bot cannot start.");
}


if (!File.Exists(NotificationsFile.FilePath))
{
File.WriteAllText(NotificationsFile.FilePath, File.ReadAllText("NotificationsTemplate.json"));
Expand Down

0 comments on commit b3dacd3

Please sign in to comment.