aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Program.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/slskd/Program.cs')
-rw-r--r--src/slskd/Program.cs141
1 files changed, 102 insertions, 39 deletions
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs
index 5ea7c304..5b86694a 100644
--- a/src/slskd/Program.cs
+++ b/src/slskd/Program.cs
@@ -10,79 +10,98 @@
using System;
using System.Diagnostics;
using System.IO;
+ using System.Linq;
using System.Reflection;
using System.Text.Json;
-
+
public class Program
{
+ [CommandLineArgument('v', "version", "display version information")]
+ public static bool ShowVersion { get; private set; }
+
+ [CommandLineArgument('h', "help", "display command line usage")]
+ public static bool ShowHelp { get; private set; }
+
+ [CommandLineArgument('e', "envars", "display environment variables")]
+ public static bool ShowEnvironmentVariables { 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 readonly string ConfigurationFile = "config.yml";
+ public static readonly string EnvironmentVariablePrefix = "SLSKD_";
- private static Action<BinderOptions> BinderOptions = (o) => { o.BindNonPublicProperties = true; };
- public static RuntimeOptions.slskd Options { get; private set; } = new();
-
+ public static Configuration.Program Options { get; private set; } = new Configuration.Program();
public static void Main(string[] args)
{
- new ConfigurationBuilder()
- .MapCommandLineArguments(RuntimeOptions.ArgumentMap, Environment.CommandLine)
- .Build()
- .GetSection("slskd")
- .Bind(Options, BinderOptions);
+ CommandLineArguments.Populate();
var assembly = Assembly.GetExecutingAssembly();
var assemblyVersion = assembly.GetName().Version;
var informationVersion = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
Version = $"{assemblyVersion} ({informationVersion})";
- if (Options.ShowVersion)
+ if (ShowVersion)
{
Console.WriteLine(Version);
return;
}
- if (Options.ShowHelp)
+ if (ShowHelp)
{
- if (!InstanceOptions.DisableLogo)
- {
- PrintLogo(Version);
- }
+ PrintLogo(Version);
+ PrintCommandLineArguments();
+ return;
+ }
- InstanceOptions.PrintHelp();
+ if (ShowEnvironmentVariables)
+ {
+ PrintLogo(Version);
+ PrintEnvironmentVariables();
return;
}
- new ConfigurationBuilder()
- .MapEnvironmentVariables(RuntimeOptions.EnvironmentVariableMap)
+ var configuration = new ConfigurationBuilder()
+ .AddEnvironmentVariables(
+ prefix: EnvironmentVariablePrefix,
+ map: Configuration.Map.Select(o => o.ToEnvironmentVariable()))
.SetBasePath(Directory.GetCurrentDirectory())
.AddYamlFile(ConfigurationFile, optional: true, reloadOnChange: false)
- .MapCommandLineArguments(RuntimeOptions.ArgumentMap, Environment.CommandLine)
- .Build()
- .GetSection("slskd")
- .Bind(Options, BinderOptions);
+ .AddCommandLine(
+ commandLine: Environment.CommandLine,
+ map: Configuration.Map.Select(o => o.ToCommandLineArgument()))
+ .Build();
- Console.WriteLine(JsonSerializer.Serialize(Options));
+ configuration
+ .GetSection("slskd")
+ .Bind(Options, (o) =>
+ {
+ o.BindNonPublicProperties = true;
+ });
- return;
+ if (Options.Debug)
+ {
+ Console.WriteLine(configuration.GetDebugView());
+ Console.WriteLine(JsonSerializer.Serialize(Options));
+ }
InvocationId = Guid.NewGuid();
ProcessId = Environment.ProcessId;
- if (!InstanceOptions.DisableLogo)
+ if (!Options.NoLogo)
{
PrintLogo(Version);
}
- if (InstanceOptions.Debug || Debugger.IsAttached)
+ if (Options.Debug || Debugger.IsAttached)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.WithProperty("Version", Version)
- .Enrich.WithProperty("InstanceName", InstanceOptions.InstanceName)
+ .Enrich.WithProperty("InstanceName", Options.InstanceName)
.Enrich.WithProperty("InvocationId", InvocationId)
.Enrich.WithProperty("ProcessId", ProcessId)
.Enrich.FromLogContext()
@@ -94,9 +113,9 @@
outputTemplate: "[{SourceContext}] [{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day))
.WriteTo.Conditional(
- e => InstanceOptions.EnableLoki,
+ e => !string.IsNullOrEmpty(Options.Logger.Loki),
config => config.GrafanaLoki(
- InstanceOptions.LokiUrl ?? string.Empty,
+ Options.Logger.Loki ?? string.Empty,
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"))
.CreateLogger();
}
@@ -107,7 +126,7 @@
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("slskd.Security.PassthroughAuthenticationHandler", LogEventLevel.Information)
.Enrich.WithProperty("Version", Version)
- .Enrich.WithProperty("InstanceName", InstanceOptions.InstanceName)
+ .Enrich.WithProperty("InstanceName", Options.InstanceName)
.Enrich.WithProperty("InvocationId", InvocationId)
.Enrich.WithProperty("ProcessId", ProcessId)
.Enrich.FromLogContext()
@@ -119,9 +138,9 @@
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day))
.WriteTo.Conditional(
- e => InstanceOptions.EnableLoki,
+ e => !string.IsNullOrEmpty(Options.Logger.Loki),
config => config.GrafanaLoki(
- InstanceOptions.LokiUrl ?? string.Empty,
+ Options.Logger.Loki ?? string.Empty,
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"))
.CreateLogger();
}
@@ -129,33 +148,33 @@
var logger = Log.ForContext<Program>();
logger.Information("Version: {Version}", Version);
- logger.Information("Instance Name: {InstanceName}", InstanceOptions.InstanceName);
+ logger.Information("Instance Name: {InstanceName}", Options.InstanceName);
logger.Information("Invocation ID: {InvocationId}", InvocationId);
logger.Information("Process ID: {ProcessId}", ProcessId);
- if (InstanceOptions.DisableAuthentication)
+ if (Options.NoAuth)
{
logger.Warning("Authentication of web requests is DISABLED");
}
- if (InstanceOptions.EnablePrometheus)
+ if (Options.Feature.Prometheus)
{
logger.Information("Publishing Prometheus metrics to /metrics");
}
- if (InstanceOptions.EnableSwagger)
+ if (Options.Feature.Swagger)
{
logger.Information("Publishing Swagger documentation to /swagger");
}
- if (InstanceOptions.EnableLoki)
+ if (!string.IsNullOrEmpty(Options.Logger.Loki))
{
- logger.Information("Logging to Loki instance at {LoggerLokiUrl}", InstanceOptions.LokiUrl);
+ logger.Information("Logging to Loki instance at {LoggerLokiUrl}", Options.Logger.Loki);
}
try
{
- if (InstanceOptions.EnablePrometheus)
+ if (Options.Feature.Prometheus)
{
using var runtimeMetrics = DotNetRuntimeStatsBuilder.Default().StartCollecting();
}
@@ -204,5 +223,49 @@
Console.WriteLine(banner);
}
+
+ public static void PrintCommandLineArguments()
+ {
+ static string GetLongName(string longName, Type type)
+ => type == typeof(bool) ? longName : $"{longName} <{type.Name.ToLowerInvariant()}>";
+
+ var longestName = Configuration.Map.Select(a => GetLongName(a.LongName, a.Type)).Max(n => n.Length);
+
+ Console.WriteLine("\nusage: slskd [arguments]\n");
+ Console.WriteLine("arguments:\n");
+ foreach (Option option in Configuration.Map)
+ {
+ var (shortName, longName, _, type, key, description) = option;
+
+ if (shortName == default && string.IsNullOrEmpty(longName))
+ {
+ continue;
+ }
+
+ var result = $" {shortName}|--{GetLongName(longName, type).PadRight(longestName + 3)}{description}";
+ Console.WriteLine(result);
+ }
+ }
+
+ public static void PrintEnvironmentVariables()
+ {
+ static string GetName(string name, Type type) => $"{name} <{type.Name.ToLowerInvariant()}>";
+
+ var longestName = Configuration.Map.Select(a => GetName(a.EnvironmentVariable, a.Type)).Max(n => n.Length);
+
+ Console.WriteLine("\nenvironment variables (arguments and config.yml have precedence):\n");
+ foreach (Option option in Configuration.Map)
+ {
+ var (_, _, environmentVariable, type, key, description) = option;
+
+ if (string.IsNullOrEmpty(environmentVariable) || string.IsNullOrEmpty(key))
+ {
+ continue;
+ }
+
+ var result = $" {EnvironmentVariablePrefix}{GetName(environmentVariable, type).PadRight(longestName + 3)}{description}";
+ Console.WriteLine(result);
+ }
+ }
}
}