using System; using System.CommandLine; using System.CommandLine.Invocation; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Wabbajack.Common; using Wabbajack.Downloaders; using Wabbajack.DTOs; using Wabbajack.Paths; using Wabbajack.Paths.IO; namespace Wabbajack.CLI.Verbs; public class DownloadUrl : IVerb { private readonly DownloadDispatcher _dispatcher; private readonly ILogger _logger; public DownloadUrl(ILogger logger, DownloadDispatcher dispatcher) { _logger = logger; _dispatcher = dispatcher; } public Command MakeCommand() { var command = new Command("download-url"); command.Add(new Option(new[] {"-u", "-url"}, "Url to parse")); command.Add(new Option(new[] {"-o", "-output"}, "Output file")); command.Description = "Downloads a file to a given output"; command.Handler = CommandHandler.Create(Run); return command; } private async Task 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()}; var hash = await _dispatcher.Download(archive, output, CancellationToken.None); ; Console.WriteLine($"Download complete: {output.Size().ToFileSizeString()} {hash} {hash.ToHex()} {(long)hash}"); return 0; } }