EasySign BETA
Digital Signing Tool
Loading...
Searching...
No Matches
Program.cs
Go to the documentation of this file.
1using System.CommandLine;
2using System.Text.Json.Serialization;
3using System.Text.Json;
4using System.Text;
5
6using Serilog;
7using Serilog.Extensions.Logging;
9using SAPTeam.CommonTK;
10using Spectre.Console;
11
13{
14 internal class Program
15 {
16 public static string AppDirectory => Context.GetApplicationDataDirectory("EasySign");
17
18 public static string ConfigPath => Path.Combine(AppDirectory, "config.json");
19
20 private static int Main(string[] args)
21 {
22 Log.Logger = new LoggerConfiguration()
23 .Enrich.WithThreadId()
24 .WriteTo.File(
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}"
28 )
29 .MinimumLevel.Debug() // Minimum log level
30 .CreateLogger();
31
32 Serilog.ILogger appLogger = Log.Logger.ForContext("Context", "Main");
33 appLogger.Information("Starting EasySign CLI");
34
35 Microsoft.Extensions.Logging.ILogger bundleLogger = new SerilogLoggerFactory(Log.Logger.ForContext("Context", "Bundle"))
36 .CreateLogger("CommandProvider");
37
38 Microsoft.Extensions.Logging.ILogger commandProviderLogger = new SerilogLoggerFactory(Log.Logger.ForContext("Context", "CommandProvider"))
39 .CreateLogger("CommandProvider");
40
41 if (!Directory.Exists(AppDirectory))
42 {
43 appLogger.Debug("Creating application data directory at {AppDirectory}", AppDirectory);
44 Directory.CreateDirectory(AppDirectory);
45 }
46
47 CommandProviderConfiguration? config = null;
48 if (File.Exists(ConfigPath))
49 {
50 appLogger.Information("Loading configuration from {ConfigPath}", ConfigPath);
51 FileStream fs = File.OpenRead(ConfigPath);
52
53 try
54 {
55 config = JsonSerializer.Deserialize(fs, typeof(CommandProviderConfiguration), SourceGenerationConfigurationContext.Default) as CommandProviderConfiguration ?? new CommandProviderConfiguration();
56 fs.Dispose();
57 }
58 catch (Exception ex)
59 {
60 fs.Dispose();
61
62 appLogger.Warning(ex, "Failed to load configuration from {ConfigPath}", ConfigPath);
63 config = null;
64
65 appLogger.Information("Creating backup of the old configuration file at {ConfigPath}.old", ConfigPath + ".old");
66 File.Copy(ConfigPath, ConfigPath + ".old", true);
67
68 appLogger.Information("Deleting the broken configuration file at {ConfigPath}", ConfigPath);
69 File.Delete(ConfigPath);
70
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.[/]");
73 }
74 }
75
76 if (config == null)
77 {
78 appLogger.Information("Loading default configuration");
79 config = new CommandProviderConfiguration();
80 }
81
82 appLogger.Debug("Loading SAP Team trusted certificates");
84
85 appLogger.Debug("Initializing command provider");
86 var cp = new BundleCommandProvider(config, commandProviderLogger, bundleLogger);
87
88 appLogger.Debug("Loading command line interface");
89 RootCommand root = cp.GetRootCommand();
90
91 appLogger.Information("Running command: {command}", args);
92 int exitCode = root.Invoke(args);
93 appLogger.Information("Command completed with exit code {exitCode}", exitCode);
94
95 appLogger.Information("Shutting down EasySign CLI");
96
97 appLogger.Debug("Saving configuration to {ConfigPath}", ConfigPath);
98 string data = JsonSerializer.Serialize(config, config.GetType(), SourceGenerationConfigurationContext.Default);
99
100 if(File.Exists(ConfigPath))
101 {
102 File.Delete(ConfigPath);
103 }
104
105 using (FileStream fs = File.Create(ConfigPath))
106 {
107 fs.Write(Encoding.UTF8.GetBytes(data));
108 }
109 appLogger.Debug("Configuration saved to {ConfigPath}", ConfigPath);
110
111 appLogger.Debug("Application shutdown successfully completed");
112 Log.CloseAndFlush();
113 return exitCode;
114 }
115 }
116}
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...