adds first code to upload an image using cli
All checks were successful
piwigodotnet/pipeline/head This commit looks good
All checks were successful
piwigodotnet/pipeline/head This commit looks good
This commit is contained in:
parent
34807e6572
commit
79aedc4855
15
src/Piwigo.Client.Cli/BaseOptions.cs
Normal file
15
src/Piwigo.Client.Cli/BaseOptions.cs
Normal 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!;
|
||||||
|
}
|
11
src/Piwigo.Client.Cli/EventIds.cs
Normal file
11
src/Piwigo.Client.Cli/EventIds.cs
Normal 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");
|
||||||
|
}
|
@ -8,8 +8,14 @@
|
|||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Piwigo.Client\Piwigo.Client.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CommandLineParser" Version="2.9.1" />
|
<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>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -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
|
30
src/Piwigo.Client.Cli/UploadHandler.cs
Normal file
30
src/Piwigo.Client.Cli/UploadHandler.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
src/Piwigo.Client.Cli/UploadOptions.cs
Normal file
12
src/Piwigo.Client.Cli/UploadOptions.cs
Normal 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!;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user