| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | using Spectre.Console; |
| | | 3 | | using Spectre.Console.Cli; |
| | | 4 | | |
| | | 5 | | namespace Orchestrator.Commands.Utility.Snapshots; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Command for encrypting HTML snapshots for safe committing. |
| | | 9 | | /// </summary> |
| | | 10 | | public class SnapshotsEncryptCommand : AsyncCommand<SnapshotsEncryptSettings> |
| | | 11 | | { |
| | | 12 | | private readonly IAnsiConsole _console; |
| | | 13 | | private readonly ILogger<SnapshotsEncryptCommand> _logger; |
| | | 14 | | |
| | 0 | 15 | | public SnapshotsEncryptCommand(IAnsiConsole console, ILogger<SnapshotsEncryptCommand> logger) |
| | | 16 | | { |
| | 0 | 17 | | _console = console; |
| | 0 | 18 | | _logger = logger; |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | public override async Task<int> ExecuteAsync(CommandContext context, SnapshotsEncryptSettings settings) |
| | | 22 | | { |
| | | 23 | | |
| | | 24 | | try |
| | | 25 | | { |
| | | 26 | | // Get encryption key from environment (loaded at startup) |
| | 0 | 27 | | var encryptionKey = Environment.GetEnvironmentVariable("KICKTIPP_FIXTURE_KEY"); |
| | 0 | 28 | | if (string.IsNullOrEmpty(encryptionKey)) |
| | | 29 | | { |
| | 0 | 30 | | _console.MarkupLine("[red]Error: KICKTIPP_FIXTURE_KEY environment variable is not set.[/]"); |
| | 0 | 31 | | _console.WriteLine(); |
| | 0 | 32 | | _console.MarkupLine("[yellow]To generate a new key:[/]"); |
| | 0 | 33 | | _console.MarkupLine("[dim] .\\Encrypt-Fixture.ps1 -GenerateKey[/]"); |
| | 0 | 34 | | _console.WriteLine(); |
| | 0 | 35 | | _console.MarkupLine("[yellow]Then store the key in:[/]"); |
| | 0 | 36 | | _console.MarkupLine("[dim] <repo>/../KicktippAi.Secrets/tests/KicktippIntegration.Tests/.env[/]"); |
| | 0 | 37 | | return 1; |
| | | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | var inputPath = Path.GetFullPath(settings.InputDirectory); |
| | | 41 | | // Output to community-specific subdirectory |
| | 0 | 42 | | var outputPath = Path.GetFullPath(Path.Combine(settings.OutputDirectory, settings.Community)); |
| | | 43 | | |
| | 0 | 44 | | if (!Directory.Exists(inputPath)) |
| | | 45 | | { |
| | 0 | 46 | | _console.MarkupLine($"[red]Error: Input directory not found: {inputPath}[/]"); |
| | 0 | 47 | | return 1; |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | _console.MarkupLine("[green]Encrypting snapshots...[/]"); |
| | 0 | 51 | | _console.MarkupLine($"[blue]Community:[/] [yellow]{settings.Community}[/]"); |
| | 0 | 52 | | _console.MarkupLine($"[blue]Input directory:[/] [yellow]{inputPath}[/]"); |
| | 0 | 53 | | _console.MarkupLine($"[blue]Output directory:[/] [yellow]{outputPath}[/]"); |
| | 0 | 54 | | _console.WriteLine(); |
| | | 55 | | |
| | 0 | 56 | | var (encryptedCount, deletedCount) = await EncryptSnapshotsAsync( |
| | 0 | 57 | | _console, inputPath, outputPath, encryptionKey, settings.DeleteOriginals); |
| | | 58 | | |
| | 0 | 59 | | _console.WriteLine(); |
| | 0 | 60 | | _console.MarkupLine($"[green]Done![/] Encrypted {encryptedCount} file(s) to [yellow]{outputPath}[/]"); |
| | | 61 | | |
| | 0 | 62 | | if (deletedCount > 0) |
| | | 63 | | { |
| | 0 | 64 | | _console.MarkupLine($"[dim]Deleted {deletedCount} original HTML file(s)[/]"); |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | return 0; |
| | | 68 | | } |
| | 0 | 69 | | catch (Exception ex) |
| | | 70 | | { |
| | 0 | 71 | | _logger.LogError(ex, "Error encrypting snapshots"); |
| | 0 | 72 | | _console.MarkupLine($"[red]Error:[/] {ex.Message}"); |
| | 0 | 73 | | return 1; |
| | | 74 | | } |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | internal static async Task<(int encryptedCount, int deletedCount)> EncryptSnapshotsAsync( |
| | | 78 | | IAnsiConsole console, |
| | | 79 | | string inputPath, |
| | | 80 | | string outputPath, |
| | | 81 | | string encryptionKey, |
| | | 82 | | bool deleteOriginals) |
| | | 83 | | { |
| | 0 | 84 | | var encryptedCount = 0; |
| | 0 | 85 | | var deletedCount = 0; |
| | | 86 | | |
| | | 87 | | // Create output directory |
| | 0 | 88 | | Directory.CreateDirectory(outputPath); |
| | | 89 | | |
| | | 90 | | // Find all HTML files |
| | 0 | 91 | | var htmlFiles = Directory.GetFiles(inputPath, "*.html"); |
| | | 92 | | |
| | 0 | 93 | | if (htmlFiles.Length == 0) |
| | | 94 | | { |
| | 0 | 95 | | console.MarkupLine("[yellow]No HTML files found to encrypt[/]"); |
| | 0 | 96 | | return (0, 0); |
| | | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | await console.Status() |
| | 0 | 100 | | .StartAsync("Encrypting files...", async ctx => |
| | 0 | 101 | | { |
| | 0 | 102 | | foreach (var htmlFile in htmlFiles) |
| | 0 | 103 | | { |
| | 0 | 104 | | var fileName = Path.GetFileName(htmlFile); |
| | 0 | 105 | | ctx.Status($"Encrypting {fileName}..."); |
| | 0 | 106 | | |
| | 0 | 107 | | // Read and encrypt |
| | 0 | 108 | | var content = await File.ReadAllTextAsync(htmlFile); |
| | 0 | 109 | | var encrypted = SnapshotEncryptor.Encrypt(content, encryptionKey); |
| | 0 | 110 | | |
| | 0 | 111 | | // Write encrypted file |
| | 0 | 112 | | var outputFile = Path.Combine(outputPath, $"{fileName}.enc"); |
| | 0 | 113 | | await File.WriteAllTextAsync(outputFile, encrypted); |
| | 0 | 114 | | encryptedCount++; |
| | 0 | 115 | | |
| | 0 | 116 | | console.MarkupLine($"[green]✓[/] Encrypted {fileName} → {Path.GetFileName(outputFile)}"); |
| | 0 | 117 | | |
| | 0 | 118 | | // Delete original if requested |
| | 0 | 119 | | if (deleteOriginals) |
| | 0 | 120 | | { |
| | 0 | 121 | | File.Delete(htmlFile); |
| | 0 | 122 | | deletedCount++; |
| | 0 | 123 | | } |
| | 0 | 124 | | } |
| | 0 | 125 | | }); |
| | | 126 | | |
| | 0 | 127 | | return (encryptedCount, deletedCount); |
| | 0 | 128 | | } |
| | | 129 | | } |