< Summary - Combined Code Coverage

Information
Class: NLightning.Daemon.Contracts.Helpers.CommandLineHelper
Assembly: NLightning.Daemon.Contracts
File(s): /home/runner/work/NLightning/NLightning/src/NLightning.Daemon.Contracts/Helpers/CommandLineHelper.cs
Tag: 57_24045730253
Line coverage
0%
Covered lines: 0
Uncovered lines: 100
Coverable lines: 100
Total lines: 151
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 70
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsHelpRequested(...)0%620%
IsHelpRequested(...)0%620%
GetCommand(...)0%2040%
GetCommand(...)0%2040%
GetCommandArguments(...)0%4260%
GetCommandArguments(...)0%4260%
GetCookiePath(...)0%1980440%
GetCookiePath(...)0%1980440%
ExtractDirectoryFromCookiePath(...)0%7280%
ExtractDirectoryFromCookiePath(...)0%7280%
IsOption(...)0%4260%

File(s)

/home/runner/work/NLightning/NLightning/src/NLightning.Daemon.Contracts/Helpers/CommandLineHelper.cs

#LineLine coverage
 1namespace NLightning.Daemon.Contracts.Helpers;
 2
 3/// <summary>
 4/// Helper class for displaying command line usage information
 5/// </summary>
 6public static class CommandLineHelper
 7{
 8    public const string DashH = "-h";
 9    public const string DashDashHelp = "--help";
 10    public const string DashN = "-n";
 11    public const string DashDashNetwork = "--network";
 12    public const string DashDashNetworkEquals = "--network=";
 13    public const string DashC = "-c";
 14    public const string DashDashCookie = "--cookie";
 15    public const string DashDashCookieEquals = "--cookie=";
 16
 17    /// <summary>
 18    /// Parse command line arguments to check for help request
 19    /// </summary>
 20    public static bool IsHelpRequested(string[] args)
 021    {
 022        return args.Any(arg =>
 023                            arg.Equals(DashDashHelp, StringComparison.OrdinalIgnoreCase)
 024                         || arg.Equals(DashH, StringComparison.OrdinalIgnoreCase));
 025    }
 26
 27    public static string? GetCommand(string[] args)
 028    {
 029        for (var i = 0; i < args.Length; i++)
 030        {
 031            if (IsOption(args[i]))
 032            {
 033                i++;
 034                continue;
 35            }
 36
 037            return args[i].ToLowerInvariant();
 38        }
 39
 040        return null;
 041    }
 42
 43    public static string[] GetCommandArguments(string command, string[] args)
 044    {
 045        var cmdArgs = new List<string>();
 046        var cmdFound = false;
 47
 048        for (var i = 0; i < args.Length; i++)
 049        {
 050            if (!cmdFound)
 051            {
 052                if (args[i].Equals(command, StringComparison.OrdinalIgnoreCase))
 053                    cmdFound = true;
 54
 055                continue;
 56            }
 57
 058            cmdArgs.Add(args[i]);
 059        }
 60
 061        return cmdArgs.ToArray();
 062    }
 63
 64    public static string GetCookiePath(string[] args)
 065    {
 066        string? network = null;
 067        string? cookiePath = null;
 68
 69        // Check command line args
 070        for (var i = 0; i < args.Length; i++)
 071        {
 72            // Check for network
 073            if (args[i].StartsWith(DashN) || args[i].StartsWith(DashDashNetwork, StringComparison.OrdinalIgnoreCase))
 074            {
 075                if ((args[i].Equals(DashDashNetwork, StringComparison.OrdinalIgnoreCase) || args[i].Equals(DashN))
 076                 && i + 1 < args.Length)
 077                {
 078                    network = args[i + 1];
 079                }
 080                else if (args[i].StartsWith(DashDashNetworkEquals, StringComparison.OrdinalIgnoreCase))
 081                {
 082                    network = args[i][DashDashNetworkEquals.Length..];
 083                }
 84
 085                if (network is not null)
 086                    break;
 087            }
 088            else if (args[i].StartsWith(DashC) || // Check for cookie
 089                     args[i].StartsWith(DashDashCookie, StringComparison.OrdinalIgnoreCase))
 090            {
 091                if ((args[i].Equals(DashDashCookie, StringComparison.OrdinalIgnoreCase) || args[i].Equals(DashC))
 092                 && i + 1 < args.Length)
 093                {
 094                    cookiePath = args[i];
 095                }
 096                else if (args[i].StartsWith(DashDashCookieEquals, StringComparison.OrdinalIgnoreCase))
 097                {
 098                    cookiePath = args[i][DashDashCookieEquals.Length..];
 099                }
 100
 0101                if (cookiePath is not null)
 0102                    break;
 0103            }
 0104        }
 105
 106        // Check the environment if no args provided
 0107        if (cookiePath is null && network is null)
 0108        {
 0109            var envNetwork = Environment.GetEnvironmentVariable("NLTG_NETWORK");
 0110            if (!string.IsNullOrEmpty(envNetwork))
 0111            {
 0112                network = envNetwork;
 0113            }
 114            else
 0115            {
 0116                var envCookie = Environment.GetEnvironmentVariable("NLTG_COOKIE");
 0117                if (!string.IsNullOrEmpty(envCookie))
 0118                {
 0119                    cookiePath = envCookie;
 0120                }
 0121            }
 0122        }
 123
 124        // Go with default paths if no environments provided
 0125        if (cookiePath is not null)
 0126            return ExtractDirectoryFromCookiePath(cookiePath);
 127
 0128        var homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
 0129        cookiePath = Path.Combine(homeDir, ".nltg", network ?? "mainnet");
 0130        return Directory.Exists(cookiePath) ? cookiePath : throw new InvalidOperationException("Cookie not found");
 0131    }
 132
 133    private static string ExtractDirectoryFromCookiePath(string cookiePath)
 0134    {
 0135        cookiePath = Path.GetFullPath(cookiePath);
 0136        if (cookiePath.EndsWith(".cookie", StringComparison.OrdinalIgnoreCase))
 0137        {
 0138            cookiePath = Path.GetDirectoryName(cookiePath) ??
 0139                         throw new InvalidOperationException("Cookie not found");
 0140        }
 0141        else if (cookiePath.EndsWith(Path.DirectorySeparatorChar))
 0142            cookiePath = cookiePath[..^1];
 143
 0144        return Directory.Exists(cookiePath) ? cookiePath : throw new InvalidOperationException("Cookie not found");
 0145    }
 146
 0147    private static bool IsOption(string arg) => arg.StartsWith("-n")
 0148                                             || arg.StartsWith("--network")
 0149                                             || arg.StartsWith("-c")
 0150                                             || arg.StartsWith("--cookie");
 151}