< Summary - Combined Code Coverage

Information
Class: Program
Assembly: NLightning.Application.NLTG
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Application.NLTG/Program.cs
Tag: 30_15166811759
Line coverage
0%
Covered lines: 0
Uncovered lines: 67
Coverable lines: 67
Total lines: 145
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 24
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
<Main>$()0%420200%
ReportDaemonStatus()0%2040%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Application.NLTG/Program.cs

#LineLine coverage
 1using Microsoft.Extensions.Hosting;
 2using NBitcoin;
 3using NLightning.Application.NLTG.Extensions;
 4using NLightning.Application.NLTG.Helpers;
 5using NLightning.Application.NLTG.Managers;
 6using NLightning.Application.NLTG.Utilities;
 7using Serilog;
 8using Network = NLightning.Domain.ValueObjects.Network;
 9
 10try
 11{
 12    // Bootstrap logger for startup messages
 013    Log.Logger = new LoggerConfiguration()
 014        .WriteTo.Console()
 015        .CreateBootstrapLogger();
 16
 17    // Get network for the PID file path
 018    var network = CommandLineHelper.GetNetwork(args);
 019    var pidFilePath = DaemonUtils.GetPidFilePath(network);
 20
 21    // Check for the stop command
 022    if (CommandLineHelper.IsStopRequested(args))
 23    {
 024        var stopped = DaemonUtils.StopDaemon(pidFilePath, Log.Logger);
 025        return stopped ? 0 : 1;
 26    }
 27
 28    // Check for status command
 029    if (CommandLineHelper.IsStatusRequested(args))
 30    {
 031        ReportDaemonStatus(pidFilePath);
 032        return 0;
 33    }
 34
 35    // Check if help is requested
 036    if (CommandLineHelper.IsHelpRequested(args))
 37    {
 038        CommandLineHelper.ShowUsage();
 039        return 0;
 40    }
 41
 42    // Read the configuration file to check for daemon setting
 043    var initialConfig = NltgConfigurationExtensions.ReadInitialConfiguration(args);
 44
 045    string? password = null;
 46
 47    // Try to get password from args or prompt
 048    if (args.Contains("--password"))
 49    {
 050        var idx = Array.IndexOf(args, "--password");
 051        if (idx >= 0 && idx + 1 < args.Length)
 052            password = args[idx + 1];
 53    }
 054    if (string.IsNullOrWhiteSpace(password))
 55    {
 056        password = ConsoleUtils.ReadPassword("Enter password for key encryption: ");
 57    }
 058    if (string.IsNullOrWhiteSpace(password))
 59    {
 060        Log.Error("Password cannot be empty.");
 061        return 1;
 62    }
 63
 64    SecureKeyManager keyManager;
 065    var keyFilePath = SecureKeyManager.GetKeyFilePath(network);
 066    if (!File.Exists(keyFilePath))
 67    {
 68        // Creates new key
 069        var key = new Key();
 070        keyManager = new SecureKeyManager(key.ToBytes(), new Network(network), keyFilePath);
 071        keyManager.SaveToFile(password);
 072        Console.WriteLine($"New key created and saved to {keyFilePath}");
 73    }
 74    else
 75    {
 76        // Load the existing key
 077        keyManager = SecureKeyManager.FromFilePath(keyFilePath, new Network(network), password);
 078        Console.WriteLine($"Loaded key from {keyFilePath}");
 79    }
 80
 81    // Start as a daemon if requested
 082    if (DaemonUtils.StartDaemonIfRequested(args, initialConfig, pidFilePath, Log.Logger))
 83    {
 84        // The parent process exits immediately after starting the daemon
 085        return 0;
 86    }
 87
 088    Log.Information("Starting NLTG...");
 89
 90    // Create and run host
 091    await Host.CreateDefaultBuilder(args)
 092        .ConfigureNltg(initialConfig)
 093        .ConfigureNltgServices(keyManager)
 094        .RunConsoleAsync();
 95
 096    return 0;
 97}
 98catch (Exception e)
 99{
 0100    Log.Fatal(e, "Application terminated unexpectedly");
 0101    return 1;
 102}
 103finally
 104{
 0105    Log.CloseAndFlush();
 106}
 107
 108static void ReportDaemonStatus(string pidFilePath)
 109{
 110    try
 111    {
 0112        if (!File.Exists(pidFilePath))
 113        {
 0114            Console.WriteLine("NLTG daemon is not running");
 0115            return;
 116        }
 117
 0118        var pidText = File.ReadAllText(pidFilePath).Trim();
 0119        if (!int.TryParse(pidText, out var pid))
 120        {
 0121            Console.WriteLine("Invalid PID in file, daemon may not be running");
 0122            return;
 123        }
 124
 125        try
 126        {
 0127            var process = System.Diagnostics.Process.GetProcessById(pid);
 0128            var runTime = DateTime.Now - process.StartTime;
 129
 0130            Console.WriteLine("NLTG daemon is running");
 0131            Console.WriteLine($"PID: {pid}");
 0132            Console.WriteLine($"Started: {process.StartTime}");
 0133            Console.WriteLine($"Uptime: {runTime.Days}d {runTime.Hours}h {runTime.Minutes}m");
 0134            Console.WriteLine($"Memory: {process.WorkingSet64 / (1024 * 1024)} MB");
 0135        }
 0136        catch (ArgumentException)
 137        {
 0138            Console.WriteLine("NLTG daemon is not running (stale PID file)");
 0139        }
 0140    }
 0141    catch (Exception e)
 142    {
 0143        Console.WriteLine($"Error checking daemon status: {e.Message}");
 0144    }
 0145}

Methods/Properties

<Main>$()
ReportDaemonStatus()