From 175aa6733babcfa2cb1208784b0fa528513c964e Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Sun, 3 Sep 2023 15:00:45 -0500 Subject: [PATCH] refactor: Reorganize config list command code This is in preparation for changes to list template includes --- src/Recyclarr.Cli/Console/CliSetup.cs | 7 ++- .../Console/Commands/ConfigListCommand.cs | 60 ------------------- .../Commands/ConfigListLocalCommand.cs | 46 ++++++++++++++ .../Commands/ConfigListTemplatesCommand.cs | 46 ++++++++++++++ .../Config/ConfigListLocalProcessor.cs} | 9 +-- .../Processors/Config/ConfigListProcessor.cs | 27 --------- .../Config/ConfigListTemplateProcessor.cs} | 16 +++-- .../ServiceProcessorsAutofacModule.cs | 3 +- .../Config/ConfigAutofacModule.cs | 5 -- .../Config/Listers/ConfigCategory.cs | 7 --- .../Config/Listers/IConfigLister.cs | 6 -- .../Commands/ConfigCommandsIntegrationTest.cs | 4 +- .../Processors/ConfigListProcessorTest.cs | 25 -------- .../Processors}/ConfigTemplateListerTest.cs | 8 +-- .../Config/ConfigAutofacModuleTest.cs | 23 ------- 15 files changed, 121 insertions(+), 171 deletions(-) delete mode 100644 src/Recyclarr.Cli/Console/Commands/ConfigListCommand.cs create mode 100644 src/Recyclarr.Cli/Console/Commands/ConfigListLocalCommand.cs create mode 100644 src/Recyclarr.Cli/Console/Commands/ConfigListTemplatesCommand.cs rename src/{Recyclarr.TrashLib/Config/Listers/ConfigLocalLister.cs => Recyclarr.Cli/Processors/Config/ConfigListLocalProcessor.cs} (92%) delete mode 100644 src/Recyclarr.Cli/Processors/Config/ConfigListProcessor.cs rename src/{Recyclarr.TrashLib/Config/Listers/ConfigTemplateLister.cs => Recyclarr.Cli/Processors/Config/ConfigListTemplateProcessor.cs} (79%) delete mode 100644 src/Recyclarr.TrashLib/Config/Listers/ConfigCategory.cs delete mode 100644 src/Recyclarr.TrashLib/Config/Listers/IConfigLister.cs delete mode 100644 src/tests/Recyclarr.Cli.Tests/Processors/ConfigListProcessorTest.cs rename src/tests/{Recyclarr.TrashLib.Tests/Config/Listers => Recyclarr.Cli.Tests/Processors}/ConfigTemplateListerTest.cs (88%) delete mode 100644 src/tests/Recyclarr.TrashLib.Tests/Config/ConfigAutofacModuleTest.cs diff --git a/src/Recyclarr.Cli/Console/CliSetup.cs b/src/Recyclarr.Cli/Console/CliSetup.cs index 8798a691..a8199827 100644 --- a/src/Recyclarr.Cli/Console/CliSetup.cs +++ b/src/Recyclarr.Cli/Console/CliSetup.cs @@ -26,7 +26,12 @@ public static class CliSetup { config.SetDescription("Operations for configuration files"); config.AddCommand("create"); - config.AddCommand("list"); + config.AddBranch("list", list => + { + list.SetDescription("List configuration files in various ways"); + list.AddCommand("local"); + list.AddCommand("templates"); + }); }); cli.AddBranch("delete", delete => diff --git a/src/Recyclarr.Cli/Console/Commands/ConfigListCommand.cs b/src/Recyclarr.Cli/Console/Commands/ConfigListCommand.cs deleted file mode 100644 index 2b422e3f..00000000 --- a/src/Recyclarr.Cli/Console/Commands/ConfigListCommand.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System.ComponentModel; -using System.Diagnostics.CodeAnalysis; -using JetBrains.Annotations; -using Recyclarr.Cli.Console.Helpers; -using Recyclarr.Cli.Processors.Config; -using Recyclarr.TrashLib.Config.Listers; -using Recyclarr.TrashLib.Config.Parsing.ErrorHandling; -using Recyclarr.TrashLib.ExceptionTypes; -using Recyclarr.TrashLib.Repo; -using Spectre.Console.Cli; - -namespace Recyclarr.Cli.Console.Commands; - -[UsedImplicitly] -[Description("List configuration files in various ways.")] -public class ConfigListCommand : AsyncCommand -{ - private readonly ILogger _log; - private readonly ConfigListProcessor _processor; - private readonly IMultiRepoUpdater _repoUpdater; - - [SuppressMessage("Design", "CA1034:Nested types should not be visible")] - public class CliSettings : BaseCommandSettings - { - [CommandArgument(0, "[ListCategory]")] - [EnumDescription( - "The type of configuration information to list. If not specified, defaults to 'local'.")] - public ConfigCategory ListCategory { get; [UsedImplicitly] init; } = ConfigCategory.Local; - } - - public ConfigListCommand(ILogger log, ConfigListProcessor processor, IMultiRepoUpdater repoUpdater) - { - _log = log; - _processor = processor; - _repoUpdater = repoUpdater; - } - - public override async Task ExecuteAsync(CommandContext context, CliSettings settings) - { - try - { - await _repoUpdater.UpdateAllRepositories(settings.CancellationToken); - _processor.Process(settings.ListCategory); - } - catch (FileExistsException e) - { - _log.Error( - "The file {ConfigFile} already exists. Please choose another path or " + - "delete/move the existing file and run this command again", e.AttemptedPath); - - return 1; - } - catch (NoConfigurationFilesException) - { - _log.Error("No configuration files found"); - } - - return 0; - } -} diff --git a/src/Recyclarr.Cli/Console/Commands/ConfigListLocalCommand.cs b/src/Recyclarr.Cli/Console/Commands/ConfigListLocalCommand.cs new file mode 100644 index 00000000..1887fe62 --- /dev/null +++ b/src/Recyclarr.Cli/Console/Commands/ConfigListLocalCommand.cs @@ -0,0 +1,46 @@ +using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; +using Recyclarr.Cli.Processors.Config; +using Recyclarr.TrashLib.Config.Parsing.ErrorHandling; +using Recyclarr.TrashLib.Repo; +using Spectre.Console.Cli; + +namespace Recyclarr.Cli.Console.Commands; + +[UsedImplicitly] +[Description("List local configuration files.")] +public class ConfigListLocalCommand : AsyncCommand +{ + private readonly ILogger _log; + private readonly ConfigListLocalProcessor _processor; + private readonly IMultiRepoUpdater _repoUpdater; + + [SuppressMessage("Design", "CA1034:Nested types should not be visible")] + public class CliSettings : BaseCommandSettings + { + } + + public ConfigListLocalCommand(ILogger log, ConfigListLocalProcessor processor, IMultiRepoUpdater repoUpdater) + { + _log = log; + _processor = processor; + _repoUpdater = repoUpdater; + } + + public override async Task ExecuteAsync(CommandContext context, CliSettings settings) + { + try + { + await _repoUpdater.UpdateAllRepositories(settings.CancellationToken); + _processor.Process(); + return 0; + } + catch (NoConfigurationFilesException) + { + _log.Error("No configuration files found"); + } + + return 1; + } +} diff --git a/src/Recyclarr.Cli/Console/Commands/ConfigListTemplatesCommand.cs b/src/Recyclarr.Cli/Console/Commands/ConfigListTemplatesCommand.cs new file mode 100644 index 00000000..fcfd3f21 --- /dev/null +++ b/src/Recyclarr.Cli/Console/Commands/ConfigListTemplatesCommand.cs @@ -0,0 +1,46 @@ +using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; +using Recyclarr.Cli.Processors.Config; +using Recyclarr.TrashLib.Config.Parsing.ErrorHandling; +using Recyclarr.TrashLib.Repo; +using Spectre.Console.Cli; + +namespace Recyclarr.Cli.Console.Commands; + +[UsedImplicitly] +[Description("List local configuration files.")] +public class ConfigListTemplatesCommand : AsyncCommand +{ + private readonly ILogger _log; + private readonly ConfigListTemplateProcessor _processor; + private readonly IMultiRepoUpdater _repoUpdater; + + [SuppressMessage("Design", "CA1034:Nested types should not be visible")] + public class CliSettings : BaseCommandSettings + { + } + + public ConfigListTemplatesCommand(ILogger log, ConfigListTemplateProcessor processor, IMultiRepoUpdater repoUpdater) + { + _log = log; + _processor = processor; + _repoUpdater = repoUpdater; + } + + public override async Task ExecuteAsync(CommandContext context, CliSettings settings) + { + try + { + await _repoUpdater.UpdateAllRepositories(settings.CancellationToken); + _processor.Process(); + return 0; + } + catch (NoConfigurationFilesException) + { + _log.Error("No configuration files found"); + } + + return 1; + } +} diff --git a/src/Recyclarr.TrashLib/Config/Listers/ConfigLocalLister.cs b/src/Recyclarr.Cli/Processors/Config/ConfigListLocalProcessor.cs similarity index 92% rename from src/Recyclarr.TrashLib/Config/Listers/ConfigLocalLister.cs rename to src/Recyclarr.Cli/Processors/Config/ConfigListLocalProcessor.cs index 7a456ad1..b9e05b92 100644 --- a/src/Recyclarr.TrashLib/Config/Listers/ConfigLocalLister.cs +++ b/src/Recyclarr.Cli/Processors/Config/ConfigListLocalProcessor.cs @@ -1,20 +1,21 @@ using System.IO.Abstractions; +using Recyclarr.TrashLib.Config; using Recyclarr.TrashLib.Config.Parsing; using Recyclarr.TrashLib.Config.Services; using Recyclarr.TrashLib.Startup; using Spectre.Console; using Spectre.Console.Rendering; -namespace Recyclarr.TrashLib.Config.Listers; +namespace Recyclarr.Cli.Processors.Config; -public class ConfigLocalLister : IConfigLister +public class ConfigListLocalProcessor { private readonly IAnsiConsole _console; private readonly IConfigurationFinder _configFinder; private readonly IConfigurationLoader _configLoader; private readonly IAppPaths _paths; - public ConfigLocalLister( + public ConfigListLocalProcessor( IAnsiConsole console, IConfigurationFinder configFinder, IConfigurationLoader configLoader, @@ -26,7 +27,7 @@ public class ConfigLocalLister : IConfigLister _paths = paths; } - public void List() + public void Process() { var tree = new Tree(_paths.AppDataDirectory.ToString()!); diff --git a/src/Recyclarr.Cli/Processors/Config/ConfigListProcessor.cs b/src/Recyclarr.Cli/Processors/Config/ConfigListProcessor.cs deleted file mode 100644 index 90725a6c..00000000 --- a/src/Recyclarr.Cli/Processors/Config/ConfigListProcessor.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Autofac.Features.Indexed; -using Recyclarr.TrashLib.Config.Listers; - -namespace Recyclarr.Cli.Processors.Config; - -public class ConfigListProcessor -{ - private readonly ILogger _log; - private readonly IIndex _configListers; - - public ConfigListProcessor(ILogger log, IIndex configListers) - { - _log = log; - _configListers = configListers; - } - - public void Process(ConfigCategory listCategory) - { - _log.Debug("Listing configuration for category {Category}", listCategory); - if (!_configListers.TryGetValue(listCategory, out var lister)) - { - throw new ArgumentOutOfRangeException(nameof(listCategory), listCategory, "Unknown list category"); - } - - lister.List(); - } -} diff --git a/src/Recyclarr.TrashLib/Config/Listers/ConfigTemplateLister.cs b/src/Recyclarr.Cli/Processors/Config/ConfigListTemplateProcessor.cs similarity index 79% rename from src/Recyclarr.TrashLib/Config/Listers/ConfigTemplateLister.cs rename to src/Recyclarr.Cli/Processors/Config/ConfigListTemplateProcessor.cs index 963bbaf8..c31a31f1 100644 --- a/src/Recyclarr.TrashLib/Config/Listers/ConfigTemplateLister.cs +++ b/src/Recyclarr.Cli/Processors/Config/ConfigListTemplateProcessor.cs @@ -1,22 +1,26 @@ +using Recyclarr.TrashLib.Config; using Recyclarr.TrashLib.Config.Services; using Spectre.Console; -namespace Recyclarr.TrashLib.Config.Listers; +namespace Recyclarr.Cli.Processors.Config; -public class ConfigTemplateLister : IConfigLister +public class ConfigListTemplateProcessor { private readonly IAnsiConsole _console; private readonly IConfigTemplateGuideService _guideService; - public ConfigTemplateLister( - IAnsiConsole console, - IConfigTemplateGuideService guideService) + public ConfigListTemplateProcessor(IAnsiConsole console, IConfigTemplateGuideService guideService) { _console = console; _guideService = guideService; } - public void List() + public void Process() + { + ListTemplates(); + } + + private void ListTemplates() { var data = _guideService.LoadTemplateData(); diff --git a/src/Recyclarr.Cli/Processors/ServiceProcessorsAutofacModule.cs b/src/Recyclarr.Cli/Processors/ServiceProcessorsAutofacModule.cs index aecc98c8..e88d3bea 100644 --- a/src/Recyclarr.Cli/Processors/ServiceProcessorsAutofacModule.cs +++ b/src/Recyclarr.Cli/Processors/ServiceProcessorsAutofacModule.cs @@ -23,7 +23,8 @@ public class ServiceProcessorsAutofacModule : Module // Configuration builder.RegisterType().As(); builder.RegisterType().As(); - builder.RegisterType(); + builder.RegisterType(); + builder.RegisterType(); // Delete builder.RegisterType().As(); diff --git a/src/Recyclarr.TrashLib/Config/ConfigAutofacModule.cs b/src/Recyclarr.TrashLib/Config/ConfigAutofacModule.cs index cda2044b..fc103b19 100644 --- a/src/Recyclarr.TrashLib/Config/ConfigAutofacModule.cs +++ b/src/Recyclarr.TrashLib/Config/ConfigAutofacModule.cs @@ -1,6 +1,5 @@ using Autofac; using FluentValidation; -using Recyclarr.TrashLib.Config.Listers; using Recyclarr.TrashLib.Config.Parsing; using Recyclarr.TrashLib.Config.Parsing.PostProcessing; using Recyclarr.TrashLib.Config.Secrets; @@ -33,10 +32,6 @@ public class ConfigAutofacModule : Module builder.RegisterType(); builder.RegisterType(); - // Config Listers - builder.RegisterType().Keyed(ConfigCategory.Templates); - builder.RegisterType().Keyed(ConfigCategory.Local); - // Config Post Processors builder.RegisterType().As(); diff --git a/src/Recyclarr.TrashLib/Config/Listers/ConfigCategory.cs b/src/Recyclarr.TrashLib/Config/Listers/ConfigCategory.cs deleted file mode 100644 index 4a67b1c4..00000000 --- a/src/Recyclarr.TrashLib/Config/Listers/ConfigCategory.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Recyclarr.TrashLib.Config.Listers; - -public enum ConfigCategory -{ - Local, - Templates -} diff --git a/src/Recyclarr.TrashLib/Config/Listers/IConfigLister.cs b/src/Recyclarr.TrashLib/Config/Listers/IConfigLister.cs deleted file mode 100644 index 411b193c..00000000 --- a/src/Recyclarr.TrashLib/Config/Listers/IConfigLister.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Recyclarr.TrashLib.Config.Listers; - -public interface IConfigLister -{ - void List(); -} diff --git a/src/tests/Recyclarr.Cli.Tests/Console/Commands/ConfigCommandsIntegrationTest.cs b/src/tests/Recyclarr.Cli.Tests/Console/Commands/ConfigCommandsIntegrationTest.cs index 75376e9b..47209e5f 100644 --- a/src/tests/Recyclarr.Cli.Tests/Console/Commands/ConfigCommandsIntegrationTest.cs +++ b/src/tests/Recyclarr.Cli.Tests/Console/Commands/ConfigCommandsIntegrationTest.cs @@ -11,9 +11,9 @@ public class ConfigCommandsIntegrationTest : CliIntegrationFixture [Test, AutoMockData] public async Task Repo_update_is_called_on_config_list( [Frozen] IMultiRepoUpdater updater, - ConfigListCommand sut) + ConfigListLocalCommand sut) { - await sut.ExecuteAsync(default!, new ConfigListCommand.CliSettings()); + await sut.ExecuteAsync(default!, new ConfigListLocalCommand.CliSettings()); await updater.ReceivedWithAnyArgs().UpdateAllRepositories(default); } diff --git a/src/tests/Recyclarr.Cli.Tests/Processors/ConfigListProcessorTest.cs b/src/tests/Recyclarr.Cli.Tests/Processors/ConfigListProcessorTest.cs deleted file mode 100644 index 3c450d2e..00000000 --- a/src/tests/Recyclarr.Cli.Tests/Processors/ConfigListProcessorTest.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Recyclarr.Cli.Processors.Config; -using Recyclarr.TestLibrary.Autofac; -using Recyclarr.TrashLib.Config.Listers; - -namespace Recyclarr.Cli.Tests.Processors; - -[TestFixture] -[Parallelizable(ParallelScope.All)] -public class ConfigListProcessorTest -{ - [Test] - [InlineAutoMockData(ConfigCategory.Templates)] - public void List_templates_invokes_correct_lister( - ConfigCategory category, - [Frozen(Matching.ImplementedInterfaces)] StubAutofacIndex configListers, - IConfigLister lister, - ConfigListProcessor sut) - { - configListers.Add(category, lister); - - sut.Process(category); - - lister.Received().List(); - } -} diff --git a/src/tests/Recyclarr.TrashLib.Tests/Config/Listers/ConfigTemplateListerTest.cs b/src/tests/Recyclarr.Cli.Tests/Processors/ConfigTemplateListerTest.cs similarity index 88% rename from src/tests/Recyclarr.TrashLib.Tests/Config/Listers/ConfigTemplateListerTest.cs rename to src/tests/Recyclarr.Cli.Tests/Processors/ConfigTemplateListerTest.cs index c32ab72b..00c548a6 100644 --- a/src/tests/Recyclarr.TrashLib.Tests/Config/Listers/ConfigTemplateListerTest.cs +++ b/src/tests/Recyclarr.Cli.Tests/Processors/ConfigTemplateListerTest.cs @@ -1,11 +1,11 @@ using System.IO.Abstractions; +using Recyclarr.Cli.Processors.Config; using Recyclarr.TrashLib.Config; -using Recyclarr.TrashLib.Config.Listers; using Recyclarr.TrashLib.Config.Services; using Recyclarr.TrashLib.TestLibrary; using Spectre.Console.Testing; -namespace Recyclarr.TrashLib.Tests.Config.Listers; +namespace Recyclarr.Cli.Tests.Processors; [TestFixture] [Parallelizable(ParallelScope.All)] @@ -16,7 +16,7 @@ public class ConfigTemplateListerTest : TrashLibIntegrationFixture IFileInfo stubFile, [Frozen(Matching.ImplementedInterfaces)] TestConsole console, [Frozen] IConfigTemplateGuideService guideService, - ConfigTemplateLister sut) + ConfigListTemplateProcessor sut) { guideService.LoadTemplateData().Returns(new[] { @@ -26,7 +26,7 @@ public class ConfigTemplateListerTest : TrashLibIntegrationFixture new TemplatePath {Id = "s2", TemplateFile = stubFile, Service = SupportedServices.Sonarr, Hidden = true} }); - sut.List(); + sut.Process(); console.Output.Should().NotContain("s2"); } diff --git a/src/tests/Recyclarr.TrashLib.Tests/Config/ConfigAutofacModuleTest.cs b/src/tests/Recyclarr.TrashLib.Tests/Config/ConfigAutofacModuleTest.cs deleted file mode 100644 index 3c7de5c0..00000000 --- a/src/tests/Recyclarr.TrashLib.Tests/Config/ConfigAutofacModuleTest.cs +++ /dev/null @@ -1,23 +0,0 @@ -using Autofac.Features.Indexed; -using Recyclarr.TrashLib.Config.Listers; -using Recyclarr.TrashLib.TestLibrary; - -namespace Recyclarr.TrashLib.Tests.Config; - -[TestFixture] -[Parallelizable(ParallelScope.All)] -public class ConfigAutofacModuleTest : TrashLibIntegrationFixture -{ - private static IEnumerable AllConfigListCategories() - { - return Enum.GetValues(); - } - - [TestCaseSource(nameof(AllConfigListCategories))] - public void All_list_category_types_registered(ConfigCategory category) - { - var sut = Resolve>(); - var result = sut.TryGetValue(category, out _); - result.Should().BeTrue(); - } -}