diff options
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | slskd.sln | 2 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 130 | ||||
| -rw-r--r-- | src/slskd/slskd.csproj | 1 |
4 files changed, 94 insertions, 42 deletions
@@ -1,2 +1,3 @@ .vs -dist/
\ No newline at end of file +dist/ +logs/
\ No newline at end of file @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "slskd", "src\slskd\slskd.cs EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{CA2A6837-A966-4198-9B62-32D0110CA078}" ProjectSection(SolutionItems) = preProject + .dockerignore = .dockerignore + .gitignore = .gitignore Dockerfile = Dockerfile LICENSE = LICENSE README.md = README.md diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 44f52db9..bd1d1d77 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -10,53 +10,83 @@ using System.Diagnostics; using System.Linq; using System.Reflection; + using Utility.CommandLine; public class Program { + [Argument('h', "help", "print usage")] + public static bool ShowHelp { get; private set; } + + [Argument('v', "version", "display version information")] + public static bool ShowVersion { get; private set; } + + [Argument('d', "debug", "run in debug mode")] public static bool Debug { get; private set; } + + [Argument('n', "no-logo", "suppress logo on startup")] + public static bool NoLogo { get; private set; } + + [Argument('i', "instance-name", "optional; a unique name for this instance")] public static string InstanceName { get; private set; } - public static bool LoggerLokiEnable { get; private set; } + + [Argument(default, "loki", "the url to a Grafana Loki instance to log to")] public static string LoggerLokiUrl { get; private set; } + public static bool LoggerLokiEnabled { get; private set; } + public static string Version { get; private set; } + public static Guid InvocationId { get; private set; } + public static int ProcessId { get; private set; } + public static void Main(string[] args) { - var runId = Guid.NewGuid(); - var processId = Environment.ProcessId; + try + { + Arguments.Populate(); + } + catch (Exception ex) + { + Console.WriteLine($"Error parsing input: {ex.Message}"); + } + + InvocationId = Guid.NewGuid(); + ProcessId = Environment.ProcessId; var assembly = Assembly.GetExecutingAssembly(); var assemblyVersion = assembly.GetName().Version; var informationVersion = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion; - var version = $"{assemblyVersion} ({informationVersion})"; + Version = $"{assemblyVersion} ({informationVersion})"; - if (args.Any(a => a == "--version" || a == "-v")) + if (ShowVersion) { - Console.WriteLine(version); + Console.WriteLine(Version); return; } - if (args.Any(a => a == "--help" || a == "-h")) + if (ShowHelp) { + if (!NoLogo) PrintBanner(Version); PrintHelp(); return; } Debug = Debugger.IsAttached - || args.Any(a => a == "--debug" || a == "-d") + || Debug || Environment.GetEnvironmentVariable("SLSKD_DEBUG") != null; - InstanceName = Environment.GetEnvironmentVariable("SLSKD_INSTANCE_NAME") ?? "default"; - - LoggerLokiUrl = Environment.GetEnvironmentVariable("SLSKD_LOGGER_LOKI_URL"); - LoggerLokiEnable = !string.IsNullOrWhiteSpace(LoggerLokiUrl) && Environment.GetEnvironmentVariable("SLSKD_LOGGER_LOKI_ENABLE") != null; + InstanceName ??= Environment.GetEnvironmentVariable("SLSKD_INSTANCE_NAME") ?? "default"; + LoggerLokiUrl ??= Environment.GetEnvironmentVariable("SLSKD_LOGGER_LOKI_URL"); + LoggerLokiEnabled = !string.IsNullOrEmpty(LoggerLokiUrl); + if (Debug) { Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .MinimumLevel.Override("Microsoft", LogEventLevel.Information) + .Enrich.WithProperty("Version", Version) .Enrich.WithProperty("InstanceName", InstanceName) - .Enrich.WithProperty("ProcessId", processId) - .Enrich.WithProperty("RunId", runId) + .Enrich.WithProperty("InvocationId", InvocationId) + .Enrich.WithProperty("ProcessId", ProcessId) .Enrich.FromLogContext() .WriteTo.Console( outputTemplate: "[{SourceContext}] [{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}") @@ -66,7 +96,7 @@ outputTemplate: "[{SourceContext}] [{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}", rollingInterval: RollingInterval.Day)) .WriteTo.Conditional( - e => LoggerLokiEnable, + e => LoggerLokiEnabled, config => config.GrafanaLoki( LoggerLokiUrl ?? string.Empty, outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")) @@ -78,9 +108,10 @@ .MinimumLevel.Information() .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("slskd.Security.PassthroughAuthenticationHandler", LogEventLevel.Information) + .Enrich.WithProperty("Version", Version) .Enrich.WithProperty("InstanceName", InstanceName) - .Enrich.WithProperty("ProcessId", processId) - .Enrich.WithProperty("RunId", runId) + .Enrich.WithProperty("InvocationId", InvocationId) + .Enrich.WithProperty("ProcessId", ProcessId) .Enrich.FromLogContext() .WriteTo.Console( outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}") @@ -90,22 +121,26 @@ outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}", rollingInterval: RollingInterval.Day)) .WriteTo.Conditional( - e => LoggerLokiEnable, + e => LoggerLokiEnabled, config => config.GrafanaLoki( LoggerLokiUrl ?? string.Empty, outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")) .CreateLogger(); } - PrintBanner(version); + if (!NoLogo) + { + PrintBanner(Version); + } var logger = Log.ForContext<Program>(); + logger.Information("Version: {Version}", Version); logger.Information("Instance Name: {InstanceName}", InstanceName); - logger.Information("Process ID: {ProcessId}", processId); - logger.Information("Run ID: {RunId}", runId); + logger.Information("Invocation ID: {InvocationId}", InvocationId); + logger.Information("Process ID: {ProcessId}", ProcessId); - if (LoggerLokiEnable) + if (LoggerLokiEnabled) { logger.Information("Logging to Loki instance at {LoggerLokiUrl}", LoggerLokiUrl); } @@ -136,32 +171,45 @@ public static void PrintBanner(string version) { + var padding = 56 - version.Length; + var paddingLeft = padding / 2; + var paddingRight = paddingLeft + (padding % 2); + + var centeredVersion = new string(' ', paddingLeft) + version + new string(' ', paddingRight); + var banner = @$" - oooo oooo .o8 - `888 `888 '888 - .oooo.o 888 .oooo.o 888 oooo .oooo888 - d88( '8 888 d88( '8 888 .8P' d88' `888 - `'Y88b. 888 `'Y88b. 888888. 888 888 - o. )88b 888 o. )88b 888 `88b. 888 888 - 8''888P' o888o 8''888P' o888o o888o `Y8bod88P -╒═══════════════════════════════════════════════╕ -│ GNU AFFERO GENERAL PUBLIC LICENSE │ -│ slskd.org │ -│ │ -│ {version} │ -└───────────────────────────────────────────────┘ - "; + __ __ __ + .-----. | | .-----. | |--. .--| | + |__ --| | | |__ --| | < | _ | + |_____| |__| |_____| |__|__| |_____| +╒════════════════════════════════════════════════════════╕ +│ GNU AFFERO GENERAL PUBLIC LICENSE │ +│ https://slskd.org │ +│ │ +│{centeredVersion}│ +└────────────────────────────────────────────────────────┘"; Console.WriteLine(banner); } public static void PrintHelp() { - Console.WriteLine(@" -usage: slskd [options] - --version|-v displays the current version - --debug|-d runs the daemon in debug mode - --help|-h prints this help"); + static string GetLongName(ArgumentInfo info) + => info.Property.PropertyType == typeof(bool) ? info.LongName : $"{info.LongName} <{info.Property.PropertyType.Name.ToLowerInvariant()}>"; + + static string GetShortName(ArgumentInfo info) + => info.ShortName > 0 ? $"-{info.ShortName}|" : " "; + + var arguments = Arguments.GetArgumentInfo(typeof(Program)); + var longestName = arguments.Select(a => GetLongName(a)).Max(n => n.Length); + + Console.WriteLine("\nusage: slskd [options]\n"); + Console.WriteLine("options:"); + foreach (var info in arguments) + { + var result = $" {GetShortName(info)}--{GetLongName(info).PadRight(longestName + 3)}{info.HelpText}"; + Console.WriteLine(result); + } } } } diff --git a/src/slskd/slskd.csproj b/src/slskd/slskd.csproj index 86faae5d..ed256675 100644 --- a/src/slskd/slskd.csproj +++ b/src/slskd/slskd.csproj @@ -36,6 +36,7 @@ <PackageReference Include="Soulseek" Version="1.0.1" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" /> <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" /> + <PackageReference Include="Utility.CommandLine.Arguments" Version="5.0.0" /> </ItemGroup> <ItemGroup> |
