Skip to content

Commit

Permalink
new CLI: download command
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter-Juhasz committed Jan 2, 2025
1 parent b1a27f5 commit 6dd941d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
52 changes: 52 additions & 0 deletions src/PhotoArchiver.Console/Commands/Download.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.CommandLine;
using System.CommandLine.Invocation;

namespace PhotoArchiver.Console.Commands;

using Download;

public static partial class Extensions
{
public static void AddDownloadCommand(this RootCommand root, IHostBuilder hostBuilder)
{
var command = new Command("download", "Downloads media from cloud storage to a folder.");

var dateArgument = new Argument<DateTime>("date", "The date to download media for.");
command.AddArgument(dateArgument);
var pathArgument = new Argument<string>("path", "The path of the destination folder.");
command.AddArgument(pathArgument);

var verifyOption = new Option<bool>("--verify", "Verifies the upload after completion.");
verifyOption.SetDefaultValue(true);
command.AddOption(verifyOption);
var archiveOption = new Option<bool>("--archive", "Archives the file after download.");
archiveOption.SetDefaultValue(false);
command.AddOption(archiveOption);

command.SetHandler(async (InvocationContext context) =>
{
var cancellationToken = context.GetCancellationToken();

// configure
hostBuilder.ConfigureServices((services) =>
{
services.Configure<DownloadOptions>(options =>
{
options.Date = context.ParseResult.GetValueForArgument(dateArgument);
options.Path = context.ParseResult.GetValueForArgument(pathArgument);
options.Verify = context.ParseResult.GetValueForOption(verifyOption);
options.Archive = context.ParseResult.GetValueForOption(archiveOption);
});
});
using var host = hostBuilder.Build();

// run
await host.StartAsync(cancellationToken);
});

root.AddCommand(command);
}
}
3 changes: 2 additions & 1 deletion src/PhotoArchiver.Console/Commands/Upload.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Azure.Storage.Blobs.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using PhotoArchiver.Upload;
using System.CommandLine;
using System.CommandLine.Invocation;

namespace PhotoArchiver.Console.Commands;

using Upload;

public static partial class Extensions
{
public static void AddUploadCommand(this RootCommand root, IHostBuilder hostBuilder)
Expand Down
3 changes: 2 additions & 1 deletion src/PhotoArchiver.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.CommandLine;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

Expand All @@ -19,7 +20,6 @@
using PhotoArchiver.Update;
using PhotoArchiver.Upload;
using PhotoArchiver.Console;
using System.CommandLine;
using PhotoArchiver.Console.Commands;

[assembly: InternalsVisibleTo("PhotoArchiver.Tests")]
Expand Down Expand Up @@ -93,5 +93,6 @@

var rootCommand = new RootCommand();
rootCommand.AddUploadCommand(hostBuilder);
rootCommand.AddDownloadCommand(hostBuilder);

await rootCommand.InvokeAsync(args);

0 comments on commit 6dd941d

Please sign in to comment.