From 45814aa89c7d56ba4f3c44583262d9ea06d932b2 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 8 Nov 2021 21:19:45 -0600 Subject: move things around, create config file if not exists --- 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