1using System.CommandLine;
2using System.Text.Json.Serialization;
7using Serilog.Extensions.Logging;
14 internal class Program
16 public static string AppDirectory => Context.GetApplicationDataDirectory(
"EasySign");
18 public static string ConfigPath => Path.Combine(AppDirectory,
"config.json");
20 private static int Main(
string[] args)
22 Log.Logger =
new LoggerConfiguration()
23 .Enrich.WithThreadId()
25 Path.Combine(AppDirectory,
"logs/log-.txt"),
26 rollingInterval: RollingInterval.Day,
27 outputTemplate:
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Context}({ThreadId}): {Message} {NewLine}{Exception}"
32 Serilog.ILogger appLogger = Log.Logger.ForContext(
"Context",
"Main");
33 appLogger.Information(
"Starting EasySign CLI");
35 Microsoft.Extensions.Logging.ILogger bundleLogger =
new SerilogLoggerFactory(Log.Logger.ForContext(
"Context",
"Bundle"))
36 .CreateLogger(
"CommandProvider");
38 Microsoft.Extensions.Logging.ILogger commandProviderLogger =
new SerilogLoggerFactory(Log.Logger.ForContext(
"Context",
"CommandProvider"))
39 .CreateLogger(
"CommandProvider");
41 if (!Directory.Exists(AppDirectory))
43 appLogger.Debug(
"Creating application data directory at {AppDirectory}", AppDirectory);
44 Directory.CreateDirectory(AppDirectory);
48 if (File.Exists(ConfigPath))
50 appLogger.Information(
"Loading configuration from {ConfigPath}", ConfigPath);
51 FileStream fs = File.OpenRead(ConfigPath);
62 appLogger.Warning(ex,
"Failed to load configuration from {ConfigPath}", ConfigPath);
65 appLogger.Information(
"Creating backup of the old configuration file at {ConfigPath}.old", ConfigPath +
".old");
66 File.Copy(ConfigPath, ConfigPath +
".old",
true);
68 appLogger.Information(
"Deleting the broken configuration file at {ConfigPath}", ConfigPath);
69 File.Delete(ConfigPath);
71 AnsiConsole.MarkupLine($
"[yellow]Failed to load configuration file. A backup has been created at {ConfigPath + ".old
"}[/]");
72 AnsiConsole.MarkupLine(
"[yellow]A new configuration file will be created with default values.[/]");
78 appLogger.Information(
"Loading default configuration");
82 appLogger.Debug(
"Loading SAP Team trusted certificates");
85 appLogger.Debug(
"Initializing command provider");
86 var cp =
new BundleCommandProvider(config, commandProviderLogger, bundleLogger);
88 appLogger.Debug(
"Loading command line interface");
89 RootCommand root = cp.GetRootCommand();
91 appLogger.Information(
"Running command: {command}", args);
92 int exitCode = root.Invoke(args);
93 appLogger.Information(
"Command completed with exit code {exitCode}", exitCode);
95 appLogger.Information(
"Shutting down EasySign CLI");
97 appLogger.Debug(
"Saving configuration to {ConfigPath}", ConfigPath);
98 string data = JsonSerializer.Serialize(config, config.GetType(), SourceGenerationConfigurationContext.Default);
100 if(File.Exists(ConfigPath))
102 File.Delete(ConfigPath);
105 using (FileStream fs = File.Create(ConfigPath))
107 fs.Write(Encoding.UTF8.GetBytes(data));
109 appLogger.Debug(
"Configuration saved to {ConfigPath}", ConfigPath);
111 appLogger.Debug(
"Application shutdown successfully completed");
Represents the configuration for the EasySign command provider.
void AddSAPTeamCertificates()
Adds the SAP Team certificates to the trusted root CA and intermediate CA stores and Locks the saptea...