From 583c8d351677a7f0e9b8129eece6f21e6b3ab1cf Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 7 Nov 2021 19:02:53 -0600 Subject: verify app directories --- src/slskd/Program.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index edecf4e8..db0f58b5 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -230,6 +230,10 @@ namespace slskd try { VerifyDirectory(OptionsAtStartup.Directories.App, createIfMissing: true, verifyWriteable: true); + VerifyDirectory(Path.Combine(OptionsAtStartup.Directories.App, "config"), createIfMissing: true, verifyWriteable: true); + VerifyDirectory(Path.Combine(OptionsAtStartup.Directories.App, "data"), createIfMissing: true, verifyWriteable: true); + VerifyDirectory(Path.Combine(OptionsAtStartup.Directories.App, "files"), createIfMissing: true, verifyWriteable: true); + VerifyDirectory(OptionsAtStartup.Directories.Incomplete, createIfMissing: true, verifyWriteable: true); VerifyDirectory(OptionsAtStartup.Directories.Downloads, createIfMissing: true, verifyWriteable: true); } -- cgit v1.2.3 From c62bb64823237ea7b413ff6e0b734e0eca30a870 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 7 Nov 2021 19:36:27 -0600 Subject: copy example config to app dir --- src/slskd/Program.cs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index db0f58b5..f649ce40 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -61,7 +61,7 @@ namespace slskd /// /// The default configuration filename. /// - public static readonly string DefaultConfigurationFile = Path.Combine(AppContext.BaseDirectory, "config", $"{AppName}.yml"); + public static readonly string DefaultConfigurationFile = Path.Combine(DefaultAppDirectory, $"{AppName}.yml"); /// /// The default incomplete download directory. @@ -230,9 +230,7 @@ namespace slskd try { VerifyDirectory(OptionsAtStartup.Directories.App, createIfMissing: true, verifyWriteable: true); - VerifyDirectory(Path.Combine(OptionsAtStartup.Directories.App, "config"), createIfMissing: true, verifyWriteable: true); VerifyDirectory(Path.Combine(OptionsAtStartup.Directories.App, "data"), createIfMissing: true, verifyWriteable: true); - VerifyDirectory(Path.Combine(OptionsAtStartup.Directories.App, "files"), createIfMissing: true, verifyWriteable: true); VerifyDirectory(OptionsAtStartup.Directories.Incomplete, createIfMissing: true, verifyWriteable: true); VerifyDirectory(OptionsAtStartup.Directories.Downloads, createIfMissing: true, verifyWriteable: true); @@ -243,6 +241,20 @@ namespace slskd return; } + try + { + // for convenience, try to copy the example configuration file to the application directory + // if any part fails, just move on silently. + var exampleFileName = $"{AppName}.example.yml"; + var source = Path.Combine(AppContext.BaseDirectory, "config", exampleFileName); + var destination = Path.Combine(OptionsAtStartup.Directories.App, exampleFileName); + File.Copy(source, destination); + } + catch + { + // no-op + } + Log.Logger = (OptionsAtStartup.Debug ? new LoggerConfiguration().MinimumLevel.Debug() : new LoggerConfiguration().MinimumLevel.Information()) .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("System.Net.Http.HttpClient", OptionsAtStartup.Debug ? LogEventLevel.Warning : LogEventLevel.Fatal) -- cgit v1.2.3 From ca8fe2230ae16b6d507e6a0adeaf7c4807453e24 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 7 Nov 2021 21:06:48 -0600 Subject: swap config option for AppDir --- src/slskd/Program.cs | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index f649ce40..e4c15704 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -58,20 +58,15 @@ namespace slskd /// public static readonly string DefaultAppDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.DoNotVerify), AppName); - /// - /// The default configuration filename. - /// - public static readonly string DefaultConfigurationFile = Path.Combine(DefaultAppDirectory, $"{AppName}.yml"); - /// /// The default incomplete download directory. /// - public static readonly string DefaultIncompleteDirectory = Path.Combine(DefaultAppDirectory, "incomplete"); + public static string DefaultIncompleteDirectory => Path.Combine(AppDirectory, "incomplete"); /// /// The default downloads directory. /// - public static readonly string DefaultDownloadsDirectory = Path.Combine(DefaultAppDirectory, "downloads"); + public static string DefaultDownloadsDirectory => Path.Combine(AppDirectory, "downloads"); /// /// The global prefix for environment variables. @@ -93,6 +88,13 @@ namespace slskd /// public static event EventHandler LogEmitted; + /// + /// Gets the path where application data is saved. + /// + [Argument(default, "appdir", "path where application data is saved")] + [EnvironmentVariable("APP_DIR")] + public static string AppDirectory { get; private set; } = DefaultAppDirectory; + /// /// Gets the assembly version of the application. /// @@ -135,11 +137,9 @@ namespace slskd public static ConcurrentFixedSizeQueue LogBuffer { get; } = new ConcurrentFixedSizeQueue(size: 500); private static IConfigurationRoot Configuration { get; set; } + private static string ConfigurationFile => Path.Combine(AppDirectory, $"{AppName}.yml"); private static OptionsAtStartup OptionsAtStartup { get; } = new OptionsAtStartup(); - [EnvironmentVariable("CONFIG")] - [Argument('c', "config", "path to configuration file")] - private static string ConfigurationFile { get; set; } = DefaultConfigurationFile; [Argument('g', "generate-cert", "generate X509 certificate and password for HTTPs")] private static bool GenerateCertificate { get; set; } @@ -229,8 +229,8 @@ namespace slskd try { - VerifyDirectory(OptionsAtStartup.Directories.App, createIfMissing: true, verifyWriteable: true); - VerifyDirectory(Path.Combine(OptionsAtStartup.Directories.App, "data"), createIfMissing: true, verifyWriteable: true); + VerifyDirectory(AppDirectory, createIfMissing: true, verifyWriteable: true); + VerifyDirectory(Path.Combine(AppDirectory, "data"), createIfMissing: true, verifyWriteable: true); VerifyDirectory(OptionsAtStartup.Directories.Incomplete, createIfMissing: true, verifyWriteable: true); VerifyDirectory(OptionsAtStartup.Directories.Downloads, createIfMissing: true, verifyWriteable: true); @@ -247,7 +247,7 @@ namespace slskd // if any part fails, just move on silently. var exampleFileName = $"{AppName}.example.yml"; var source = Path.Combine(AppContext.BaseDirectory, "config", exampleFileName); - var destination = Path.Combine(OptionsAtStartup.Directories.App, exampleFileName); + var destination = Path.Combine(AppDirectory, exampleFileName); File.Copy(source, destination); } catch @@ -268,7 +268,7 @@ namespace slskd outputTemplate: (OptionsAtStartup.Debug ? "[{SourceContext}] [{SoulseekContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}") .WriteTo.Async(config => config.File( - Path.Combine(OptionsAtStartup.Directories.App, "logs", $"{AppName}-.log"), + Path.Combine(AppDirectory, "logs", $"{AppName}-.log"), outputTemplate: (OptionsAtStartup.Debug ? "[{SourceContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}", rollingInterval: RollingInterval.Day)) .WriteTo.Conditional( @@ -298,9 +298,9 @@ namespace slskd PrintLogo(Version); } - if (ConfigurationFile != DefaultConfigurationFile && !IOFile.Exists(ConfigurationFile)) + if (!IOFile.Exists(ConfigurationFile)) { - logger.Warning($"Specified configuration file '{ConfigurationFile}' could not be found and was not loaded."); + logger.Warning($"Configuration file '{ConfigurationFile}' could not be found and was not loaded."); } logger.Information("Version: {Version}", Version); -- cgit v1.2.3 From 80748ef499215a5b39532b9500b277494e1119be Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 8 Nov 2021 18:57:05 -0600 Subject: wip --- src/slskd/Program.cs | 93 ++++++++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 46 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index e4c15704..3aba9ec3 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -54,46 +54,24 @@ namespace slskd public static readonly string AppName = "slskd"; /// - /// The default application data directory. - /// - public static readonly string DefaultAppDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.DoNotVerify), AppName); - - /// - /// The default incomplete download directory. - /// - public static string DefaultIncompleteDirectory => Path.Combine(AppDirectory, "incomplete"); - - /// - /// The default downloads directory. + /// The url to the issues/support site. /// - public static string DefaultDownloadsDirectory => Path.Combine(AppDirectory, "downloads"); + public static readonly string IssuesUrl = "https://github.com/slskd/slskd/issues"; /// /// The global prefix for environment variables. /// public static readonly string EnvironmentVariablePrefix = $"{AppName.ToUpperInvariant()}_"; - /// - /// The url to the issues/support site. - /// - public static readonly string IssuesUrl = "https://github.com/slskd/slskd/issues"; - /// /// The default XML documentation filename. /// public static readonly string XmlDocumentationFile = Path.Combine(AppContext.BaseDirectory, "etc", $"{AppName}.xml"); /// - /// Occurs when a new log event is emitted. - /// - public static event EventHandler LogEmitted; - - /// - /// Gets the path where application data is saved. + /// The default application data directory. /// - [Argument(default, "appdir", "path where application data is saved")] - [EnvironmentVariable("APP_DIR")] - public static string AppDirectory { get; private set; } = DefaultAppDirectory; + public static readonly string DefaultAppDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.DoNotVerify), AppName); /// /// Gets the assembly version of the application. @@ -101,7 +79,7 @@ namespace slskd /// /// Inaccurate when running locally. /// - public static Version AssemblyVersion { get; } = Assembly.GetExecutingAssembly().GetName().Version.Equals(new Version(1, 0, 0, 0)) ? new Version(0, 0, 0, 0) : Assembly.GetExecutingAssembly().GetName().Version; + public static readonly Version AssemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.Equals(new Version(1, 0, 0, 0)) ? new Version(0, 0, 0, 0) : Assembly.GetExecutingAssembly().GetName().Version; /// /// Gets the informational version of the application, including the git sha at the latest commit. @@ -109,27 +87,49 @@ namespace slskd /// /// Inaccurate when running locally. /// - public static string InformationalVersion { get; } = Assembly.GetExecutingAssembly().GetCustomAttribute().InformationalVersion == "1.0.0" ? "0.0.0" : Assembly.GetExecutingAssembly().GetCustomAttribute().InformationalVersion; + public static readonly string InformationalVersion = Assembly.GetExecutingAssembly().GetCustomAttribute().InformationalVersion == "1.0.0" ? "0.0.0" : Assembly.GetExecutingAssembly().GetCustomAttribute().InformationalVersion; + + /// + /// Gets the full application version, including both assembly and informational versions. + /// + public static readonly string Version = $"{AssemblyVersion} ({InformationalVersion})"; + + /// + /// Gets a value indicating whether the current version is a Canary build. + /// + public static readonly bool IsCanary = AssemblyVersion.Revision == 65534; /// /// Gets the unique Id of this application invocation. /// - public static Guid InvocationId { get; } = Guid.NewGuid(); + public static readonly Guid InvocationId = Guid.NewGuid(); /// /// Gets the Id of the current application process. /// - public static int ProcessId { get; } = Environment.ProcessId; + public static readonly int ProcessId = Environment.ProcessId; /// - /// Gets the full application version, including both assembly and informational versions. + /// Occurs when a new log event is emitted. /// - public static string Version { get; } = $"{AssemblyVersion} ({InformationalVersion})"; + public static event EventHandler LogEmitted; /// - /// Gets a value indicating whether the current version is a Canary build. + /// Gets the path where application data is saved. + /// + [Argument(default, "app", "path where application data is saved")] + [EnvironmentVariable("APP_DIR")] + public static string AppDirectory { get; private set; } = DefaultAppDirectory; + + /// + /// Gets the default downloads directory. /// - public static bool IsCanary { get; } = AssemblyVersion.Revision == 65534; + public static string DefaultDownloadsDirectory => Path.Combine(AppDirectory, "downloads"); + + /// + /// Gets the default incomplete download directory. + /// + public static string DefaultIncompleteDirectory => Path.Combine(AppDirectory, "incomplete"); /// /// Gets a buffer containing the last few log events. @@ -140,7 +140,6 @@ namespace slskd private static string ConfigurationFile => Path.Combine(AppDirectory, $"{AppName}.yml"); private static OptionsAtStartup OptionsAtStartup { get; } = new OptionsAtStartup(); - [Argument('g', "generate-cert", "generate X509 certificate and password for HTTPs")] private static bool GenerateCertificate { get; set; } @@ -241,18 +240,20 @@ namespace slskd return; } - try - { - // for convenience, try to copy the example configuration file to the application directory - // if any part fails, just move on silently. - var exampleFileName = $"{AppName}.example.yml"; - var source = Path.Combine(AppContext.BaseDirectory, "config", exampleFileName); - var destination = Path.Combine(AppDirectory, exampleFileName); - File.Copy(source, destination); - } - catch + // for convenience, try to copy the example configuration file to the application directory if no configuration + // file exists. if any part fails, just move on silently. + if (!IOFile.Exists(ConfigurationFile)) { - // no-op + try + { + var source = Path.Combine(AppContext.BaseDirectory, "config", $"{AppName}.example.yml"); + var destination = ConfigurationFile; + IOFile.Copy(source, destination); + } + catch + { + // no-op + } } Log.Logger = (OptionsAtStartup.Debug ? new LoggerConfiguration().MinimumLevel.Debug() : new LoggerConfiguration().MinimumLevel.Information()) -- cgit v1.2.3 From 86f5e46c2dfe595d8a970cc2f9e49d2520f14b55 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 9 Nov 2021 08:52:59 -0600 Subject: verify directories before config --- src/slskd/Program.cs | 76 ++++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 38 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 3aba9ec3..2403dbbf 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -198,8 +198,23 @@ namespace slskd return; } - // the application isn't being run in command mode. load all configuration values and proceed - // with bootstrapping. + // the application isn't being run in command mode. verify (create if needed) default application + // directories. if the downloads or complete directories are overridden in config, those will be + // validated after the config is loaded. + try + { + VerifyDirectory(AppDirectory, createIfMissing: true, verifyWriteable: true); + VerifyDirectory(Path.Combine(AppDirectory, "data"), createIfMissing: true, verifyWriteable: true); + VerifyDirectory(DefaultDownloadsDirectory, createIfMissing: true, verifyWriteable: true); + VerifyDirectory(DefaultIncompleteDirectory, createIfMissing: true, verifyWriteable: true); + } + catch (Exception ex) + { + Console.WriteLine($"Filesystem exception: {ex.Message}"); + return; + } + + // load and validate the configuration try { Configuration = new ConfigurationBuilder() @@ -226,36 +241,6 @@ namespace slskd return; } - try - { - VerifyDirectory(AppDirectory, createIfMissing: true, verifyWriteable: true); - VerifyDirectory(Path.Combine(AppDirectory, "data"), createIfMissing: true, verifyWriteable: true); - - VerifyDirectory(OptionsAtStartup.Directories.Incomplete, createIfMissing: true, verifyWriteable: true); - VerifyDirectory(OptionsAtStartup.Directories.Downloads, createIfMissing: true, verifyWriteable: true); - } - catch (Exception ex) - { - Console.WriteLine($"Filesystem exception: {ex.Message}"); - return; - } - - // for convenience, try to copy the example configuration file to the application directory if no configuration - // file exists. if any part fails, just move on silently. - if (!IOFile.Exists(ConfigurationFile)) - { - try - { - var source = Path.Combine(AppContext.BaseDirectory, "config", $"{AppName}.example.yml"); - var destination = ConfigurationFile; - IOFile.Copy(source, destination); - } - catch - { - // no-op - } - } - Log.Logger = (OptionsAtStartup.Debug ? new LoggerConfiguration().MinimumLevel.Debug() : new LoggerConfiguration().MinimumLevel.Information()) .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("System.Net.Http.HttpClient", OptionsAtStartup.Debug ? LogEventLevel.Warning : LogEventLevel.Fatal) @@ -299,11 +284,6 @@ namespace slskd PrintLogo(Version); } - if (!IOFile.Exists(ConfigurationFile)) - { - logger.Warning($"Configuration file '{ConfigurationFile}' could not be found and was not loaded."); - } - logger.Information("Version: {Version}", Version); if (IsCanary) @@ -313,9 +293,29 @@ namespace slskd logger.Warning($"Please report any issues here: {IssuesUrl}"); } - logger.Information("Instance Name: {InstanceName}", OptionsAtStartup.InstanceName); logger.Information("Invocation ID: {InvocationId}", InvocationId); logger.Information("Process ID: {ProcessId}", ProcessId); + logger.Information("Instance Name: {InstanceName}", OptionsAtStartup.InstanceName); + + logger.Information("Using application directory {AppDirectory}", AppDirectory); + logger.Information("Using configuration file {ConfigurationFile}", ConfigurationFile); + + // for convenience, try to copy the example configuration file to the application directory if no configuration + // file exists. if any part fails, just move on silently. + if (!IOFile.Exists(ConfigurationFile)) + { + try + { + logger.Warning("Configuration file {ConfigurationFile} does not exist; creating from example", ConfigurationFile); + var source = Path.Combine(AppContext.BaseDirectory, "config", $"{AppName}.example.yml"); + var destination = ConfigurationFile; + IOFile.Copy(source, destination); + } + catch (Exception ex) + { + logger.Error("Failed to create configuration file {ConfigurationFile}: {Message}", ConfigurationFile, ex.Message); + } + } if (!string.IsNullOrEmpty(OptionsAtStartup.Logger.Loki)) { -- cgit v1.2.3