Skip to content

Commit ca12338

Browse files
getting started
1 parent f39a527 commit ca12338

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+727
-9
lines changed

CSVReportGenerator/CSVReportGenerator.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,11 @@
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="Serilog" Version="4.3.0" />
12+
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
13+
<PackageReference Include="Serilog.Sinks.Debug" Version="3.0.0" />
14+
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
15+
</ItemGroup>
16+
1017
</Project>

CSVReportGenerator/Exceptions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
internal class UnknownErrorException : Exception
3+
{
4+
public UnknownErrorException()
5+
{
6+
}
7+
8+
public UnknownErrorException(string? message) : base(message)
9+
{
10+
}
11+
12+
public UnknownErrorException(string? message, Exception? innerException) : base(message, innerException)
13+
{
14+
}
15+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
2+
public sealed class InputArguments
3+
{
4+
private static readonly InputArguments _instance = new InputArguments();
5+
6+
private InputArguments() { }
7+
8+
public static InputArguments Instance => _instance;
9+
10+
public string OutputSchemaFile { get; set; }
11+
public string InputFileFilter { get; set; }
12+
public List<string> InputFiles { get; set; }
13+
14+
public static InputArguments Parse(string[] args)
15+
{
16+
Serilog.Log.Debug("Parsing input arguments.");
17+
var instance = Instance;
18+
19+
ParseArgs(instance, args);
20+
21+
Serilog.Log.Information("Parsing input arguments completed.");
22+
23+
try
24+
{
25+
ValidateArgPaths(instance);
26+
}
27+
catch (Exception ex)
28+
{
29+
if (ex is FileNotFoundException || ex is UnauthorizedAccessException)
30+
{
31+
throw new FileNotFoundException("Argument Path Validation Failed", ex);
32+
}
33+
else
34+
{
35+
throw new UnknownErrorException("An unknown error occurred during argument path validation.", ex);
36+
}
37+
}
38+
Serilog.Log.Information("File/Folder validation completed.");
39+
40+
return instance;
41+
}
42+
43+
private static void ValidateArgPaths(InputArguments instance)
44+
{
45+
if (string.IsNullOrWhiteSpace(instance.OutputSchemaFile) || !File.Exists(instance.OutputSchemaFile))
46+
{
47+
Serilog.Log.Fatal($"Output schema file not found: {instance.OutputSchemaFile}");
48+
throw new FileNotFoundException($"Output schema file not found: {instance.OutputSchemaFile}");
49+
}
50+
51+
if (instance.InputFiles == null || instance.InputFiles.Count == 0)
52+
{
53+
Serilog.Log.Information("No input files or folders specified. Using current directory.");
54+
instance.InputFiles = [Directory.GetCurrentDirectory()];
55+
}
56+
57+
foreach (var path in instance.InputFiles)
58+
{
59+
if (!File.Exists(path) && !Directory.Exists(path))
60+
{
61+
Serilog.Log.Fatal($"Unable to utilize input path: {path}");
62+
throw new FileNotFoundException($"Input file or folder not found: {path}");
63+
}
64+
}
65+
}
66+
67+
private static void ParseArgs(InputArguments instance,string[] args)
68+
{
69+
Serilog.Log.Debug("Parsing input arguments.");
70+
instance.InputFiles = new List<string>();
71+
72+
// Parse arguments
73+
for (int i = 0; i < args.Length; i++)
74+
{
75+
if (args[i] == "-outputSchema" && i + 1 < args.Length)
76+
{
77+
instance.OutputSchemaFile = args[i + 1];
78+
i++;
79+
}
80+
else if (args[i] == "-filter" && i + 1 < args.Length)
81+
{
82+
instance.InputFileFilter = args[i + 1];
83+
i++;
84+
}
85+
else if (args[i] == "-input" && i + 1 < args.Length)
86+
{
87+
int j = i + 1;
88+
while (j < args.Length && !args[j].StartsWith("-"))
89+
{
90+
instance.InputFiles.Add(args[j]);
91+
j++;
92+
}
93+
i = j - 1;
94+
}
95+
}
96+
97+
}
98+
}

CSVReportGenerator/Program.cs

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,88 @@
11
// See https://aka.ms/new-console-template for more information
2-
Console.WriteLine("Hello, World!");
2+
3+
using Serilog;
4+
5+
/// <summary>
6+
/// CSVReportGenerator
7+
/// This program generates CSV reports based on XML input files and a specified output schema.
8+
/// It allows for filtering input files and specifying multiple input paths.
9+
/// The program uses Serilog for logging and handles various exceptions related to file paths and access.
10+
/// It requires an output schema file to define the structure of the generated CSV.
11+
/// The input files can be specified with a filter, and if no input paths are provided,
12+
/// the program defaults to the current directory.
13+
///
14+
/// TO DO:
15+
/// - Create XML parser
16+
/// - generate csv based on schema
17+
/// - utilize sufficent abstraction to allow for substition of output formats such as JSON
18+
///
19+
/// DONE:
20+
/// - Implemented Logger
21+
/// - Argument parsing
22+
/// - Create basic test cases
23+
/// </summary>
24+
25+
26+
27+
//First Argument - Output schema file
28+
//-filter - next arg is used for Matching input file filters
29+
//-input - Input XML file/folder
30+
//If no input path is provided, the program will look for files in the current directory.
31+
//If no filter is provided, the program will use .XML files in the current directory.
32+
class CSVReportGenerator
33+
{
34+
static void Main(string[] args)
35+
{
36+
Console.WriteLine("Hello, World!");
37+
38+
Serilog.Log.Logger = new Serilog.LoggerConfiguration()
39+
.WriteTo.Console()
40+
.WriteTo.File("logs/log.txt", retainedFileCountLimit: 10, rollingInterval: Serilog.RollingInterval.Day)
41+
.CreateLogger();
42+
43+
Serilog.Log.Information("Serilog initialized."); //Debug, Information, Warning, Error, Fatal
44+
45+
//Check Arguments
46+
if (args.Length < 1)
47+
{
48+
Serilog.Log.Error("No output schema file provided.");
49+
Console.WriteLine("Usage: CSVReportGenerator -outputSchema [<output_schema_file>] -filter [<input_file_filters>] -input [<input_files>]");
50+
return;
51+
}
52+
53+
try
54+
{
55+
56+
InputArguments inputArgs = InputArguments.Parse(args);
57+
string outputSchemaFile = inputArgs.OutputSchemaFile;
58+
//validate output schema file
59+
Serilog.Log.Information($"Verifying existence of schema file: {outputSchemaFile}");
60+
61+
// Check if output schema file exists
62+
if (!File.Exists(outputSchemaFile))
63+
{
64+
Serilog.Log.Error($"Output schema file not found: {outputSchemaFile}");
65+
return;
66+
}
67+
68+
69+
}
70+
catch (Exception ex)
71+
{
72+
if (ex is FileNotFoundException || ex is UnauthorizedAccessException)
73+
{
74+
Serilog.Log.Fatal(ex, "There was an issue with the file paths provided. Terminating program without processing.");
75+
}
76+
else if (ex is UnknownErrorException)
77+
{
78+
Serilog.Log.Fatal(ex, "An unknown Fatal Error occurred. Terminating Program.");
79+
}
80+
else
81+
{
82+
Serilog.Log.Fatal(ex, "An unhandled error occurred. Terminating Program.");
83+
}
84+
}
85+
}
86+
}
87+
//XML Parser
88+
//
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"runtimeTarget": {
3+
"name": ".NETCoreApp,Version=v9.0",
4+
"signature": ""
5+
},
6+
"compilationOptions": {},
7+
"targets": {
8+
".NETCoreApp,Version=v9.0": {
9+
"CSVReportGenerator/1.0.0": {
10+
"runtime": {
11+
"CSVReportGenerator.dll": {}
12+
}
13+
}
14+
}
15+
},
16+
"libraries": {
17+
"CSVReportGenerator/1.0.0": {
18+
"type": "project",
19+
"serviceable": false,
20+
"sha512": ""
21+
}
22+
}
23+
}
5 KB
Binary file not shown.
145 KB
Binary file not shown.
10.5 KB
Binary file not shown.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"runtimeOptions": {
3+
"tfm": "net9.0",
4+
"framework": {
5+
"name": "Microsoft.NETCore.App",
6+
"version": "9.0.0"
7+
},
8+
"configProperties": {
9+
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
10+
}
11+
}
12+
}

CSVReportGenerator/obj/CSVReportGenerator.csproj.nuget.dgspec.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@
4343
"frameworks": {
4444
"net9.0": {
4545
"targetAlias": "net9.0",
46+
"dependencies": {
47+
"Serilog": {
48+
"target": "Package",
49+
"version": "[4.3.0, )"
50+
},
51+
"Serilog.Sinks.Console": {
52+
"target": "Package",
53+
"version": "[6.0.0, )"
54+
},
55+
"Serilog.Sinks.Debug": {
56+
"target": "Package",
57+
"version": "[3.0.0, )"
58+
},
59+
"Serilog.Sinks.File": {
60+
"target": "Package",
61+
"version": "[7.0.0, )"
62+
}
63+
},
4664
"imports": [
4765
"net461",
4866
"net462",

0 commit comments

Comments
 (0)