adds first code to upload an image using cli
piwigodotnet/pipeline/head This commit looks good Details

This commit is contained in:
Philipp Häfelfinger 2022-11-17 23:12:57 +01:00
parent 34807e6572
commit 79aedc4855
6 changed files with 81 additions and 2 deletions

View File

@ -0,0 +1,15 @@
using CommandLine;
namespace Piwigo.Client.Cli;
internal class BaseOptions
{
[Option('s', "server", Required = true, HelpText = "Piwigo server url")]
public string ServerUrl { get; set; } = null!;
[Option('u', "user", Required = true, HelpText = "Username to log into piwigo")]
public string UserName { get; set; } = null!;
[Option('p', "password", Required = true, HelpText = "Password to log into piwigo")]
public string Password { get; set; } = null!;
}

View File

@ -0,0 +1,11 @@
using Microsoft.Extensions.Logging;
namespace Piwigo.Client.Cli;
internal static class EventIds
{
public static EventId ActionSucceeded { get; } = new(0, "action completed successfully");
public static EventId Upload { get; } = new(1, "upload an image");
public static EventId SystemError { get; } = new(-1, "system error");
public static EventId PiwigoError { get; } = new(-2, "piwigo error");
}

View File

@ -8,8 +8,14 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Piwigo.Client\Piwigo.Client.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
</ItemGroup>
</Project>

View File

@ -1,3 +1,8 @@
// See https://aka.ms/new-console-template for more information
using CommandLine;
using Microsoft.Extensions.Logging;
using Piwigo.Client.Cli;
Console.WriteLine("Hello, World!");
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().AddDebug());
return await Parser.Default.ParseArguments<UploadOptions>(args).MapResult(async opts => await UploadHandler.UploadAndReturnExitCode(opts, loggerFactory),
_ => Task.FromResult(-1)); // Invalid arguments

View File

@ -0,0 +1,30 @@
using Microsoft.Extensions.Logging;
namespace Piwigo.Client.Cli;
internal static class UploadHandler
{
public static async Task<int> UploadAndReturnExitCode(UploadOptions options, ILoggerFactory loggerFactory)
{
var logger = loggerFactory.CreateLogger(nameof(UploadHandler));
try
{
logger.LogInformation(EventIds.Upload, "Uploading {ImagePath}", options.ImagePath);
var client = PiwigoClient.CreateClient(options.ServerUrl, options.UserName, options.Password, loggerFactory);
var result = await client.UploadImageAsync(new FileInfo(options.ImagePath));
logger.LogInformation(EventIds.ActionSucceeded, "Uploaded {ImagePath} with new image id {ImageId}", options.ImagePath, result.ImageId);
return EventIds.ActionSucceeded.Id;
}
catch (PiwigoException ex)
{
logger.LogCritical(EventIds.PiwigoError, ex, "{Message}", ex.Message);
return EventIds.PiwigoError.Id;
}
catch (Exception ex)
{
logger.LogCritical(EventIds.SystemError, ex, "{Message}", ex.Message);
return EventIds.SystemError.Id;
}
}
}

View File

@ -0,0 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using CommandLine;
namespace Piwigo.Client.Cli;
[Verb("upload", HelpText = "Add file contents to the index.")]
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global", Justification = "done by command line parser")]
internal class UploadOptions : BaseOptions
{
[Option('i', "image", Required = true, HelpText = "Path to the image to upload")]
public string ImagePath { get; set; } = null!;
}