Skip to content

Commit ba56b9a

Browse files
committed
Add file-only flag for scans
1 parent 3b287c6 commit ba56b9a

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

ProtectionScan/Features/MainFeature.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ internal sealed class MainFeature : Feature
2626
private const string _debugName = "debug";
2727
internal readonly FlagInput DebugInput = new(_debugName, ["-d", "--debug"], "Enable debug mode");
2828

29+
private const string _fileOnlyName = "file-only";
30+
internal readonly FlagInput FileOnlyInput = new(_fileOnlyName, ["-f", "--file"], "Print to file only");
31+
2932
private const string _noArchivesName = "no-archives";
3033
internal readonly FlagInput NoArchivesInput = new(_noArchivesName, ["-na", "--no-archives"], "Disable scanning archives");
3134

@@ -40,12 +43,18 @@ internal sealed class MainFeature : Feature
4043

4144
#endregion
4245

46+
/// <summary>
47+
/// Output information to file only, skip printing to console
48+
/// </summary>
49+
public bool FileOnly { get; private set; }
50+
4351
public MainFeature()
4452
: base(DisplayName, _flags, _description)
4553
{
4654
RequiresInputs = true;
4755

4856
Add(DebugInput);
57+
Add(FileOnlyInput);
4958
Add(NoContentsInput);
5059
Add(NoArchivesInput);
5160
Add(NoPathsInput);
@@ -59,6 +68,9 @@ public override bool Execute()
5968
var fileProgress = new Progress<ProtectionProgress>();
6069
fileProgress.ProgressChanged += Changed;
6170

71+
// Get the options from the arguments
72+
FileOnly = GetBoolean(_fileOnlyName);
73+
6274
// Create scanner for all paths
6375
var scanner = new Scanner(
6476
!GetBoolean(_noArchivesName),
@@ -100,7 +112,7 @@ private static void Changed(object? source, ProtectionProgress value)
100112
/// </summary>
101113
/// <param name="scanner">Scanner object to use</param>
102114
/// <param name="path">File or directory path</param>
103-
private static void GetAndWriteProtections(Scanner scanner, string path)
115+
private void GetAndWriteProtections(Scanner scanner, string path)
104116
{
105117
// Normalize by getting the full path
106118
path = Path.GetFullPath(path);
@@ -137,7 +149,7 @@ private static void GetAndWriteProtections(Scanner scanner, string path)
137149
/// </summary>
138150
/// <param name="path">File or directory path</param>
139151
/// <param name="protections">Dictionary of protections found, if any</param>
140-
private static void WriteProtectionResultFile(string path, Dictionary<string, List<string>> protections)
152+
private void WriteProtectionResultFile(string path, Dictionary<string, List<string>> protections)
141153
{
142154
if (protections == null)
143155
{
@@ -154,6 +166,7 @@ private static void WriteProtectionResultFile(string path, Dictionary<string, Li
154166
catch
155167
{
156168
Console.WriteLine("Could not open protection log file for writing. Only a console log will be provided.");
169+
FileOnly = false;
157170
}
158171

159172
// Sort the keys for consistent output
@@ -174,8 +187,13 @@ private static void WriteProtectionResultFile(string path, Dictionary<string, Li
174187

175188
// Format and output the line
176189
string line = $"{key}: {string.Join(", ", fileProtections)}";
177-
Console.WriteLine(line);
190+
191+
// Only print to console if enabled
192+
if (!FileOnly)
193+
Console.WriteLine(line);
194+
178195
sw?.WriteLine(line);
196+
sw?.Flush();
179197
}
180198

181199
// Dispose of the writer

0 commit comments

Comments
 (0)