ImgMetadataRemover/ConsoleInterface/Program.cs

114 lines
4 KiB
C#
Raw Normal View History

using CommandLine;
2022-01-22 17:06:10 +00:00
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
2022-01-22 17:06:10 +00:00
2022-01-23 13:12:17 +00:00
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
2022-01-22 17:06:10 +00:00
namespace ConsoleInterface
{
public static class Program
2022-01-22 17:06:10 +00:00
{
private static ILoggerFactory _loggerFactory;
2022-02-12 20:25:10 +00:00
public static ILogger Logger = NullLogger.Instance;
2022-01-23 17:30:05 +00:00
/// <summary>
/// The console interface for the project and the main entrypoint.
/// </summary>
/// <param name="args">Command line provided args.</param>
2022-01-27 21:22:10 +00:00
// ReSharper disable once UnusedMember.Local
public static void Main(string[] args)
2022-01-23 13:12:17 +00:00
{
2022-02-12 20:25:10 +00:00
Parser.Default.ParseArguments<ProgramOptions>(args).WithParsed(RunOptions);
2022-01-23 13:12:17 +00:00
}
2022-01-23 17:30:05 +00:00
/// <summary>
/// RunOptions will be called after the command-line arguments were successfully parsed.
/// </summary>
2022-02-12 20:25:10 +00:00
private static void RunOptions(ProgramOptions options)
2022-01-22 17:06:10 +00:00
{
2022-02-12 20:25:10 +00:00
SetupLogging(options.LogLevel);
var outputFormatter = SimpleOutputSink.Create(options.DestinationDirectory);
2022-01-23 13:12:17 +00:00
var executor = TaskExecutor.Create(new TaskExecutorOptions
2022-01-22 17:06:10 +00:00
{
2022-01-23 13:12:17 +00:00
EnableCompression = options.CompressFiles is true,
2022-02-12 20:25:10 +00:00
OutputSink = outputFormatter
2022-01-23 13:12:17 +00:00
});
2022-02-12 20:25:10 +00:00
var filesRetriever = LocalFileBrowser.Create();
2022-01-23 13:12:17 +00:00
executor.ParallelCleanImages(filesRetriever.GetFilenamesFromPath(options.SourceDirectory));
}
2022-02-12 20:25:10 +00:00
public static void SetupLogging(string logLevel)
2022-01-23 13:12:17 +00:00
{
2022-02-12 20:25:10 +00:00
_loggerFactory = LoggerFactory.Create(b =>
{
b.AddConsole();
var logLevelToBeSet = LogLevel.Information;
switch (logLevel.ToLower())
{
case "trace":
case "t":
{
logLevelToBeSet = LogLevel.Trace;
break;
}
case "debug":
case "d":
{
logLevelToBeSet = LogLevel.Debug;
break;
}
case "information":
case "i":
case "info":
{
logLevelToBeSet = LogLevel.Information;
break;
}
case "warning":
case "warn":
case "w":
{
logLevelToBeSet = LogLevel.Warning;
break;
}
case "error":
case "err":
case "e":
{
logLevelToBeSet = LogLevel.Error;
break;
}
case "critical":
case "crt":
case "c":
{
logLevelToBeSet = LogLevel.Critical;
break;
}
case "none":
case "n":
{
logLevelToBeSet = LogLevel.None;
break;
}
}
2022-01-23 13:12:17 +00:00
2022-02-12 20:25:10 +00:00
b.SetMinimumLevel(logLevelToBeSet);
});
Logger = _loggerFactory.CreateLogger(nameof(Program));
// Tasks
TaskExecutor.Logger = _loggerFactory.CreateLogger(nameof(TaskExecutor));
// File Retriever
LocalFileBrowser.Logger = _loggerFactory.CreateLogger(nameof(LocalFileBrowser));
// FileName formatter
SimpleOutputSink.Logger =
_loggerFactory.CreateLogger(nameof(SimpleOutputSink));
FileSystemHelpers.Logger = _loggerFactory.CreateLogger(nameof(FileSystemHelpers));
Logger.LogTrace("SetupLogging - exit");
2022-01-22 17:06:10 +00:00
}
}
}