You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.6 KiB
46 lines
1.6 KiB
using Recyclarr.Common.Extensions;
|
|
using Recyclarr.TrashLib.Config.Yaml;
|
|
using Recyclarr.TrashLib.Startup;
|
|
|
|
namespace Recyclarr.TrashLib.Settings;
|
|
|
|
public class SettingsProvider : ISettingsProvider
|
|
{
|
|
public SettingsValues Settings => _settings.Value;
|
|
|
|
private readonly IAppPaths _paths;
|
|
private readonly Lazy<SettingsValues> _settings;
|
|
|
|
public SettingsProvider(IAppPaths paths, IYamlSerializerFactory serializerFactory)
|
|
{
|
|
_paths = paths;
|
|
_settings = new Lazy<SettingsValues>(() => LoadOrCreateSettingsFile(serializerFactory));
|
|
}
|
|
|
|
private SettingsValues LoadOrCreateSettingsFile(IYamlSerializerFactory serializerFactory)
|
|
{
|
|
if (!_paths.SettingsPath.Exists)
|
|
{
|
|
CreateDefaultSettingsFile();
|
|
}
|
|
|
|
using var stream = _paths.SettingsPath.OpenText();
|
|
var deserializer = serializerFactory.CreateDeserializer();
|
|
return deserializer.Deserialize<SettingsValues?>(stream.ReadToEnd()) ?? new SettingsValues();
|
|
}
|
|
|
|
private void CreateDefaultSettingsFile()
|
|
{
|
|
const string fileData =
|
|
"# yaml-language-server: $schema=https://raw.githubusercontent.com/recyclarr/recyclarr/master/schemas/settings-schema.json\n" +
|
|
"\n" +
|
|
"# Edit this file to customize the behavior of Recyclarr beyond its defaults\n" +
|
|
"# For the settings file reference guide, visit the link to the wiki below:\n" +
|
|
"# https://recyclarr.dev/wiki/yaml/settings-reference/\n";
|
|
|
|
_paths.SettingsPath.CreateParentDirectory();
|
|
using var stream = _paths.SettingsPath.CreateText();
|
|
stream.Write(fileData);
|
|
}
|
|
}
|