|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.CommandLine; |
| 4 | +using System.CommandLine.Invocation; |
| 5 | +using System.IO; |
| 6 | +using System.Reflection; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.Git.CredentialManager.Diagnostics; |
| 10 | + |
| 11 | +namespace Microsoft.Git.CredentialManager.Commands |
| 12 | +{ |
| 13 | + public class DiagnoseCommand : Command |
| 14 | + { |
| 15 | + private const string TestOutputIndent = " "; |
| 16 | + |
| 17 | + private readonly ICommandContext _context; |
| 18 | + private readonly ICollection<IDiagnostic> _diagnostics; |
| 19 | + |
| 20 | + public DiagnoseCommand(ICommandContext context) |
| 21 | + : base("diagnose", "Run diagnostics and gather logs to diagnose problems with Git Credential Manager") |
| 22 | + { |
| 23 | + EnsureArgument.NotNull(context, nameof(context)); |
| 24 | + |
| 25 | + _context = context; |
| 26 | + _diagnostics = new List<IDiagnostic> |
| 27 | + { |
| 28 | + // Add standard diagnostics |
| 29 | + new EnvironmentDiagnostic(context.Environment), |
| 30 | + new FileSystemDiagnostic(context.FileSystem), |
| 31 | + new NetworkingDiagnostic(context.HttpClientFactory), |
| 32 | + new GitDiagnostic(context.Git), |
| 33 | + new CredentialStoreDiagnostic(context.CredentialStore), |
| 34 | + new MicrosoftAuthenticationDiagnostic(context) |
| 35 | + }; |
| 36 | + |
| 37 | + AddOption( |
| 38 | + new Option<string>(new []{"--output", "-o"}, "Output directory for diagnostic logs.") |
| 39 | + ); |
| 40 | + |
| 41 | + Handler = CommandHandler.Create<string>(ExecuteAsync); |
| 42 | + } |
| 43 | + |
| 44 | + public void AddDiagnostic(IDiagnostic diagnostic) |
| 45 | + { |
| 46 | + _diagnostics.Add(diagnostic); |
| 47 | + } |
| 48 | + |
| 49 | + private async Task<int> ExecuteAsync(string output) |
| 50 | + { |
| 51 | + // Don't use IStandardStreams for writing output in this command as we |
| 52 | + // cannot trust any component on the ICommandContext is working correctly. |
| 53 | + Console.WriteLine($"Running diagnostics...{Environment.NewLine}"); |
| 54 | + |
| 55 | + if (_diagnostics.Count == 0) |
| 56 | + { |
| 57 | + Console.WriteLine("No diagnostics to run."); |
| 58 | + return 0; |
| 59 | + } |
| 60 | + |
| 61 | + int numFailed = 0; |
| 62 | + int numSkipped = 0; |
| 63 | + |
| 64 | + string currentDir = Directory.GetCurrentDirectory(); |
| 65 | + string outputDir; |
| 66 | + if (string.IsNullOrWhiteSpace(output)) |
| 67 | + { |
| 68 | + outputDir = currentDir; |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + if (!Directory.Exists(output)) |
| 73 | + { |
| 74 | + Directory.CreateDirectory(output); |
| 75 | + } |
| 76 | + |
| 77 | + outputDir = Path.GetFullPath(Path.Combine(currentDir, output)); |
| 78 | + } |
| 79 | + |
| 80 | + string logFilePath = Path.Combine(outputDir, "gcm-diagnose.log"); |
| 81 | + var extraLogs = new List<string>(); |
| 82 | + |
| 83 | + using var fullLog = new StreamWriter(logFilePath, append: false, Encoding.UTF8); |
| 84 | + fullLog.WriteLine("Diagnose log at {0:s}Z", DateTime.UtcNow); |
| 85 | + fullLog.WriteLine(); |
| 86 | + fullLog.WriteLine($"Executable: {_context.ApplicationPath}"); |
| 87 | + fullLog.WriteLine( |
| 88 | + TryGetAssemblyVersion(out string version) |
| 89 | + ? $"Version: {version}" |
| 90 | + : "Version: [!] Failed to get version information [!]" |
| 91 | + ); |
| 92 | + fullLog.WriteLine(); |
| 93 | + |
| 94 | + foreach (IDiagnostic diagnostic in _diagnostics) |
| 95 | + { |
| 96 | + fullLog.WriteLine("------------"); |
| 97 | + fullLog.WriteLine($"Diagnostic: {diagnostic.Name}"); |
| 98 | + |
| 99 | + if (!diagnostic.CanRun()) |
| 100 | + { |
| 101 | + fullLog.WriteLine("Skipped: True"); |
| 102 | + fullLog.WriteLine(); |
| 103 | + |
| 104 | + Console.Write(" "); |
| 105 | + ConsoleEx.WriteColor("[SKIP]", ConsoleColor.Gray); |
| 106 | + Console.WriteLine(" {0}", diagnostic.Name); |
| 107 | + |
| 108 | + numSkipped++; |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + string inProgressMsg = $" >>>> {diagnostic.Name}"; |
| 113 | + Console.Write(inProgressMsg); |
| 114 | + |
| 115 | + fullLog.WriteLine("Skipped: False"); |
| 116 | + DiagnosticResult result = await diagnostic.RunAsync(); |
| 117 | + fullLog.WriteLine("Success: {0}", result.IsSuccess); |
| 118 | + |
| 119 | + if (result.Exception is null) |
| 120 | + { |
| 121 | + fullLog.WriteLine("Exception: None"); |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + fullLog.WriteLine("Exception:"); |
| 126 | + fullLog.WriteLine(result.Exception.ToString()); |
| 127 | + } |
| 128 | + |
| 129 | + fullLog.WriteLine("Log:"); |
| 130 | + fullLog.WriteLine(result.DiagnosticLog); |
| 131 | + |
| 132 | + Console.Write(new string('\b', inProgressMsg.Length - 1)); |
| 133 | + ConsoleEx.WriteColor( |
| 134 | + result.IsSuccess ? "[ OK ]" : "[FAIL]", |
| 135 | + result.IsSuccess ? ConsoleColor.DarkGreen : ConsoleColor.Red |
| 136 | + ); |
| 137 | + Console.WriteLine(" {0}", diagnostic.Name); |
| 138 | + |
| 139 | + if (!result.IsSuccess) |
| 140 | + { |
| 141 | + numFailed++; |
| 142 | + |
| 143 | + if (result.Exception is not null) |
| 144 | + { |
| 145 | + Console.WriteLine(); |
| 146 | + ConsoleEx.WriteLineIndent("[!] Encountered an exception [!]"); |
| 147 | + ConsoleEx.WriteLineIndent(result.Exception.ToString()); |
| 148 | + } |
| 149 | + |
| 150 | + Console.WriteLine(); |
| 151 | + ConsoleEx.WriteLineIndent("[*] Diagnostic test log [*]"); |
| 152 | + ConsoleEx.WriteLineIndent(result.DiagnosticLog); |
| 153 | + |
| 154 | + Console.WriteLine(); |
| 155 | + } |
| 156 | + |
| 157 | + foreach (string filePath in result.AdditionalFiles) |
| 158 | + { |
| 159 | + string fileName = Path.GetFileName(filePath); |
| 160 | + string destPath = Path.Combine(outputDir, fileName); |
| 161 | + try |
| 162 | + { |
| 163 | + File.Copy(filePath, destPath, overwrite: true); |
| 164 | + } |
| 165 | + catch |
| 166 | + { |
| 167 | + ConsoleEx.WriteLineIndent($"Failed to copy additional file '{filePath}'"); |
| 168 | + } |
| 169 | + |
| 170 | + extraLogs.Add(destPath); |
| 171 | + } |
| 172 | + |
| 173 | + fullLog.Flush(); |
| 174 | + } |
| 175 | + |
| 176 | + Console.WriteLine(); |
| 177 | + string summary = $"Diagnostic summary: {_diagnostics.Count - numFailed} passed, {numSkipped} skipped, {numFailed} failed."; |
| 178 | + Console.WriteLine(summary); |
| 179 | + Console.WriteLine("Log files:"); |
| 180 | + Console.WriteLine($" {logFilePath}"); |
| 181 | + foreach (string log in extraLogs) |
| 182 | + { |
| 183 | + Console.WriteLine($" {log}"); |
| 184 | + } |
| 185 | + Console.WriteLine(); |
| 186 | + Console.WriteLine("Caution: Log files may include sensitive information - redact before sharing."); |
| 187 | + Console.WriteLine(); |
| 188 | + |
| 189 | + if (numFailed > 0) |
| 190 | + { |
| 191 | + Console.WriteLine("Diagnostics indicate a possible problem with your installation."); |
| 192 | + Console.WriteLine($"Please open an issue at {Constants.HelpUrls.GcmNewIssue} and include log files."); |
| 193 | + Console.WriteLine(); |
| 194 | + } |
| 195 | + |
| 196 | + fullLog.Close(); |
| 197 | + return numFailed; |
| 198 | + } |
| 199 | + |
| 200 | + private bool TryGetAssemblyVersion(out string version) |
| 201 | + { |
| 202 | + try |
| 203 | + { |
| 204 | + var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly(); |
| 205 | + var assemblyVersionAttribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>(); |
| 206 | + version = assemblyVersionAttribute is null |
| 207 | + ? assembly.GetName().Version.ToString() |
| 208 | + : assemblyVersionAttribute.InformationalVersion; |
| 209 | + return true; |
| 210 | + } |
| 211 | + catch |
| 212 | + { |
| 213 | + version = null; |
| 214 | + return false; |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + private static class ConsoleEx |
| 219 | + { |
| 220 | + public static void WriteLineIndent(string str) |
| 221 | + { |
| 222 | + string[] lines = str?.Split('\n', '\r'); |
| 223 | + |
| 224 | + if (lines is null) return; |
| 225 | + |
| 226 | + foreach (string line in lines) |
| 227 | + { |
| 228 | + Console.Write(TestOutputIndent); |
| 229 | + Console.WriteLine(line); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + public static void WriteColor(string str, ConsoleColor fgColor) |
| 234 | + { |
| 235 | + var initFgColor = Console.ForegroundColor; |
| 236 | + Console.ForegroundColor = fgColor; |
| 237 | + Console.Write(str); |
| 238 | + Console.ForegroundColor = initFgColor; |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments