From 1e83323a08f686eee23a54c93a36859cb9a2dbc2 Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Wed, 26 Feb 2025 21:37:42 -0800 Subject: [PATCH 1/4] Refactor --- .../Shared/Commands/UninstallCommandExec.cs | 26 ++++--------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs b/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs index 6f041cdb..04940089 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) From 3b84129323a65a402f79202a3e1defcd1c850817 Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Thu, 27 Feb 2025 09:04:17 -0800 Subject: [PATCH 2/4] Add sudo --- .../Shared/Commands/UninstallCommandExec.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs b/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs index 04940089..3e04b605 100644 --- a/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs +++ b/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs @@ -155,8 +155,8 @@ private static ProcessStartInfo GetProcessStartInfo(string command) { return new ProcessStartInfo { - FileName = "rm", - Arguments = $"-rf {command}", + FileName = "sudo", + Arguments = $"rm -rf {command}", UseShellExecute = true }; } From 183b5f461f278004a919b825d32417e12d04ff52 Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Thu, 27 Feb 2025 09:23:53 -0800 Subject: [PATCH 3/4] Refactoring --- .../Shared/Commands/UninstallCommandExec.cs | 35 +++++++------------ 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs b/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs index 3e04b605..2c7077a8 100644 --- a/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs +++ b/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs @@ -122,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 { @@ -142,7 +139,6 @@ private static ProcessStartInfo GetProcessStartInfo(string command) if (RuntimeInfo.RunningOnWindows) { var args = ParseCommandToArgs(command); - return new ProcessStartInfo { FileName = args.First(), @@ -151,19 +147,16 @@ private static ProcessStartInfo GetProcessStartInfo(string command) Verb = "runas" }; } - else if (RuntimeInfo.RunningOnOSX) + if (RuntimeInfo.RunningOnOSX) { return new ProcessStartInfo { - FileName = "sudo", - Arguments = $"rm -rf {command}", + FileName = "rm", + Arguments = $"-rf {command}", UseShellExecute = true }; } - else - { - throw new OperatingSystemNotSupportedException(); - } + throw new OperatingSystemNotSupportedException(); } private static IEnumerable ParseCommandToArgs(string command) @@ -200,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.ReadLine().Trim().ToUpper(); if (response.Equals("Y") || response.Equals("YES")) { 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) @@ -225,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.ReadLine().Trim().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; } } From 0403783a0ac4db3276bca3ad3104835871d94fe0 Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Fri, 28 Feb 2025 10:21:11 -0800 Subject: [PATCH 4/4] Simplify user interactions --- .../Shared/Commands/UninstallCommandExec.cs | 6 +++--- .../Shared/Commands/UninstallCommandExecTests.cs | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs b/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs index 2c7077a8..612e724d 100644 --- a/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs +++ b/src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs @@ -193,9 +193,9 @@ public static bool AskItAndReturnUserAnswer(IDictionary bundles, Console.Write(string.Format(RuntimeInfo.RunningOnWindows ? LocalizableStrings.WindowsConfirmationPromptOutputFormat : LocalizableStrings.MacConfirmationPromptOutputFormat, displayNames)); - var response = userResponse?.ToUpper() ?? Console.ReadLine().Trim().ToUpper(); + var response = userResponse?.ToUpper() ?? Console.ReadKey().KeyChar.ToString().ToUpper(); - if (response.Equals("Y") || response.Equals("YES")) + if (response.Equals("Y")) { return true; } @@ -216,7 +216,7 @@ public static bool AskWithWarningsForRequiredBundles(IDictionary LocalizableStrings.MacRequiredBundleConfirmationPromptOutputFormat, pair.Key.DisplayName, pair.Value)); Console.ResetColor(); - var response = userResponse?.ToUpper() ?? Console.ReadLine().Trim().ToUpper(); + var response = userResponse?.ToUpper() ?? Console.ReadKey().KeyChar.ToString().ToUpper(); if (response.Equals("N")) { 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)