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 _settings; public SettingsProvider(IAppPaths paths, IYamlSerializerFactory serializerFactory) { _paths = paths; _settings = new Lazy(() => 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(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); } }