< Summary - Combined Code Coverage

Information
Class: Program
Assembly: NLightning.Node
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Node/Program.cs
Tag: 36_15743069263
Line coverage
0%
Covered lines: 0
Uncovered lines: 69
Coverable lines: 69
Total lines: 153
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.Node/Program.cs

#LineLine coverage
 1using Microsoft.Extensions.Hosting;
 2using NBitcoin;
 3using NLightning.Domain.Protocol.ValueObjects;
 4using NLightning.Infrastructure.Bitcoin.Managers;
 5using NLightning.Node.Extensions;
 6using NLightning.Node.Helpers;
 7using NLightning.Node.Utilities;
 8using Serilog;
 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 = NodeConfigurationExtensions.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    }
 54
 055    if (string.IsNullOrWhiteSpace(password))
 56    {
 057        password = ConsoleUtils.ReadPassword("Enter password for key encryption: ");
 58    }
 59
 060    if (string.IsNullOrWhiteSpace(password))
 61    {
 062        Log.Error("Password cannot be empty.");
 063        return 1;
 64    }
 65
 66    SecureKeyManager keyManager;
 067    var keyFilePath = SecureKeyManager.GetKeyFilePath(network);
 068    if (!File.Exists(keyFilePath))
 69    {
 70        // Creates new key
 071        var key = new Key();
 072        keyManager = new SecureKeyManager(key.ToBytes(), new BitcoinNetwork(network), keyFilePath);
 073        keyManager.SaveToFile(password);
 074        Console.WriteLine($"New key created and saved to {keyFilePath}");
 75    }
 76    else
 77    {
 78        // Load the existing key
 079        keyManager = SecureKeyManager.FromFilePath(keyFilePath, new BitcoinNetwork(network), password);
 080        Console.WriteLine($"Loaded key from {keyFilePath}");
 81    }
 82
 83    // Start as a daemon if requested
 084    if (DaemonUtils.StartDaemonIfRequested(args, initialConfig, pidFilePath, Log.Logger))
 85    {
 86        // The parent process exits immediately after starting the daemon
 087        return 0;
 88    }
 89
 090    Log.Information("Starting NLTG...");
 91
 92    // Create and run host
 093    var host = Host.CreateDefaultBuilder(args)
 094                   .ConfigureNltg(initialConfig)
 095                   .ConfigureNltgServices(keyManager)
 096                   .Build();
 97
 98    // Run migrations if configured
 099    await host.MigrateDatabaseIfConfiguredAsync();
 100
 101    // Run the host
 0102    await host.RunAsync();
 103
 0104    return 0;
 105}
 106catch (Exception e)
 107{
 0108    Log.Fatal(e, "Application terminated unexpectedly");
 0109    return 1;
 110}
 111finally
 112{
 0113    Log.CloseAndFlush();
 114}
 115
 116static void ReportDaemonStatus(string pidFilePath)
 117{
 118    try
 119    {
 0120        if (!File.Exists(pidFilePath))
 121        {
 0122            Console.WriteLine("NLTG daemon is not running");
 0123            return;
 124        }
 125
 0126        var pidText = File.ReadAllText(pidFilePath).Trim();
 0127        if (!int.TryParse(pidText, out var pid))
 128        {
 0129            Console.WriteLine("Invalid PID in file, daemon may not be running");
 0130            return;
 131        }
 132
 133        try
 134        {
 0135            var process = System.Diagnostics.Process.GetProcessById(pid);
 0136            var runTime = DateTime.Now - process.StartTime;
 137
 0138            Console.WriteLine("NLTG daemon is running");
 0139            Console.WriteLine($"PID: {pid}");
 0140            Console.WriteLine($"Started: {process.StartTime}");
 0141            Console.WriteLine($"Uptime: {runTime.Days}d {runTime.Hours}h {runTime.Minutes}m");
 0142            Console.WriteLine($"Memory: {process.WorkingSet64 / (1024 * 1024)} MB");
 0143        }
 0144        catch (ArgumentException)
 145        {
 0146            Console.WriteLine("NLTG daemon is not running (stale PID file)");
 0147        }
 0148    }
 0149    catch (Exception e)
 150    {
 0151        Console.WriteLine($"Error checking daemon status: {e.Message}");
 0152    }
 0153}

Methods/Properties

<Main>$()
ReportDaemonStatus()