wabbajack/Wabbajack.CLI/Verbs/DownloadUrl.cs
2021-10-23 10:51:17 -06:00

48 lines
1.4 KiB
C#

using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Wabbajack.Downloaders;
using Wabbajack.DTOs;
using Wabbajack.Paths;
namespace Wabbajack.CLI.Verbs;
public class DownloadUrl : IVerb
{
private readonly DownloadDispatcher _dispatcher;
private readonly ILogger<DownloadUrl> _logger;
public DownloadUrl(ILogger<DownloadUrl> logger, DownloadDispatcher dispatcher)
{
_logger = logger;
_dispatcher = dispatcher;
}
public Command MakeCommand()
{
var command = new Command("download-url");
command.Add(new Option<Uri>(new[] {"-u", "-url"}, "Url to parse"));
command.Add(new Option<AbsolutePath>(new[] {"-o", "-output"}, "Output file"));
command.Description = "Downloads a file to a given output";
command.Handler = CommandHandler.Create(Run);
return command;
}
private async Task<int> Run(Uri url, AbsolutePath output)
{
var parsed = _dispatcher.Parse(url);
if (parsed == null)
{
_logger.LogCritical("No downloader found for {Url}", url);
return 1;
}
var archive = new Archive() {State = parsed, Name = output.FileName.ToString()};
await _dispatcher.Download(archive, output, CancellationToken.None);
return 0;
}
}