diff --git a/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs b/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs new file mode 100644 index 0000000..ad91f5d --- /dev/null +++ b/PiwigoDirectorySync/Commands/PiwigoAddCommand.cs @@ -0,0 +1,42 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using PiwigoDirectorySync.Infrastructure; +using PiwigoDirectorySync.Persistence; +using Spectre.Console.Cli; + +namespace PiwigoDirectorySync.Commands; + +internal class PiwigoAddCommand : CancellableAsyncCommand +{ + private readonly ILogger _logger; + private readonly PersistenceContext _persistenceContext; + + public PiwigoAddCommand(ILogger logger, PersistenceContext persistenceContext) + { + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _persistenceContext = persistenceContext ?? throw new ArgumentNullException(nameof(persistenceContext)); + } + + protected override async Task ExecuteAsync(CommandContext context, PiwigoAddSettings settings, CancellationToken cancellation) + { + _logger.LogInformation("Adding piwigo server {SettingsServerName} with user {SettingsUserName} and url {SettingsUrl}", settings.ServerName, settings.UserName, + settings.Url); + + var server = new ServerEntity + { + Url = settings.Url, + Name = settings.ServerName, + Username = settings.UserName, + Password = settings.Password, + RootDirectory = settings.RootDirectory + }; + + _persistenceContext.PiwigoServers.Add(server); + await _persistenceContext.SaveChangesAsync(cancellation); + + var piwigoServers = await _persistenceContext.PiwigoServers.ToListAsync(cancellation); + PiwigoListCommand.PrintServers(piwigoServers); + + return 0; + } +} \ No newline at end of file diff --git a/PiwigoDirectorySync/Commands/PiwigoAddSettings.cs b/PiwigoDirectorySync/Commands/PiwigoAddSettings.cs new file mode 100644 index 0000000..6d78ed5 --- /dev/null +++ b/PiwigoDirectorySync/Commands/PiwigoAddSettings.cs @@ -0,0 +1,14 @@ +using Spectre.Console.Cli; + +namespace PiwigoDirectorySync.Commands; + +internal class PiwigoAddSettings : CommandSettings +{ + [CommandArgument(0, "")] public string ServerName { get; init; } = string.Empty; + [CommandArgument(1, "")] public string UserName { get; init; } = string.Empty; + [CommandArgument(1, "")] public string Password { get; init; } = string.Empty; + [CommandArgument(1, "")] public string Url { get; init; } = string.Empty; + + [CommandArgument(1, "")] + public string RootDirectory { get; init; } = string.Empty; +} \ No newline at end of file diff --git a/PiwigoDirectorySync/Commands/PiwigoListCommand.cs b/PiwigoDirectorySync/Commands/PiwigoListCommand.cs new file mode 100644 index 0000000..df7030b --- /dev/null +++ b/PiwigoDirectorySync/Commands/PiwigoListCommand.cs @@ -0,0 +1,45 @@ +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 PiwigoListCommand : CancellableAsyncCommand +{ + private readonly ILogger _logger; + private readonly PersistenceContext _persistenceContext; + + public PiwigoListCommand(ILogger logger, PersistenceContext persistenceContext) + { + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _persistenceContext = persistenceContext ?? throw new ArgumentNullException(nameof(persistenceContext)); + } + + protected override async Task ExecuteAsync(CommandContext context, PiwigoListSettings settings, CancellationToken cancellation) + { + var piwigoServers = await _persistenceContext.PiwigoServers.ToListAsync(cancellation); + + PrintServers(piwigoServers); + + return 0; + } + + internal static void PrintServers(List piwigoServers) + { + var table = new Table(); + table.AddColumn(new TableColumn("Server Id").Centered()); + table.AddColumn("Name"); + table.AddColumn("User"); + table.AddColumn("Url"); + + foreach (var piwigoServer in piwigoServers) + { + table.AddRow($"[green]{piwigoServer.Id}[/]", piwigoServer.Name, piwigoServer.Username, piwigoServer.Url); + } + + AnsiConsole.Write(table); + } +} \ No newline at end of file diff --git a/PiwigoDirectorySync/Commands/PiwigoListSettings.cs b/PiwigoDirectorySync/Commands/PiwigoListSettings.cs new file mode 100644 index 0000000..fd4af71 --- /dev/null +++ b/PiwigoDirectorySync/Commands/PiwigoListSettings.cs @@ -0,0 +1,7 @@ +using Spectre.Console.Cli; + +namespace PiwigoDirectorySync.Commands; + +internal class PiwigoListSettings : CommandSettings +{ +} \ No newline at end of file diff --git a/PiwigoDirectorySync/Program.cs b/PiwigoDirectorySync/Program.cs index 3d10eda..b8f8b2a 100644 --- a/PiwigoDirectorySync/Program.cs +++ b/PiwigoDirectorySync/Program.cs @@ -52,6 +52,13 @@ app.Configure(config => c.AddCommand("albums"); c.AddCommand("images"); }); + config.AddBranch("piwigo", c => + { + c.AddCommand("list"); + c.AddCommand("add"); + // c.AddCommand("update"); + // c.AddCommand("remove"); + }); }); return await app.RunAsync(args); \ No newline at end of file diff --git a/PiwigoDirectorySync/Properties/launchSettings.json b/PiwigoDirectorySync/Properties/launchSettings.json index 97cb2ef..3eaa37b 100644 --- a/PiwigoDirectorySync/Properties/launchSettings.json +++ b/PiwigoDirectorySync/Properties/launchSettings.json @@ -36,6 +36,12 @@ "commandLineArgs": "download", "environmentVariables": { } + }, + "ListServers": { + "commandName": "Project", + "commandLineArgs": "piwigo list", + "environmentVariables": { + } } } }