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.
recyclarr/src/Recyclarr.TrashLib/Config/Services/ConfigTemplateGuideService.cs

50 lines
1.7 KiB

using System.Collections.ObjectModel;
using System.IO.Abstractions;
using JetBrains.Annotations;
using Recyclarr.TrashLib.Repo;
namespace Recyclarr.TrashLib.Config.Services;
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
public record TemplateEntry(string Id, string Template, bool Hidden = false);
public record TemplatesData
{
public ReadOnlyCollection<TemplateEntry> Radarr { get; [UsedImplicitly] init; } = new(Array.Empty<TemplateEntry>());
public ReadOnlyCollection<TemplateEntry> Sonarr { get; [UsedImplicitly] init; } = new(Array.Empty<TemplateEntry>());
}
public record TemplatePath(SupportedServices Service, string Id, IFileInfo TemplateFile, bool Hidden);
public class ConfigTemplateGuideService : IConfigTemplateGuideService
{
private readonly IConfigTemplatesRepo _repo;
public ConfigTemplateGuideService(IConfigTemplatesRepo repo)
{
_repo = repo;
}
public IReadOnlyCollection<TemplatePath> LoadTemplateData()
{
var templatesPath = _repo.Path.File("templates.json");
if (!templatesPath.Exists)
{
throw new InvalidDataException(
$"Recyclarr templates.json does not exist: {templatesPath}");
}
var templates = TrashRepoJsonParser.Deserialize<TemplatesData>(templatesPath);
TemplatePath NewTemplatePath(TemplateEntry entry, SupportedServices service)
{
return new TemplatePath(service, entry.Id, _repo.Path.File(entry.Template), entry.Hidden);
}
return templates.Radarr
.Select(x => NewTemplatePath(x, SupportedServices.Radarr))
.Concat(templates.Sonarr.Select(x => NewTemplatePath(x, SupportedServices.Sonarr)))
.ToList();
}
}