Hermod
A cross-platform, modular and fully GDPR-compliant email archival solution!
Loading...
Searching...
No Matches
Program.cs
Go to the documentation of this file.
1using System;
2
3namespace Hermod {
4
5 using Config;
6
7 using getopt.net;
8
9 using Serilog;
10 using Serilog.Events;
11
12 using System.IO;
13 using System.Reflection;
14
20 class Program {
21
22 private static string _shortOpts = "c:L:hv%Ui"; // the options used for this application
23 private static Option[] _longOpts = new[] {
24 new Option { Name = "config", ArgumentType = ArgumentType.Required, Value = 'c' },
25 new Option { Name = "log-lvl", ArgumentType = ArgumentType.Required, Value = 'L' },
26 new Option { Name = "help", ArgumentType = ArgumentType.None, Value = 'h' },
27 new Option { Name = "version", ArgumentType = ArgumentType.None, Value = 'v' },
28 new Option { Name = "check-updates", ArgumentType = ArgumentType.None, Value = 'U' },
29 new Option { Name = "reset-cfg", ArgumentType = ArgumentType.Optional, Value = '%' },
30 new Option { Name = "interactive", ArgumentType = ArgumentType.None, Value = 'i' }
31 // add more as required
32 };
33
34 private static FileInfo? _overriddenConfigLocation = null;
35 private static ILogger? _appLogger = null;
36 private static LogEventLevel? _logLevel = null;
38 private static bool _interactiveMode = false;
39
40 static async Task<int> Main(string[] args) {
41 var returnCode = ParseArgs(args);
42 if (returnCode != 0) { return returnCode - 1; }
43
45
47
48 _cfgManager.AppLogger = _appLogger;
49
50 var app = new Hermod(_cfgManager, _appLogger ?? Log.Logger) {
51 InteractiveMode = _interactiveMode,
52 m_keepAlive = true
53 };
54 app.StartUp();
55
56 return await app.Execute();
57 }
58
62 static void InitialiseConfigs() {
63 if (_overriddenConfigLocation != null) {
64 _cfgManager.ConfigFile = _overriddenConfigLocation;
65 } else {
66 _cfgManager.LoadConfig();
67 }
68 }
69
73 static void InitialiseLogger() {
74 var consoleSettings = _cfgManager.GetConsoleLoggerConfig();
75 var fileSettings = _cfgManager.GetFileLoggerConfig();
76 Core.Logger logger = new Core.Logger() {
77 ConsoleLogLevel = _logLevel ?? GetLogLevelFromArg(consoleSettings.LogLevel) ?? LogEventLevel.Warning,
78 EnableConsoleOutput = consoleSettings.EnableLogging,
79
80 FileLogLevel = GetLogLevelFromArg(fileSettings.LogLevel) ?? LogEventLevel.Information,
81 EnableFileOutput = fileSettings.EnableLogging
82 };
83
84 _appLogger = logger.GetLogger();
85 }
86
92 static int ParseArgs(string[] args) {
93
94 var getopt = new GetOpt {
95 AppArgs = args,
96 DoubleDashStopsParsing = true,
97 Options = _longOpts,
98 ShortOpts = _shortOpts
99 };
100
101 int optChar = -1;
102 do {
103 optChar = getopt.GetNextOpt(out var optArg);
104
105 switch (optChar) {
106 case -1:
107 case 0:
108 break;
109 case 'h':
110 PrintHelp();
111 return 1;
112 case 'v':
113 PrintVersion();
114 return 1;
115 case 'c':
116 if (string.IsNullOrEmpty(optArg)) {
117 Console.Error.WriteLine("Missing argument for --config!");
118 return 2;
119 }
120 _overriddenConfigLocation = new FileInfo(optArg);
121 break;
122 case 'L':
123 if (string.IsNullOrEmpty(optArg)) {
124 Console.Error.WriteLine("Missing argument for --log-lvl!");
125 return 2;
126 }
128 break;
129 case 'i':
130 _interactiveMode = true;
131 break;
132 default:
133 Console.Error.WriteLine($"The argument { optChar } is not yet implemented!");
134 break;
135 }
136 } while (optChar != -1);
137
138 return 0;
139 }
140
144 static void PrintHelp() {
145 Console.WriteLine(
146"""
147hermod {0} - A high-performance, cross-platform email archival and search engine
148
149Usage: (hermod has full getopt support)
150 hermod # no options = normal execution
151 hermod [options]
152
153Switches:
154 --help, -h Display this entry and exit
155 --version, -v Display the application version and exit
156 --reset-cfg, -% !!! DANGER !!! Resets the configurations to their default values!
157
158Arguments:
159 --config, -c<config> Override the application config file location
160 --log-lvl, -L<lvl> Override the log level: debug, trace, error, warning (default), info, critical
161""",
163 );
164 }
165
170 static string GetApplicationVersion() {
171 var version = Assembly.GetExecutingAssembly().GetName().Version;
172
173 return $"v{ version?.Major }.{ version?.MajorRevision }.{ version?.Minor }.{ version?.MinorRevision }";
174 }
175
179 static void PrintVersion() => Console.WriteLine("hermod {0}", GetApplicationVersion());
180
186 static LogEventLevel? GetLogLevelFromArg(string? arg) {
187 arg = arg?.ToLowerInvariant() ?? string.Empty;
188
189 switch (arg) {
190 case "debug":
191 return LogEventLevel.Debug;
192 case "trace":
193 return LogEventLevel.Verbose;
194 case "warn":
195 case "warning":
196 return LogEventLevel.Warning;
197 case "error":
198 return LogEventLevel.Error;
199 case "info":
200 case "information":
201 return LogEventLevel.Information;
202 case "fatal":
203 return LogEventLevel.Fatal;
204 default:
205 return null;
206 }
207 }
208
209 }
210
211}
Provides an application-wide, thread safe way of getting and setting configurations relevant to the m...
static ConfigManager Instance
Gets the application-wide instance of the ConfigManager.
Custom logger class for Hermod; internally uses Serilog.
Definition: Logger.cs:12
ILogger GetLogger()
Gets the instance of the Serilog logger.
Hermod's entry point.
Definition: Program.cs:20
static string GetApplicationVersion()
Gets the application's version string.
Definition: Program.cs:170
static ? LogEventLevel GetLogLevelFromArg(string? arg)
Converts an incoming application argument to its respective log level.
Definition: Program.cs:186
static ? FileInfo _overriddenConfigLocation
Definition: Program.cs:34
static void PrintHelp()
Prints the help text for hermod.
Definition: Program.cs:144
static void PrintVersion()
Prints the application's version.
static ConfigManager _cfgManager
Definition: Program.cs:37
static Option[] _longOpts
Definition: Program.cs:23
static ? ILogger _appLogger
Definition: Program.cs:35
static int ParseArgs(string[] args)
Parses the command-line arguments passed to the application.
Definition: Program.cs:92
static ? LogEventLevel _logLevel
Definition: Program.cs:36
static bool _interactiveMode
Definition: Program.cs:38
static void InitialiseLogger()
Initialises the application's logger.
Definition: Program.cs:73
static string _shortOpts
Definition: Program.cs:22
static async Task< int > Main(string[] args)
Definition: Program.cs:40
static void InitialiseConfigs()
Initialises the application's config manager.
Definition: Program.cs:62