|
|
@ -2,9 +2,6 @@ using System.Diagnostics.CodeAnalysis;
|
|
|
|
using Autofac;
|
|
|
|
using Autofac;
|
|
|
|
using Recyclarr.Cli.Console;
|
|
|
|
using Recyclarr.Cli.Console;
|
|
|
|
using Recyclarr.Cli.Console.Helpers;
|
|
|
|
using Recyclarr.Cli.Console.Helpers;
|
|
|
|
using Recyclarr.Cli.Console.Setup;
|
|
|
|
|
|
|
|
using Recyclarr.Platform;
|
|
|
|
|
|
|
|
using Serilog.Core;
|
|
|
|
|
|
|
|
using Spectre.Console;
|
|
|
|
using Spectre.Console;
|
|
|
|
using Spectre.Console.Cli;
|
|
|
|
using Spectre.Console.Cli;
|
|
|
|
|
|
|
|
|
|
|
@ -12,21 +9,13 @@ namespace Recyclarr.Cli;
|
|
|
|
|
|
|
|
|
|
|
|
internal static class Program
|
|
|
|
internal static class Program
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private static ILifetimeScope? _scope;
|
|
|
|
|
|
|
|
private static IBaseCommandSetupTask[] _tasks = Array.Empty<IBaseCommandSetupTask>();
|
|
|
|
|
|
|
|
private static ILogger? _log;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[SuppressMessage("Design", "CA1031:Do not catch general exception types")]
|
|
|
|
[SuppressMessage("Design", "CA1031:Do not catch general exception types")]
|
|
|
|
public static int Main(string[] args)
|
|
|
|
public static int Main(string[] args)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var builder = new ContainerBuilder();
|
|
|
|
var builder = new ContainerBuilder();
|
|
|
|
CompositionRoot.Setup(builder);
|
|
|
|
CompositionRoot.Setup(builder);
|
|
|
|
|
|
|
|
|
|
|
|
var logLevelSwitch = new LoggingLevelSwitch();
|
|
|
|
var app = new CommandApp(new AutofacTypeRegistrar(builder));
|
|
|
|
var appDataPathProvider = new AppDataPathProvider();
|
|
|
|
|
|
|
|
CompositionRoot.RegisterExternal(builder, logLevelSwitch, appDataPathProvider);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var app = new CommandApp(new AutofacTypeRegistrar(builder, s => _scope = s));
|
|
|
|
|
|
|
|
app.Configure(config =>
|
|
|
|
app.Configure(config =>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
#if DEBUG
|
|
|
|
#if DEBUG
|
|
|
@ -34,60 +23,29 @@ internal static class Program
|
|
|
|
config.ValidateExamples();
|
|
|
|
config.ValidateExamples();
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
config.Settings.PropagateExceptions = true;
|
|
|
|
// config.Settings.PropagateExceptions = true;
|
|
|
|
config.Settings.StrictParsing = true;
|
|
|
|
config.Settings.StrictParsing = true;
|
|
|
|
|
|
|
|
|
|
|
|
config.SetApplicationName("recyclarr");
|
|
|
|
config.SetApplicationName("recyclarr");
|
|
|
|
config.SetApplicationVersion(
|
|
|
|
config.SetApplicationVersion(
|
|
|
|
$"v{GitVersionInformation.SemVer} ({GitVersionInformation.FullBuildMetaData})");
|
|
|
|
$"v{GitVersionInformation.SemVer} ({GitVersionInformation.FullBuildMetaData})");
|
|
|
|
|
|
|
|
|
|
|
|
var interceptor = new CliInterceptor(logLevelSwitch, appDataPathProvider);
|
|
|
|
config.SetExceptionHandler((ex, resolver) =>
|
|
|
|
interceptor.OnIntercepted.Subscribe(_ => OnAppInitialized());
|
|
|
|
{
|
|
|
|
config.SetInterceptor(interceptor);
|
|
|
|
var log = (ILogger?) resolver?.Resolve(typeof(ILogger));
|
|
|
|
|
|
|
|
if (log is null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
AnsiConsole.WriteException(ex, ExceptionFormats.ShortenEverything);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
log.Error(ex, "Non-recoverable Exception");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
CliSetup.Commands(config);
|
|
|
|
CliSetup.Commands(config);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var result = 1;
|
|
|
|
return app.Run(args);
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
result = app.Run(args);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (_log is null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
AnsiConsole.WriteException(ex, ExceptionFormats.ShortenEverything);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_log.Error(ex, "Non-recoverable Exception");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
OnAppCleanup();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void OnAppInitialized()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (_scope is null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
throw new InvalidProgramException("Composition root is not initialized");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_log = _scope.Resolve<ILogger>();
|
|
|
|
|
|
|
|
_log.Debug("Recyclarr Version: {Version}", GitVersionInformation.InformationalVersion);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_tasks = _scope.Resolve<IOrderedEnumerable<IBaseCommandSetupTask>>().ToArray();
|
|
|
|
|
|
|
|
_tasks.ForEach(x => x.OnStart());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void OnAppCleanup()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_tasks.Reverse().ForEach(x => x.OnFinish());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|