Skip to content

Commit

Permalink
Revert "changes"
Browse files Browse the repository at this point in the history
This reverts commit 2c3d294.
  • Loading branch information
pawelvds committed Jan 29, 2024
1 parent fb54d22 commit 4ddffed
Showing 1 changed file with 103 additions and 97 deletions.
200 changes: 103 additions & 97 deletions src/fiskaltrust.Launcher/Commands/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,65 +17,70 @@
using Serilog;
using Serilog.Events;

namespace fiskaltrust.Launcher.Commands
{
public record SubArguments(IEnumerable<string> Args);
namespace fiskaltrust.Launcher.Commands;

public record SubArguments(IEnumerable<string> Args);

public class CommonCommand : Command
public class CommonCommand : Command
{
public CommonCommand(string name, bool addCliOnlyParameters = true) : base(name)
{
public CommonCommand(string name, bool addCliOnlyParameters = true) : base(name)
{
AddOption(new Option<Guid?>("--cashbox-id"));
AddOption(new Option<string?>("--access-token"));
AddOption(new Option<bool>("--sandbox"));
AddOption(new Option<string?>("--log-folder"));
AddOption(new Option<Guid?>("--cashbox-id"));
AddOption(new Option<string?>("--access-token"));
AddOption(new Option<bool>("--sandbox"));
AddOption(new Option<string?>("--log-folder"));

var logLevelOption = new Option<LogLevel?>("--log-level", "Set the log level of the application.");
logLevelOption.AddAlias("-v");
logLevelOption.AddAlias("--verbosity");
AddOption(logLevelOption);
var logLevelOption = new Option<LogLevel?>("--log-level", "Set the log level of the application.");
logLevelOption.AddAlias("-v");
logLevelOption.AddAlias("--verbosity");
AddOption(logLevelOption);

if (addCliOnlyParameters)
{
AddOption(new Option<string>("--launcher-configuration-file", getDefaultValue: () => Paths.LauncherConfigurationFileName));
AddOption(new Option<string>("--legacy-configuration-file", getDefaultValue: () => Paths.LegacyConfigurationFileName));
AddOption(new Option<bool>("--merge-legacy-config-if-exists", getDefaultValue: () => true));
}
if (addCliOnlyParameters)
{
AddOption(new Option<string>("--launcher-configuration-file",
() => Paths.LauncherConfigurationFileName));
AddOption(new Option<string>("--legacy-configuration-file",
() => Paths.LegacyConfigurationFileName));
AddOption(new Option<bool>("--merge-legacy-config-if-exists", () => true));
}
}
}

public class CommonOptions
public class CommonOptions
{
public CommonOptions(LauncherConfiguration argsLauncherConfiguration, string launcherConfigurationFile,
string legacyConfigurationFile, bool mergeLegacyConfigIfExists)
{
public CommonOptions(LauncherConfiguration argsLauncherConfiguration, string launcherConfigurationFile, string legacyConfigurationFile, bool mergeLegacyConfigIfExists)
{
ArgsLauncherConfiguration = argsLauncherConfiguration;
LauncherConfigurationFile = launcherConfigurationFile;
LegacyConfigurationFile = legacyConfigurationFile;
MergeLegacyConfigIfExists = mergeLegacyConfigIfExists;
}

public LauncherConfiguration ArgsLauncherConfiguration { get; set; }
public string LauncherConfigurationFile { get; set; }
public string LegacyConfigurationFile { get; set; }
public bool MergeLegacyConfigIfExists { get; set; }
ArgsLauncherConfiguration = argsLauncherConfiguration;
LauncherConfigurationFile = launcherConfigurationFile;
LegacyConfigurationFile = legacyConfigurationFile;
MergeLegacyConfigIfExists = mergeLegacyConfigIfExists;
}

public record CommonProperties
{
public CommonProperties(LauncherConfiguration launcherConfiguration, ftCashBoxConfiguration cashboxConfiguration, ECDiffieHellman clientEcdh, IDataProtectionProvider dataProtectionProvider)
{
LauncherConfiguration = launcherConfiguration;
CashboxConfiguration = cashboxConfiguration;
ClientEcdh = clientEcdh;
DataProtectionProvider = dataProtectionProvider;
}
public LauncherConfiguration ArgsLauncherConfiguration { get; set; }
public string LauncherConfigurationFile { get; set; }
public string LegacyConfigurationFile { get; set; }
public bool MergeLegacyConfigIfExists { get; set; }
}

public LauncherConfiguration LauncherConfiguration { get; set; }
public ftCashBoxConfiguration CashboxConfiguration { get; set; }
public ECDiffieHellman ClientEcdh { get; set; }
public IDataProtectionProvider DataProtectionProvider { get; set; }
public record CommonProperties
{
public CommonProperties(LauncherConfiguration launcherConfiguration,
ftCashBoxConfiguration cashboxConfiguration, ECDiffieHellman clientEcdh,
IDataProtectionProvider dataProtectionProvider)
{
LauncherConfiguration = launcherConfiguration;
CashboxConfiguration = cashboxConfiguration;
ClientEcdh = clientEcdh;
DataProtectionProvider = dataProtectionProvider;
}

public LauncherConfiguration LauncherConfiguration { get; set; }
public ftCashBoxConfiguration CashboxConfiguration { get; set; }
public ECDiffieHellman ClientEcdh { get; set; }
public IDataProtectionProvider DataProtectionProvider { get; set; }
}

public static class CommonHandler
{
public static async Task<int> HandleAsync<O, S>(
Expand All @@ -92,45 +97,43 @@ public static async Task<int> HandleAsync<O, S>(

var launcherConfiguration = new LauncherConfiguration();

Log.Verbose("Reading launcher config file.");
try
Log.Verbose("Reading launcher config file.");
try
{
options.LauncherConfigurationFile = Path.GetFullPath(options.LauncherConfigurationFile);
launcherConfiguration =
LauncherConfiguration.Deserialize(await File.ReadAllTextAsync(options.LauncherConfigurationFile));
}
catch (Exception e)
{
if (!(options.MergeLegacyConfigIfExists && File.Exists(options.LegacyConfigurationFile)))
{
options.LauncherConfigurationFile = Path.GetFullPath(options.LauncherConfigurationFile);
launcherConfiguration = LauncherConfiguration.Deserialize(await File.ReadAllTextAsync(options.LauncherConfigurationFile));
if (File.Exists(options.LauncherConfigurationFile))
Log.Warning(e, "Could not parse launcher configuration file \"{LauncherConfigurationFile}\".",
options.LauncherConfigurationFile);
else
Log.Warning("Launcher configuration file \"{LauncherConfigurationFile}\" does not exist.",
options.LauncherConfigurationFile);

Log.Warning("Using command line parameters only.", options.LauncherConfigurationFile);
}
catch (Exception e)
{
if (!(options.MergeLegacyConfigIfExists && File.Exists(options.LegacyConfigurationFile)))
{
if (File.Exists(options.LauncherConfigurationFile))
{
Log.Warning(e, "Could not parse launcher configuration file \"{LauncherConfigurationFile}\".", options.LauncherConfigurationFile);
}
else
{
Log.Warning("Launcher configuration file \"{LauncherConfigurationFile}\" does not exist.", options.LauncherConfigurationFile);
}
Log.Warning("Using command line parameters only.", options.LauncherConfigurationFile);
}
}
Log.Verbose("Merging legacy launcher config file.");
if (options.MergeLegacyConfigIfExists && File.Exists(options.LegacyConfigurationFile))
{
var legacyConfig = await LegacyConfigFileReader.ReadLegacyConfigFile(options.LegacyConfigurationFile);
launcherConfiguration.OverwriteWith(legacyConfig);
}

var configFileDirectory = Path.GetDirectoryName(Path.GetFullPath(options.LauncherConfigurationFile));
if (configFileDirectory is not null)
{
Directory.CreateDirectory(configFileDirectory);
}
Log.Verbose("Merging legacy launcher config file.");
if (options.MergeLegacyConfigIfExists && File.Exists(options.LegacyConfigurationFile))
{
var legacyConfig = await LegacyConfigFileReader.ReadLegacyConfigFile(options.LegacyConfigurationFile);
launcherConfiguration.OverwriteWith(legacyConfig);

await File.WriteAllTextAsync(options.LauncherConfigurationFile, legacyConfig.Serialize());
var configFileDirectory = Path.GetDirectoryName(Path.GetFullPath(options.LauncherConfigurationFile));
if (configFileDirectory is not null) Directory.CreateDirectory(configFileDirectory);

var fi = new FileInfo(options.LegacyConfigurationFile);
fi.CopyTo(options.LegacyConfigurationFile + ".legacy");
fi.Delete();
}
await File.WriteAllTextAsync(options.LauncherConfigurationFile, legacyConfig.Serialize());

var fi = new FileInfo(options.LegacyConfigurationFile);
fi.CopyTo(options.LegacyConfigurationFile + ".legacy");
fi.Delete();
}

Log.Verbose("Merging launcher cli args.");
launcherConfiguration.OverwriteWith(options.ArgsLauncherConfiguration);
Expand Down Expand Up @@ -199,24 +202,27 @@ public static async Task<int> HandleAsync<O, S>(
Log.Fatal(e, "Could not read Cashbox configuration file.");
}

var cashboxConfiguration = new ftCashBoxConfiguration();
try
{
cashboxConfiguration = CashBoxConfigurationExt.Deserialize(await File.ReadAllTextAsync(launcherConfiguration.CashboxConfigurationFile!));
if (clientEcdh is not null) { cashboxConfiguration.Decrypt(launcherConfiguration, clientEcdh); }
}
catch (Exception e)
{
// will exit with non-zero exit code later.
Log.Fatal(e, "Could not parse Cashbox configuration.");
}
var cashboxConfiguration = new ftCashBoxConfiguration();
try
{
cashboxConfiguration =
CashBoxConfigurationExt.Deserialize(
await File.ReadAllTextAsync(launcherConfiguration.CashboxConfigurationFile!));
if (clientEcdh is not null) cashboxConfiguration.Decrypt(launcherConfiguration, clientEcdh);
}
catch (Exception e)
{
// will exit with non-zero exit code later.
Log.Fatal(e, "Could not parse Cashbox configuration.");
}

// Previous log messages will be logged here using this logger.
Log.Logger = new LoggerConfiguration()
.AddLoggingConfiguration(launcherConfiguration)
.AddFileLoggingConfiguration(launcherConfiguration, new[] { "fiskaltrust.Launcher", launcherConfiguration.CashboxId?.ToString() })
.Enrich.FromLogContext()
.CreateLogger();
// Previous log messages will be logged here using this logger.
Log.Logger = new LoggerConfiguration()
.AddLoggingConfiguration(launcherConfiguration)
.AddFileLoggingConfiguration(launcherConfiguration,
new[] { "fiskaltrust.Launcher", launcherConfiguration.CashboxId?.ToString() })
.Enrich.FromLogContext()
.CreateLogger();

foreach (var logEvent in collectionSink.Events) Log.Write(logEvent);

Expand Down

0 comments on commit 4ddffed

Please sign in to comment.