| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | using Spectre.Console; |
| | | 3 | | using Spectre.Console.Cli; |
| | | 4 | | using Orchestrator.Infrastructure.Factories; |
| | | 5 | | |
| | | 6 | | namespace Orchestrator.Commands.Utility.Snapshots; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Command that fetches snapshots and encrypts them in one step. |
| | | 10 | | /// </summary> |
| | | 11 | | public class SnapshotsAllCommand : AsyncCommand<SnapshotsAllSettings> |
| | | 12 | | { |
| | | 13 | | private readonly IAnsiConsole _console; |
| | | 14 | | private readonly IKicktippClientFactory _kicktippClientFactory; |
| | | 15 | | private readonly ILogger<SnapshotsAllCommand> _logger; |
| | | 16 | | |
| | 0 | 17 | | public SnapshotsAllCommand( |
| | 0 | 18 | | IAnsiConsole console, |
| | 0 | 19 | | IKicktippClientFactory kicktippClientFactory, |
| | 0 | 20 | | ILogger<SnapshotsAllCommand> logger) |
| | | 21 | | { |
| | 0 | 22 | | _console = console; |
| | 0 | 23 | | _kicktippClientFactory = kicktippClientFactory; |
| | 0 | 24 | | _logger = logger; |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | public override async Task<int> ExecuteAsync(CommandContext context, SnapshotsAllSettings settings) |
| | | 28 | | { |
| | | 29 | | |
| | | 30 | | try |
| | | 31 | | { |
| | | 32 | | // Validate settings |
| | 0 | 33 | | if (string.IsNullOrWhiteSpace(settings.Community)) |
| | | 34 | | { |
| | 0 | 35 | | _console.MarkupLine("[red]Error: Community is required[/]"); |
| | 0 | 36 | | return 1; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | // Check encryption key early (loaded at startup) |
| | 0 | 40 | | var encryptionKey = Environment.GetEnvironmentVariable("KICKTIPP_FIXTURE_KEY"); |
| | 0 | 41 | | if (string.IsNullOrEmpty(encryptionKey)) |
| | | 42 | | { |
| | 0 | 43 | | _console.MarkupLine("[red]Error: KICKTIPP_FIXTURE_KEY environment variable is not set.[/]"); |
| | 0 | 44 | | _console.WriteLine(); |
| | 0 | 45 | | _console.MarkupLine("[yellow]To generate a new key:[/]"); |
| | 0 | 46 | | _console.MarkupLine("[dim] .\\Encrypt-Fixture.ps1 -GenerateKey[/]"); |
| | 0 | 47 | | return 1; |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | _console.MarkupLine("[green]Fetching and encrypting snapshots...[/]"); |
| | 0 | 51 | | _console.MarkupLine($"[blue]Community:[/] [yellow]{settings.Community}[/]"); |
| | 0 | 52 | | _console.MarkupLine($"[blue]Snapshots directory:[/] [yellow]{settings.SnapshotsDirectory}[/]"); |
| | 0 | 53 | | _console.MarkupLine($"[blue]Output directory:[/] [yellow]{settings.OutputDirectory}[/]"); |
| | 0 | 54 | | _console.WriteLine(); |
| | | 55 | | |
| | 0 | 56 | | var snapshotsPath = Path.GetFullPath(settings.SnapshotsDirectory); |
| | 0 | 57 | | var outputPath = Path.GetFullPath(settings.OutputDirectory); |
| | | 58 | | |
| | | 59 | | // Create snapshots directory |
| | 0 | 60 | | Directory.CreateDirectory(snapshotsPath); |
| | | 61 | | |
| | | 62 | | // Create snapshot client using factory (factory handles env var loading) |
| | 0 | 63 | | var snapshotClient = _kicktippClientFactory.CreateSnapshotClient(); |
| | | 64 | | |
| | | 65 | | // Step 1: Fetch snapshots |
| | 0 | 66 | | _console.MarkupLine("[bold]Step 1: Fetching snapshots[/]"); |
| | 0 | 67 | | var fetchedCount = await SnapshotsFetchCommand.FetchSnapshotsAsync( |
| | 0 | 68 | | _console, snapshotClient, settings.Community, snapshotsPath); |
| | | 69 | | |
| | 0 | 70 | | if (fetchedCount == 0) |
| | | 71 | | { |
| | 0 | 72 | | _console.MarkupLine("[yellow]No snapshots fetched, nothing to encrypt[/]"); |
| | 0 | 73 | | return 0; |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | _console.WriteLine(); |
| | | 77 | | |
| | | 78 | | // Step 2: Encrypt snapshots to community-specific subdirectory |
| | 0 | 79 | | _console.MarkupLine("[bold]Step 2: Encrypting snapshots[/]"); |
| | 0 | 80 | | var deleteOriginals = !settings.KeepOriginals; |
| | 0 | 81 | | var communityOutputPath = Path.Combine(outputPath, settings.Community); |
| | 0 | 82 | | var (encryptedCount, deletedCount) = await SnapshotsEncryptCommand.EncryptSnapshotsAsync( |
| | 0 | 83 | | _console, snapshotsPath, communityOutputPath, encryptionKey, deleteOriginals); |
| | | 84 | | |
| | 0 | 85 | | _console.WriteLine(); |
| | 0 | 86 | | _console.MarkupLine($"[green]Done![/] Fetched {fetchedCount}, encrypted {encryptedCount} snapshot(s)"); |
| | 0 | 87 | | _console.MarkupLine($"[dim]Encrypted files saved to: {communityOutputPath}[/]"); |
| | | 88 | | |
| | 0 | 89 | | if (deletedCount > 0) |
| | | 90 | | { |
| | 0 | 91 | | _console.MarkupLine($"[dim]Deleted {deletedCount} original HTML file(s)[/]"); |
| | | 92 | | } |
| | 0 | 93 | | else if (!settings.KeepOriginals && fetchedCount > 0) |
| | | 94 | | { |
| | 0 | 95 | | _console.MarkupLine("[dim]Original HTML files kept (use default behavior to delete)[/]"); |
| | | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | return 0; |
| | | 99 | | } |
| | 0 | 100 | | catch (Exception ex) |
| | | 101 | | { |
| | 0 | 102 | | _logger.LogError(ex, "Error in snapshots all command"); |
| | 0 | 103 | | _console.MarkupLine($"[red]Error:[/] {ex.Message}"); |
| | 0 | 104 | | return 1; |
| | | 105 | | } |
| | 0 | 106 | | } |
| | | 107 | | } |