wabbajack/Wabbajack.CLI/Verbs/DownloadUrl.cs

53 lines
1.6 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
2021-09-27 12:42:46 +00:00
using Microsoft.Extensions.Logging;
using Wabbajack.Downloaders;
using Wabbajack.DTOs;
using Wabbajack.Hashing.xxHash64;
using Wabbajack.Paths;
using Wabbajack.Paths.IO;
namespace Wabbajack.CLI.Verbs
{
2021-09-27 12:42:46 +00:00
public class DownloadUrl : IVerb
{
2021-09-27 12:42:46 +00:00
private readonly ILogger<DownloadUrl> _logger;
private readonly DownloadDispatcher _dispatcher;
2021-09-27 12:42:46 +00:00
public DownloadUrl(ILogger<DownloadUrl> logger, DownloadDispatcher dispatcher)
{
2021-09-27 12:42:46 +00:00
_logger = logger;
_dispatcher = dispatcher;
}
2020-02-24 17:05:42 +00:00
2021-09-27 12:42:46 +00:00
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;
}
2021-09-27 12:42:46 +00:00
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);
2021-09-27 12:42:46 +00:00
return 1;
}
2021-09-27 12:42:46 +00:00
var archive = new Archive() { State = parsed, Name = output.FileName.ToString() };
await _dispatcher.Download(archive, output, CancellationToken.None);
return 0;
}
}
2021-09-27 12:42:46 +00:00
}