Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 17 additions & 42 deletions src/dotnet-core-uninstall/Shared/Commands/UninstallCommandExec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bundle> bundles, VerbosityLevel verbosityLevel)
Expand Down Expand Up @@ -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
{
Expand All @@ -158,7 +139,6 @@ private static ProcessStartInfo GetProcessStartInfo(string command)
if (RuntimeInfo.RunningOnWindows)
{
var args = ParseCommandToArgs(command);

return new ProcessStartInfo
{
FileName = args.First(),
Expand All @@ -167,7 +147,7 @@ private static ProcessStartInfo GetProcessStartInfo(string command)
Verb = "runas"
};
}
else if (RuntimeInfo.RunningOnOSX)
if (RuntimeInfo.RunningOnOSX)
{
return new ProcessStartInfo
{
Expand All @@ -176,10 +156,7 @@ private static ProcessStartInfo GetProcessStartInfo(string command)
UseShellExecute = true
};
}
else
{
throw new OperatingSystemNotSupportedException();
}
throw new OperatingSystemNotSupportedException();
}

private static IEnumerable<string> ParseCommandToArgs(string command)
Expand Down Expand Up @@ -216,20 +193,17 @@ public static bool AskItAndReturnUserAnswer(IDictionary<Bundle, string> 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<Bundle, string> bundles, string userResponse = null)
Expand All @@ -241,17 +215,18 @@ public static bool AskWithWarningsForRequiredBundles(IDictionary<Bundle, string>
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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down