From deb634c61debbcc8537401a43b1955a31c5ae8ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sun, 15 Jan 2023 10:19:49 +0100 Subject: [PATCH] feat: process files from cli --- src/Anonymizer.CLI/Anonymizer.CLI.csproj | 15 ++++ src/Anonymizer.CLI/Options.cs | 33 +++++++++ src/Anonymizer.CLI/Program.cs | 91 ++++++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 src/Anonymizer.CLI/Options.cs create mode 100644 src/Anonymizer.CLI/Program.cs diff --git a/src/Anonymizer.CLI/Anonymizer.CLI.csproj b/src/Anonymizer.CLI/Anonymizer.CLI.csproj index 6836c68..5de6e9e 100644 --- a/src/Anonymizer.CLI/Anonymizer.CLI.csproj +++ b/src/Anonymizer.CLI/Anonymizer.CLI.csproj @@ -4,6 +4,21 @@ net7.0 enable enable + Exe + + + + + + + + + + + + + + diff --git a/src/Anonymizer.CLI/Options.cs b/src/Anonymizer.CLI/Options.cs new file mode 100644 index 0000000..9d755e8 --- /dev/null +++ b/src/Anonymizer.CLI/Options.cs @@ -0,0 +1,33 @@ +// +// Options.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using CommandLine; + +namespace Anonymizer.CLI; + +/// +/// CLI Options. +/// +public class Options +{ + /// + /// Gets or sets the input path. + /// + [Option('i', "input")] + public string Input { get; set; } = "input.log"; + + /// + /// Gets or sets the output path. + /// + [Option('o', "output")] + public string Output { get; set; } = "output.log"; + + /// + /// Gets or sets the config path. + /// + [Option('c', "config")] + public string Config { get; set; } = "config.json"; +} \ No newline at end of file diff --git a/src/Anonymizer.CLI/Program.cs b/src/Anonymizer.CLI/Program.cs new file mode 100644 index 0000000..b634731 --- /dev/null +++ b/src/Anonymizer.CLI/Program.cs @@ -0,0 +1,91 @@ +// +// Program.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using Anonymizer.Extensions; +using Anonymizer.Filters; +using Anonymizer.Sinks; +using CommandLine; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using NosSmooth.PacketSerializer.Extensions; +using NosSmooth.PacketSerializer.Packets; + +namespace Anonymizer.CLI; + +/// +/// A main class. +/// +public static class Program +{ + /// + /// A main method. + /// + /// The cli arguments. + /// The exit code. + public static int Main(string[] args) + { + return Parser.Default.ParseArguments(args) + .MapResult + ( + options => MakeAsync + (new FileInfo(options.Input), new FileInfo(options.Output), new FileInfo(options.Config)) + .GetAwaiter().GetResult(), + _ => 1 + ); + } + + private static async Task MakeAsync(FileInfo input, FileInfo output, FileInfo config) + { + if (!input.Exists) + { + Console.Error.WriteLine("The input file does not exist."); + return 1; + } + + if (output.Exists) + { + Console.Error.WriteLine("The output file already exists."); + return 1; + } + + if (!config.Exists) + { + Console.Error.WriteLine($"The config file {config.FullName} does not exist."); + return 1; + } + + IConfigurationRoot configuration = new ConfigurationBuilder() + .AddJsonFile(config.FullName) + .Build(); + + var services = new ServiceCollection() + .Configure(configuration.GetSection("HeaderFilter")) + .Configure(configuration.GetSection("PacketFile")) + .AddPacketSerialization() + .AddAnonymizer() + .BuildServiceProvider(); + + services.GetRequiredService().AddDefaultPackets(); + var processor = services.GetRequiredService(); + using var fileSink = new FileSink + ( + input.FullName, + output.FullName, + services.GetRequiredService>().Value + ); + + var result = await processor.ProcessSourceDestination(fileSink, fileSink); + if (!result.IsSuccess) + { + Console.WriteLine(result.Error); + } + + return 0; + } +} \ No newline at end of file -- 2.48.1