wabbajack/Wabbajack.CLI/Verbs/DownloadUrl.cs

52 lines
1.7 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.CommandLine.NamingConventionBinder;
2021-09-27 12:42:46 +00:00
using System.Threading;
using System.Threading.Tasks;
2021-09-27 12:42:46 +00:00
using Microsoft.Extensions.Logging;
using Wabbajack.CLI.Builder;
2022-02-11 23:40:38 +00:00
using Wabbajack.Common;
2021-09-27 12:42:46 +00:00
using Wabbajack.Downloaders;
using Wabbajack.DTOs;
using Wabbajack.Paths;
2022-02-11 23:40:38 +00:00
using Wabbajack.Paths.IO;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.CLI.Verbs;
public class DownloadUrl
{
2021-10-23 16:51:17 +00:00
private readonly DownloadDispatcher _dispatcher;
private readonly ILogger<DownloadUrl> _logger;
2021-10-23 16:51:17 +00:00
public DownloadUrl(ILogger<DownloadUrl> logger, DownloadDispatcher dispatcher)
{
_logger = logger;
_dispatcher = dispatcher;
}
2020-02-24 17:05:42 +00:00
2022-10-01 01:35:36 +00:00
public static VerbDefinition Definition = new VerbDefinition("download-url", "Downloads a file to a given output",
new[]
{
new OptionDefinition(typeof(Uri), "u", "url", "Url to parse"),
new OptionDefinition(typeof(AbsolutePath), "o", "output", "Output File"),
new OptionDefinition(typeof(bool), "p", "proxy", "Use the Wabbajack Proxy (default true)")
});
internal async Task<int> Run(Uri url, AbsolutePath output, bool proxy = true)
2021-10-23 16:51:17 +00:00
{
var parsed = _dispatcher.Parse(url);
if (parsed == null)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
_logger.LogCritical("No downloader found for {Url}", url);
2021-10-23 16:51:17 +00:00
return 1;
}
2021-10-23 16:51:17 +00:00
var archive = new Archive() {State = parsed, Name = output.FileName.ToString()};
2022-08-09 23:41:11 +00:00
var hash = await _dispatcher.Download(archive, output, CancellationToken.None, proxy); ;
2022-02-11 23:40:38 +00:00
Console.WriteLine($"Download complete: {output.Size().ToFileSizeString()} {hash} {hash.ToHex()} {(long)hash}");
2021-10-23 16:51:17 +00:00
return 0;
}
2021-09-27 12:42:46 +00:00
}