From 87f024fa2561f751fc26c129bfaa373a966f1c67 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 12 Feb 2021 17:46:29 -0600 Subject: docs and cleanup --- src/slskd/Program.cs | 53 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 60ead86f..750ed8f2 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -40,16 +40,55 @@ namespace slskd using Utility.CommandLine; using Utility.EnvironmentVariables; + /// + /// Bootstraps configuration and handles primitive command-line instructions. + /// public static class Program { + /// + /// The name of the application. + /// public static readonly string AppName = "slskd"; + + /// + /// The default configuration filename. + /// public static readonly string DefaultConfigurationFile = $"{AppName}.yml"; + + /// + /// The global prefix for environment variables. + /// public static readonly string EnvironmentVariablePrefix = $"{AppName.ToUpperInvariant()}_"; + /// + /// Gets the assembly version of the application. + /// + /// + /// Inaccurate when running locally. + /// public static Version AssemblyVersion { get; } = Assembly.GetExecutingAssembly().GetName().Version; + + /// + /// Gets the informational version of the application, including the git sha at the latest commit. + /// + /// + /// Inaccurate when running locally. + /// public static string InformationalVersion { get; } = Assembly.GetExecutingAssembly().GetCustomAttribute().InformationalVersion; + + /// + /// Gets the unique Id of this application invocation. + /// public static Guid InvocationId { get; } = Guid.NewGuid(); + + /// + /// Gets the Id of the current application process. + /// public static int ProcessId { get; } = Environment.ProcessId; + + /// + /// Gets the full application version, including both assembly and informational versions. + /// public static string Version { get; } = $"{AssemblyVersion} ({InformationalVersion})"; private static IConfigurationRoot Configuration { get; set; } @@ -74,8 +113,14 @@ namespace slskd [Argument('v', "version", "display version information")] private static bool ShowVersion { get; set; } + /// + /// Entrypoint. + /// + /// Command line arguments. public static void Main(string[] args) { + // populate the properties above so that we can override the default config file if needed, and to + // check if the application is being run in command mode (run task and quit). EnvironmentVariables.Populate(prefix: EnvironmentVariablePrefix); Arguments.Populate(clearExistingValues: false); @@ -111,13 +156,15 @@ namespace slskd return; } + // the application isn't bein run in command mode. load all configuration values and proceed + // with bootstrapping. try { Configuration = new ConfigurationBuilder() .AddConfigurationProviders(EnvironmentVariablePrefix, ConfigurationFile) .Build(); - Configuration.GetSection("slskd") + Configuration.GetSection(AppName) .Bind(Options, (o) => { o.BindNonPublicProperties = true; }); if (!Options.TryValidate(out var result)) @@ -210,8 +257,6 @@ namespace slskd logger.Information("Invocation ID: {InvocationId}", InvocationId); logger.Information("Process ID: {ProcessId}", ProcessId); - return; - if (!string.IsNullOrEmpty(Options.Logger.Loki)) { logger.Information("Forwarding logs to Grafana Loki instance at {LoggerLokiUrl}", Options.Logger.Loki); @@ -283,7 +328,7 @@ namespace slskd path: Path.GetFileName(configurationFile), optional: true, reloadOnChange: false, - provider: new PhysicalFileProvider(Path.GetDirectoryName(configurationFile), ExclusionFilters.None)) + provider: new PhysicalFileProvider(Path.GetDirectoryName(configurationFile), ExclusionFilters.None)) // required for locations outside of the app directory .AddCommandLine( targetType: typeof(Options), commandLine: Environment.CommandLine); -- cgit v1.2.3