-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverwritePrompt.cs
More file actions
66 lines (61 loc) · 2.4 KB
/
Copy pathOverwritePrompt.cs
File metadata and controls
66 lines (61 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
namespace XISOSharp.Cli;
using Serilog;
using Logging;
/// <summary>
/// Interactive overwrite confirmation for CLI file outputs
/// (<c>XboxKit/Helpers.cs::ConfirmOverwrite</c> parity).
/// <list type="bullet">
/// <item><c>-y</c>/<c>--yes</c>: never prompt, always overwrite.</item>
/// <item><c>-n</c>/<c>--no</c>: never prompt, refuse when the output exists
/// (prints <c>[ERROR] File already exists</c> and the caller skips the operation).</item>
/// <item>Neither: prompt <c>Would you like to overwrite? (Y/N)</c> on stdout when the
/// output file exists; only <c>Y</c>/<c>YES</c> (case-insensitive) proceeds.</item>
/// </list>
/// The prompt I/O is injectable so tests can drive it without a console.
/// </summary>
internal static class OverwritePrompt
{
/// <summary>
/// Returns true when <paramref name="path"/> may be (over)written.
/// Missing files always return true without prompting.
/// </summary>
internal static bool ConfirmOverwrite(string path, bool assumeYes, bool assumeNo,
TextReader? input = null, TextWriter? output = null)
{
try
{
if (assumeYes)
return true;
if (!File.Exists(path))
return true;
output ??= Console.Out;
if (assumeNo)
{
output.WriteLine($"[ERROR] File already exists: {path}");
Log.Warning("Overwrite refused (assume-no): {Path}", path);
return false;
}
input ??= Console.In;
output.WriteLine($"[WARNING] File already exists: {path}");
output.WriteLine("Would you like to overwrite? (Y/N)");
var response = input.ReadLine()?.Trim();
return string.Equals(response, "Y", StringComparison.OrdinalIgnoreCase) ||
string.Equals(response, "YES", StringComparison.OrdinalIgnoreCase);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Error(ex, "Overwrite prompt failed for {Path}", path);
BugReporter.ReportException(ex, $"Overwrite prompt failed for {path}");
output ??= Console.Out;
try
{
output.WriteLine($"[ERROR] Overwrite check failed: {path} ({ex.Message})");
}
catch
{
// ignored
}
return false;
}
}
}