< Summary

Information
Class: Orchestrator.Commands.Utility.Snapshots.SnapshotsAllCommand
Assembly: Orchestrator
File(s): /home/runner/work/KicktippAi/KicktippAi/src/Orchestrator/Commands/Utility/Snapshots/SnapshotsAllCommand.cs
Line coverage
98%
Covered lines: 51
Uncovered lines: 1
Coverable lines: 52
Total lines: 107
Line coverage: 98%
Branch coverage
75%
Covered branches: 9
Total branches: 12
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ExecuteAsync()75%121297.73%

File(s)

/home/runner/work/KicktippAi/KicktippAi/src/Orchestrator/Commands/Utility/Snapshots/SnapshotsAllCommand.cs

#LineLine coverage
 1using Microsoft.Extensions.Logging;
 2using Spectre.Console;
 3using Spectre.Console.Cli;
 4using Orchestrator.Infrastructure.Factories;
 5
 6namespace Orchestrator.Commands.Utility.Snapshots;
 7
 8/// <summary>
 9/// Command that fetches snapshots and encrypts them in one step.
 10/// </summary>
 11public class SnapshotsAllCommand : AsyncCommand<SnapshotsAllSettings>
 12{
 13    private readonly IAnsiConsole _console;
 14    private readonly IKicktippClientFactory _kicktippClientFactory;
 15    private readonly ILogger<SnapshotsAllCommand> _logger;
 16
 117    public SnapshotsAllCommand(
 118        IAnsiConsole console,
 119        IKicktippClientFactory kicktippClientFactory,
 120        ILogger<SnapshotsAllCommand> logger)
 21    {
 122        _console = console;
 123        _kicktippClientFactory = kicktippClientFactory;
 124        _logger = logger;
 125    }
 26
 27    public override async Task<int> ExecuteAsync(CommandContext context, SnapshotsAllSettings settings)
 28    {
 29
 30        try
 31        {
 32            // Validate settings
 133            if (string.IsNullOrWhiteSpace(settings.Community))
 34            {
 135                _console.MarkupLine("[red]Error: Community is required[/]");
 136                return 1;
 37            }
 38
 39            // Check encryption key early (loaded at startup)
 140            var encryptionKey = Environment.GetEnvironmentVariable("KICKTIPP_FIXTURE_KEY");
 141            if (string.IsNullOrEmpty(encryptionKey))
 42            {
 143                _console.MarkupLine("[red]Error: KICKTIPP_FIXTURE_KEY environment variable is not set.[/]");
 144                _console.WriteLine();
 145                _console.MarkupLine("[yellow]To generate a new key:[/]");
 146                _console.MarkupLine("[dim]  .\\Encrypt-Fixture.ps1 -GenerateKey[/]");
 147                return 1;
 48            }
 49
 150            _console.MarkupLine("[green]Fetching and encrypting snapshots...[/]");
 151            _console.MarkupLine($"[blue]Community:[/] [yellow]{settings.Community}[/]");
 152            _console.MarkupLine($"[blue]Snapshots directory:[/] [yellow]{settings.SnapshotsDirectory}[/]");
 153            _console.MarkupLine($"[blue]Output directory:[/] [yellow]{settings.OutputDirectory}[/]");
 154            _console.WriteLine();
 55
 156            var snapshotsPath = Path.GetFullPath(settings.SnapshotsDirectory);
 157            var outputPath = Path.GetFullPath(settings.OutputDirectory);
 58
 59            // Create snapshots directory
 160            Directory.CreateDirectory(snapshotsPath);
 61
 62            // Create snapshot client using factory (factory handles env var loading)
 163            var snapshotClient = _kicktippClientFactory.CreateSnapshotClient();
 64
 65            // Step 1: Fetch snapshots
 166            _console.MarkupLine("[bold]Step 1: Fetching snapshots[/]");
 167            var fetchedCount = await SnapshotsFetchCommand.FetchSnapshotsAsync(
 168                _console, snapshotClient, settings.Community, snapshotsPath);
 69
 170            if (fetchedCount == 0)
 71            {
 172                _console.MarkupLine("[yellow]No snapshots fetched, nothing to encrypt[/]");
 173                return 0;
 74            }
 75
 176            _console.WriteLine();
 77
 78            // Step 2: Encrypt snapshots to community-specific subdirectory
 179            _console.MarkupLine("[bold]Step 2: Encrypting snapshots[/]");
 180            var deleteOriginals = !settings.KeepOriginals;
 181            var communityOutputPath = Path.Combine(outputPath, settings.Community);
 182            var (encryptedCount, deletedCount) = await SnapshotsEncryptCommand.EncryptSnapshotsAsync(
 183                _console, snapshotsPath, communityOutputPath, encryptionKey, deleteOriginals);
 84
 185            _console.WriteLine();
 186            _console.MarkupLine($"[green]Done![/] Fetched {fetchedCount}, encrypted {encryptedCount} snapshot(s)");
 187            _console.MarkupLine($"[dim]Encrypted files saved to: {communityOutputPath}[/]");
 88
 189            if (deletedCount > 0)
 90            {
 191                _console.MarkupLine($"[dim]Deleted {deletedCount} original HTML file(s)[/]");
 92            }
 193            else if (!settings.KeepOriginals && fetchedCount > 0)
 94            {
 095                _console.MarkupLine("[dim]Original HTML files kept (use default behavior to delete)[/]");
 96            }
 97
 198            return 0;
 99        }
 1100        catch (Exception ex)
 101        {
 1102            _logger.LogError(ex, "Error in snapshots all command");
 1103            _console.MarkupLine($"[red]Error:[/] {ex.Message}");
 1104            return 1;
 105        }
 1106    }
 107}