PiwigoDirectorySync/PiwigoDirectorySync/Program.cs

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}: {Message} {Exception}{NewLine}";
Log.Logger = new LoggerConfiguration().MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Override("Piwigo.Client", LogEventLevel.Debug)
.Enrich.FromLogContext()
.WriteTo.Console(LogEventLevel.Information, outputTemplateConsole, theme: AnsiConsoleTheme.Code)
.WriteTo.File("logs/PiwigoDirectorySync.txt", LogEventLevel.Debug, outputTemplateFile, rollingInterval: RollingInterval.Day, retainedFileCountLimit: 60)
.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);