diff --git a/PiwigoDirectorySync/Commands/UpdateDatabaseCommand.cs b/PiwigoDirectorySync/Commands/UpdateDatabaseCommand.cs new file mode 100644 index 0000000..2862dc2 --- /dev/null +++ b/PiwigoDirectorySync/Commands/UpdateDatabaseCommand.cs @@ -0,0 +1,44 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using PiwigoDirectorySync.Infrastructure; +using PiwigoDirectorySync.Persistence; +using Spectre.Console; +using Spectre.Console.Cli; + +namespace PiwigoDirectorySync.Commands; + +internal class UpdateDatabaseCommand : CancellableAsyncCommand +{ + private readonly ILogger _logger; + private readonly PersistenceContext _persistenceContext; + + public UpdateDatabaseCommand(ILogger logger, PersistenceContext persistenceContext) + { + _logger = logger; + _persistenceContext = persistenceContext; + } + + protected override async Task ExecuteAsync(CommandContext context, UpdateDatabaseSettings settings, CancellationToken cancellation) + { + try + { + await AnsiConsole.Status() + .Spinner(Spinner.Known.Star) + .SpinnerStyle(Style.Parse("green bold")) + .StartAsync("[orange3]Updating database[/]", async _ => { await _persistenceContext.Database.MigrateAsync(cancellation); }); + AnsiConsole.MarkupLine("[green]done[/]"); + return 0; + } + catch (Exception ex) + { + _logger.LogCritical(ex, "Could not update database due to an error {ExMessage}", ex.Message); + AnsiConsole.WriteException(ex); + } + + return -1; + } + + internal class UpdateDatabaseSettings : CommandSettings + { + } +} \ No newline at end of file diff --git a/PiwigoDirectorySync/Program.cs b/PiwigoDirectorySync/Program.cs index 86ce052..9f8f021 100644 --- a/PiwigoDirectorySync/Program.cs +++ b/PiwigoDirectorySync/Program.cs @@ -59,6 +59,7 @@ app.Configure(config => c.AddCommand("update"); c.AddCommand("remove"); }); + config.AddCommand("updateDb"); }); return await app.RunAsync(args); \ No newline at end of file diff --git a/PiwigoDirectorySync/Properties/launchSettings.json b/PiwigoDirectorySync/Properties/launchSettings.json index 3eaa37b..d9ff3d5 100644 --- a/PiwigoDirectorySync/Properties/launchSettings.json +++ b/PiwigoDirectorySync/Properties/launchSettings.json @@ -36,12 +36,18 @@ "commandLineArgs": "download", "environmentVariables": { } - }, + }, "ListServers": { "commandName": "Project", "commandLineArgs": "piwigo list", "environmentVariables": { } + }, + "UpdateDb": { + "commandName": "Project", + "commandLineArgs": "updateDb", + "environmentVariables": { + } } } }