diff --git a/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs b/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs index 6f041cdb..612e724d 100644 --- a/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs +++ b/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs @@ -33,33 +33,17 @@ private static extern IntPtr CommandLineToArgvW( public static void Execute(IBundleCollector bundleCollector, ParseResult parseResult) { + if (!IsAdmin()) + { + throw new NotAdminException(); + } var filtered = CommandBundleFilter.GetFilteredWithRequirementStrings(bundleCollector, parseResult); var verbosity = parseResult.CommandResult.GetVerbosityLevel(); - if (parseResult.FindResultFor(CommandLineConfigs.YesOption) != null) + if (parseResult.FindResultFor(CommandLineConfigs.YesOption) != null || (AskItAndReturnUserAnswer(filtered) && AskWithWarningsForRequiredBundles(filtered))) { - if (!IsAdmin()) - { - throw new NotAdminException(); - } - DoIt(filtered.Keys, verbosity); } - else - { - if (!IsAdmin()) - { - throw new NotAdminException(); - } - - if (AskItAndReturnUserAnswer(filtered)) - { - if (AskWithWarningsForRequiredBundles(filtered)) - { - DoIt(filtered.Keys, verbosity); - } - } - } } private static void DoIt(IEnumerable bundles, VerbosityLevel verbosityLevel) @@ -138,14 +122,11 @@ private static bool IsAdmin() var principal = new WindowsPrincipal(identity); return principal.IsInRole(WindowsBuiltInRole.Administrator); } - else if (OperatingSystem.IsMacOS()) + if (OperatingSystem.IsMacOS()) { return getuid() == 0; } - else - { - throw new OperatingSystemNotSupportedException(); - } + throw new OperatingSystemNotSupportedException(); } catch { @@ -158,7 +139,6 @@ private static ProcessStartInfo GetProcessStartInfo(string command) if (RuntimeInfo.RunningOnWindows) { var args = ParseCommandToArgs(command); - return new ProcessStartInfo { FileName = args.First(), @@ -167,7 +147,7 @@ private static ProcessStartInfo GetProcessStartInfo(string command) Verb = "runas" }; } - else if (RuntimeInfo.RunningOnOSX) + if (RuntimeInfo.RunningOnOSX) { return new ProcessStartInfo { @@ -176,10 +156,7 @@ private static ProcessStartInfo GetProcessStartInfo(string command) UseShellExecute = true }; } - else - { - throw new OperatingSystemNotSupportedException(); - } + throw new OperatingSystemNotSupportedException(); } private static IEnumerable ParseCommandToArgs(string command) @@ -216,20 +193,17 @@ public static bool AskItAndReturnUserAnswer(IDictionary bundles, Console.Write(string.Format(RuntimeInfo.RunningOnWindows ? LocalizableStrings.WindowsConfirmationPromptOutputFormat : LocalizableStrings.MacConfirmationPromptOutputFormat, displayNames)); - var response = userResponse == null ? Console.ReadLine().Trim().ToUpper() : userResponse.ToUpper(); + var response = userResponse?.ToUpper() ?? Console.ReadKey().KeyChar.ToString().ToUpper(); - if (response.Equals("Y") || response.Equals("YES")) + if (response.Equals("Y")) { return true; } - else if (response.Equals("N")) + if (response.Equals("N")) { return false; } - else - { - throw new ConfirmationPromptInvalidException(); - } + throw new ConfirmationPromptInvalidException(); } public static bool AskWithWarningsForRequiredBundles(IDictionary bundles, string userResponse = null) @@ -241,17 +215,18 @@ public static bool AskWithWarningsForRequiredBundles(IDictionary Console.Write(string.Format(RuntimeInfo.RunningOnWindows ? LocalizableStrings.WindowsRequiredBundleConfirmationPromptOutputFormat : LocalizableStrings.MacRequiredBundleConfirmationPromptOutputFormat, pair.Key.DisplayName, pair.Value)); Console.ResetColor(); - var response = userResponse == null ? Console.ReadLine().Trim().ToUpper() : userResponse.ToUpper(); + + var response = userResponse?.ToUpper() ?? Console.ReadKey().KeyChar.ToString().ToUpper(); + if (response.Equals("N")) { return false; } - else if (!(response.Equals("Y") || response.Equals("YES"))) + if (!(response.Equals("Y") || response.Equals("YES"))) { throw new ConfirmationPromptInvalidException(); } } - return true; } } diff --git a/test/dotnet-core-uninstall.Tests/Shared/Commands/UninstallCommandExecTests.cs b/test/dotnet-core-uninstall.Tests/Shared/Commands/UninstallCommandExecTests.cs index 80fde254..037d4d7d 100644 --- a/test/dotnet-core-uninstall.Tests/Shared/Commands/UninstallCommandExecTests.cs +++ b/test/dotnet-core-uninstall.Tests/Shared/Commands/UninstallCommandExecTests.cs @@ -18,9 +18,10 @@ internal enum Results [Theory] [InlineData("Y", Results.Success)] - [InlineData("YES", Results.Success)] - [InlineData("yes", Results.Success)] + [InlineData("YES", Results.Error)] + [InlineData("yes", Results.Error)] [InlineData("n", Results.Reject)] + [InlineData("no", Results.Error)] [InlineData("", Results.Error)] [InlineData("foo", Results.Error)] internal void UserInputIsInterpretedCorrectly(string userResponse, Results expectedResult)