|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
| 5 | +using System.CommandLine; |
| 6 | +using System.CommandLine.Invocation; |
| 7 | + |
5 | 8 | namespace DevProxy.CommandHandlers; |
6 | 9 |
|
7 | 10 | public static class CertRemoveCommandHandler |
8 | 11 | { |
9 | | - public static void RemoveCert(ILogger logger) |
| 12 | + public static void RemoveCert(ILogger logger, InvocationContext invocationContext, Option<bool> forceOption) |
10 | 13 | { |
11 | 14 | logger.LogTrace("RemoveCert() called"); |
| 15 | + ArgumentNullException.ThrowIfNull(logger); |
| 16 | + ArgumentNullException.ThrowIfNull(invocationContext); |
| 17 | + ArgumentNullException.ThrowIfNull(forceOption); |
12 | 18 |
|
13 | 19 | try |
14 | 20 | { |
| 21 | + var isForced = invocationContext.ParseResult.GetValueForOption(forceOption); |
| 22 | + if (!isForced) |
| 23 | + { |
| 24 | + var isConfirmed = PromptConfirmation("Do you want to remove the root certificate", defaultValue: false); |
| 25 | + if (!isConfirmed) |
| 26 | + { |
| 27 | + return; |
| 28 | + } |
| 29 | + } |
| 30 | + |
15 | 31 | logger.LogInformation("Uninstalling the root certificate..."); |
| 32 | + |
16 | 33 | ProxyEngine.ProxyServer.CertificateManager.RemoveTrustedRootCertificate(machineTrusted: false); |
| 34 | + |
17 | 35 | logger.LogInformation("DONE"); |
18 | 36 | } |
19 | 37 | catch (Exception ex) |
20 | 38 | { |
21 | 39 | logger.LogError(ex, "Error removing certificate"); |
22 | 40 | } |
| 41 | + finally |
| 42 | + { |
| 43 | + logger.LogTrace("RemoveCert() finished"); |
| 44 | + } |
| 45 | + } |
23 | 46 |
|
24 | | - logger.LogTrace("RemoveCert() finished"); |
| 47 | + private static bool PromptConfirmation(string message, bool defaultValue) |
| 48 | + { |
| 49 | + while (true) |
| 50 | + { |
| 51 | + Console.Write(message + $" ({(defaultValue ? "Y/n" : "y/N")}): "); |
| 52 | + var answer = Console.ReadLine(); |
| 53 | + |
| 54 | + if (string.IsNullOrWhiteSpace(answer)) |
| 55 | + { |
| 56 | + return defaultValue; |
| 57 | + } |
| 58 | + else if (answer.StartsWith("y", StringComparison.OrdinalIgnoreCase)) |
| 59 | + { |
| 60 | + return true; |
| 61 | + } |
| 62 | + else if (answer.StartsWith("n", StringComparison.OrdinalIgnoreCase)) |
| 63 | + { |
| 64 | + return false; |
| 65 | + } |
| 66 | + } |
25 | 67 | } |
26 | 68 | } |
0 commit comments