PiwigoDirectorySync/PiwigoDirectorySync/Program.cs
Philipp Häfelfinger b910987a24
All checks were successful
PiwigoDirectorySync/pipeline/head This commit looks good
moves download to sync download and make it more stable
2023-09-15 23:20:37 +02:00

75 lines
3.0 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PiwigoDirectorySync;
using PiwigoDirectorySync.Commands;
using PiwigoDirectorySync.Infrastructure;
using PiwigoDirectorySync.Persistence;
using PiwigoDirectorySync.Services;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;
using Spectre.Console.Cli;
const string outputTemplateConsole = "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}: {Message}{NewLine}";
const string outputTemplateFile = "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message}{NewLine}{NewLine}{Exception}{NewLine}";
Log.Logger = new LoggerConfiguration().MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Override("Piwigo.Client", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Console(LogEventLevel.Information, outputTemplateConsole, theme: AnsiConsoleTheme.Code)
.WriteTo.File("logs/PiwigoDirectorySync.txt", LogEventLevel.Debug, outputTemplateFile, rollingInterval: RollingInterval.Day, retainedFileCountLimit: 90)
.CreateLogger();
var registrations = new ServiceCollection();
registrations.AddSingleton(AppSettings.Config);
registrations.AddLogging(l => l.AddConfiguration(AppSettings.Config.GetSection("Logging")).AddSerilog());
registrations.AddTransient<IPiwigoClientFactory, PiwigoClientFactory>();
registrations.AddTransient<IFileIndexer, FileIndexer>();
registrations.AddTransient<IFileSystemScanner, FileSystemScanner>();
registrations.AddTransient<IAlbumSynchronizer, AlbumSynchronizer>();
registrations.AddTransient<IImageSynchronizer, ImageSynchronizer>();
registrations.AddDbContext<PersistenceContext>(options =>
{
_ = AppSettings.Settings.DbProvider switch
{
"Sqlite" => options.UseSqlite(AppSettings.ConnectionString),
"InMemory" => options.UseInMemoryDatabase(AppSettings.ConnectionString),
_ => throw new Exception($"Unsupported dbType: {AppSettings.Settings.DbProvider}")
};
});
var registrar = new DependencyInjectionTypeRegistrar(registrations);
var app = new CommandApp(registrar);
app.Configure(config =>
{
#if DEBUG
config.PropagateExceptions();
config.ValidateExamples();
#endif
config.AddCommand<ScanCommand>("scan");
config.AddBranch("sync", c =>
{
c.AddCommand<SyncFullCommand>("full");
c.AddCommand<SyncAlbumsCommand>("albums");
c.AddCommand<SyncImagesCommand>("images");
c.AddCommand<SyncDownloadCommand>("download");
c.AddCommand<SyncPendingChangesCommand>("pending");
});
config.AddBranch("piwigo", c =>
{
c.AddCommand<PiwigoListCommand>("list");
c.AddCommand<PiwigoAddCommand>("add");
c.AddCommand<PiwigoUpdateCommand>("update");
c.AddCommand<PiwigoRemoveCommand>("remove");
});
config.AddCommand<UpdateDatabaseCommand>("updateDb");
});
return await app.RunAsync(args);