| | | 1 | | namespace NLightning.Daemon.Contracts.Helpers; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Helper class for displaying command line usage information |
| | | 5 | | /// </summary> |
| | | 6 | | public 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) |
| | 0 | 21 | | { |
| | 0 | 22 | | return args.Any(arg => |
| | 0 | 23 | | arg.Equals(DashDashHelp, StringComparison.OrdinalIgnoreCase) |
| | 0 | 24 | | || arg.Equals(DashH, StringComparison.OrdinalIgnoreCase)); |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | public static string? GetCommand(string[] args) |
| | 0 | 28 | | { |
| | 0 | 29 | | for (var i = 0; i < args.Length; i++) |
| | 0 | 30 | | { |
| | 0 | 31 | | if (IsOption(args[i])) |
| | 0 | 32 | | { |
| | 0 | 33 | | i++; |
| | 0 | 34 | | continue; |
| | | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | return args[i].ToLowerInvariant(); |
| | | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | return null; |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | public static string[] GetCommandArguments(string command, string[] args) |
| | 0 | 44 | | { |
| | 0 | 45 | | var cmdArgs = new List<string>(); |
| | 0 | 46 | | var cmdFound = false; |
| | | 47 | | |
| | 0 | 48 | | for (var i = 0; i < args.Length; i++) |
| | 0 | 49 | | { |
| | 0 | 50 | | if (!cmdFound) |
| | 0 | 51 | | { |
| | 0 | 52 | | if (args[i].Equals(command, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 53 | | cmdFound = true; |
| | | 54 | | |
| | 0 | 55 | | continue; |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | cmdArgs.Add(args[i]); |
| | 0 | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | return cmdArgs.ToArray(); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | public static string GetCookiePath(string[] args) |
| | 0 | 65 | | { |
| | 0 | 66 | | string? network = null; |
| | 0 | 67 | | string? cookiePath = null; |
| | | 68 | | |
| | | 69 | | // Check command line args |
| | 0 | 70 | | for (var i = 0; i < args.Length; i++) |
| | 0 | 71 | | { |
| | | 72 | | // Check for network |
| | 0 | 73 | | if (args[i].StartsWith(DashN) || args[i].StartsWith(DashDashNetwork, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 74 | | { |
| | 0 | 75 | | if ((args[i].Equals(DashDashNetwork, StringComparison.OrdinalIgnoreCase) || args[i].Equals(DashN)) |
| | 0 | 76 | | && i + 1 < args.Length) |
| | 0 | 77 | | { |
| | 0 | 78 | | network = args[i + 1]; |
| | 0 | 79 | | } |
| | 0 | 80 | | else if (args[i].StartsWith(DashDashNetworkEquals, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 81 | | { |
| | 0 | 82 | | network = args[i][DashDashNetworkEquals.Length..]; |
| | 0 | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | if (network is not null) |
| | 0 | 86 | | break; |
| | 0 | 87 | | } |
| | 0 | 88 | | else if (args[i].StartsWith(DashC) || // Check for cookie |
| | 0 | 89 | | args[i].StartsWith(DashDashCookie, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 90 | | { |
| | 0 | 91 | | if ((args[i].Equals(DashDashCookie, StringComparison.OrdinalIgnoreCase) || args[i].Equals(DashC)) |
| | 0 | 92 | | && i + 1 < args.Length) |
| | 0 | 93 | | { |
| | 0 | 94 | | cookiePath = args[i]; |
| | 0 | 95 | | } |
| | 0 | 96 | | else if (args[i].StartsWith(DashDashCookieEquals, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 97 | | { |
| | 0 | 98 | | cookiePath = args[i][DashDashCookieEquals.Length..]; |
| | 0 | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | if (cookiePath is not null) |
| | 0 | 102 | | break; |
| | 0 | 103 | | } |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | // Check the environment if no args provided |
| | 0 | 107 | | if (cookiePath is null && network is null) |
| | 0 | 108 | | { |
| | 0 | 109 | | var envNetwork = Environment.GetEnvironmentVariable("NLTG_NETWORK"); |
| | 0 | 110 | | if (!string.IsNullOrEmpty(envNetwork)) |
| | 0 | 111 | | { |
| | 0 | 112 | | network = envNetwork; |
| | 0 | 113 | | } |
| | | 114 | | else |
| | 0 | 115 | | { |
| | 0 | 116 | | var envCookie = Environment.GetEnvironmentVariable("NLTG_COOKIE"); |
| | 0 | 117 | | if (!string.IsNullOrEmpty(envCookie)) |
| | 0 | 118 | | { |
| | 0 | 119 | | cookiePath = envCookie; |
| | 0 | 120 | | } |
| | 0 | 121 | | } |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | // Go with default paths if no environments provided |
| | 0 | 125 | | if (cookiePath is not null) |
| | 0 | 126 | | return ExtractDirectoryFromCookiePath(cookiePath); |
| | | 127 | | |
| | 0 | 128 | | var homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); |
| | 0 | 129 | | cookiePath = Path.Combine(homeDir, ".nltg", network ?? "mainnet"); |
| | 0 | 130 | | return Directory.Exists(cookiePath) ? cookiePath : throw new InvalidOperationException("Cookie not found"); |
| | 0 | 131 | | } |
| | | 132 | | |
| | | 133 | | private static string ExtractDirectoryFromCookiePath(string cookiePath) |
| | 0 | 134 | | { |
| | 0 | 135 | | cookiePath = Path.GetFullPath(cookiePath); |
| | 0 | 136 | | if (cookiePath.EndsWith(".cookie", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 137 | | { |
| | 0 | 138 | | cookiePath = Path.GetDirectoryName(cookiePath) ?? |
| | 0 | 139 | | throw new InvalidOperationException("Cookie not found"); |
| | 0 | 140 | | } |
| | 0 | 141 | | else if (cookiePath.EndsWith(Path.DirectorySeparatorChar)) |
| | 0 | 142 | | cookiePath = cookiePath[..^1]; |
| | | 143 | | |
| | 0 | 144 | | return Directory.Exists(cookiePath) ? cookiePath : throw new InvalidOperationException("Cookie not found"); |
| | 0 | 145 | | } |
| | | 146 | | |
| | 0 | 147 | | private static bool IsOption(string arg) => arg.StartsWith("-n") |
| | 0 | 148 | | || arg.StartsWith("--network") |
| | 0 | 149 | | || arg.StartsWith("-c") |
| | 0 | 150 | | || arg.StartsWith("--cookie"); |
| | | 151 | | } |