Skip to content

Commit dd1e496

Browse files
committed
Cleanup
1 parent 7894761 commit dd1e496

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

ProtectionScan/Features/MainFeature.cs

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal sealed class MainFeature : Feature
3030
internal readonly FlagInput FileOnlyInput = new(_fileOnlyName, ["-f", "--file"], "Print to file only");
3131

3232
#if NETCOREAPP
33-
private const string _jsonName = "json";
33+
private const string _jsonName = "json";
3434
internal readonly FlagInput JsonInput = new(_jsonName, ["-j", "--json"], "Output to json file");
3535
#endif
3636

@@ -48,15 +48,22 @@ internal sealed class MainFeature : Feature
4848

4949
#endregion
5050

51+
/// <summary>
52+
/// Enable debug output for relevant operations
53+
/// </summary>
54+
public bool Debug { get; private set; }
55+
5156
/// <summary>
5257
/// Output information to file only, skip printing to console
5358
/// </summary>
5459
public bool FileOnly { get; private set; }
55-
60+
61+
#if NETCOREAPP
5662
/// <summary>
57-
/// Output information to json
63+
/// Enable JSON output
5864
/// </summary>
59-
public bool JsonFlag { get; private set; }
65+
public bool Json { get; private set; }
66+
#endif
6067

6168
public MainFeature()
6269
: base(DisplayName, _flags, _description)
@@ -82,9 +89,10 @@ public override bool Execute()
8289
fileProgress.ProgressChanged += Changed;
8390

8491
// Get the options from the arguments
92+
Debug = GetBoolean(_debugName);
8593
FileOnly = GetBoolean(_fileOnlyName);
86-
#if NETCOREAPP
87-
JsonFlag = GetBoolean(_jsonName);
94+
#if NETCOREAPP
95+
Json = GetBoolean(_jsonName);
8896
#endif
8997

9098
// Create scanner for all paths
@@ -144,10 +152,10 @@ private void GetAndWriteProtections(Scanner scanner, string path)
144152
{
145153
var protections = scanner.GetProtections(path);
146154

147-
WriteProtectionResultFile(path, protections);
148-
155+
WriteProtectionResults(path, protections);
149156
#if NETCOREAPP
150-
WriteProtectionResultJson(path, protections);
157+
if (Json)
158+
WriteProtectionResultJson(path, protections);
151159
#endif
152160
}
153161
catch (Exception ex)
@@ -170,7 +178,7 @@ private void GetAndWriteProtections(Scanner scanner, string path)
170178
/// </summary>
171179
/// <param name="path">File or directory path</param>
172180
/// <param name="protections">Dictionary of protections found, if any</param>
173-
private void WriteProtectionResultFile(string path, Dictionary<string, List<string>> protections)
181+
private void WriteProtectionResults(string path, Dictionary<string, List<string>> protections)
174182
{
175183
if (protections == null)
176184
{
@@ -220,45 +228,41 @@ private void WriteProtectionResultFile(string path, Dictionary<string, List<stri
220228
// Dispose of the writer
221229
sw?.Dispose();
222230
}
223-
231+
224232
#if NETCOREAPP
225233
/// <summary>
226234
/// Write the protection results from a single path to a json file, if possible
227235
/// </summary>
228236
/// <param name="path">File or directory path</param>
229237
/// <param name="protections">Dictionary of protections found, if any</param>
230-
private static void WriteProtectionResultJson(string path, Dictionary<string, List<string>> protections)
238+
private void WriteProtectionResultJson(string path, Dictionary<string, List<string>> protections)
231239
{
232240
if (protections == null)
233241
{
234242
Console.WriteLine($"No protections found for {path}");
235243
return;
236244
}
237245

238-
// Attempt to open a protection file for writing
239-
StreamWriter? jsw = null;
240246
try
241247
{
242-
jsw = new StreamWriter(File.OpenWrite($"protection-{DateTime.Now:yyyy-MM-dd_HHmmss.ffff}.json"));
248+
// Attempt to open a protection file for writing
249+
using var jsw = new StreamWriter(File.OpenWrite($"protection-{DateTime.Now:yyyy-MM-dd_HHmmss.ffff}.json"));
250+
243251
// Create the output data
244-
string serializedData = System.Text.Json.JsonSerializer.Serialize(protections, JsonSerializerOptions);
252+
var jsonSerializerOptions = new System.Text.Json.JsonSerializerOptions { WriteIndented = true };
253+
string serializedData = System.Text.Json.JsonSerializer.Serialize(protections, jsonSerializerOptions);
245254

246255
// Write the output data
247256
// TODO: this prints plus symbols wrong, probably some other things too
248-
jsw?.WriteLine(serializedData);
249-
jsw?.Flush();
250-
251-
// Dispose of the writer
252-
jsw?.Dispose();
257+
jsw.WriteLine(serializedData);
258+
jsw.Flush();
259+
}
260+
catch (Exception ex)
261+
{
262+
Console.WriteLine(Debug ? ex : "[Exception opening file, please try again]");
263+
Console.WriteLine();
253264
}
254-
catch { }
255265
}
256-
257-
/// <summary>
258-
/// JSON serializer options for output printing
259-
/// </summary>
260-
private static System.Text.Json.JsonSerializerOptions JsonSerializerOptions
261-
=> new System.Text.Json.JsonSerializerOptions { WriteIndented = true };
262266
#endif
263267
}
264268
}

0 commit comments

Comments
 (0)